spring_ioc相关_第二章

继spring_ioc相关_第一章之后,继续ioc的第二章内容

上章内容回顾:

1 spring的概念

(1)核心:ioc和aop

(2)spring一站式框架

2 spring的bean管理(xml)

(1)bean实例化

(2)注入属性

(3)注入对象属性

3 ioc和di

(1)ioc:控制反转,把对象创建交给spring管理

(2)di:依赖注入,创建对象过程中,向属性设置值

4 在服务器启动时候加载配置文件,创建对象

(1)ServletContext对象

(2)监听器

这章内容简介:

  spring的bean管理(注解)

(1)使用注解创建对象

- 四个注解

(2)使用注解注入属性

- Autowired

- Resource

(3)xml和注解方式混合使用

- 创建对象使用配置文件,注入属性使用注解

 

 spring注解开发准备

(1) 导入基本的jar包

(2) 创建类 创建方法

(3) 创建spring配置文件,引入约束 schema约束有两个: 1第一天的基本功能,引入约束beans 2ioc的注解开发,引入新的约束

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
        
       <!--  开启注解扫描 -->
       <context:component-scan base-package="cn.hr.*"></context:component-scan>
        
</beans>

(4) 开启注解扫描

其中cn.hr.* 为要扫描spring注解的所有包文件

 

  注解创建对象

创建对象有四个注解

@Component

@Controller  : 业务层

@Service : 业务层

@Respository: 持久层

有注释的三个注解是为了让标注类本身的用途清晰,Spring会在后续版本对齐增强,目前这四个注解功能一致,都创建对象

创建对象是单实例还是多实例: spring默认是单实例的,线程不安全的,若要多实例则使用@Scope注解如:

 

注解注入属性

1 创建service类,创建dao类,在service得到dao对象

注入属性第一个注解 @Autowired

1)创建daoservice对象

2)在service类里面定义dao类型属性

 

注入属性第二个注解 @Resource

 

实例代码:

CarService中关联CarDao,CarDao中引入Car

CarService:

package cn.hr.service;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import cn.hr.dao.CarDao;

public class CarService {
	@Autowired
	private CarDao carDao;
	//service中引入dao
	public void add() {
		System.out.println("carService add...");
		carDao.add();
		
	}
}

CarDao:

package cn.hr.dao;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.hr.bean.Car;

@Service(value="carDao")
public class CarDao {
//	@Autowired
//	private Car car;  //使用注解注入对象属性实现一
	
	@Resource(name="car")
	private Car car;   //使用注解注入对象属性二
	
	public void add() {
		car.setCarName("bmw");
		car.setPrice(200000);
		System.out.println("cardao add...." + car.toString());
	}
}

Car:

package cn.hr.bean;

import org.springframework.stereotype.Component;

@Component(value="car")
public class Car {
	private String carName;
	private int price;

	public String getCarName() {
		return carName;
	}

	public void setCarName(String carName) {
		this.carName = carName;
	}

	public int getPrice() {
		return price;
	}

	public void setPrice(int price) {
		this.price = price;
	}



	public void add() {
		System.out.println("增加一辆车");
	}

	@Override
	public String toString() {
		return "Car [carName=" + carName + ", price=" + price + "]";
	}
	
}

测试类:

package cn.hr.test;

import org.junit.Test;
import org.junit.validator.PublicClassValidator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import cn.hr.bean.Car;
import cn.hr.service.CarService;

public class TestAnnotation {

	@Test
	public void car() {
		ApplicationContext context = new ClassPathXmlApplicationContext("application.xml");
		Car car=  (Car) context.getBean("car");
		car.add();
	}
	
	@Test
	public void add() {
		ApplicationContext context = new ClassPathXmlApplicationContext("application.xml");
		CarService carService = (CarService) context.getBean("carService");
		carService.add();
	}
	
	
}

github:https://github.com/2402zmybie/spring_ioc_02

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值