继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的注解开发,引入新的约束
(4) 开启注解扫描
其中cn.hr.* 为要扫描spring注解的所有包文件
注解创建对象
创建对象有四个注解
@Component
@Controller : 业务层
@Service : 业务层
@Respository: 持久层
有注释的三个注解是为了让标注类本身的用途清晰,Spring会在后续版本对齐增强,目前这四个注解功能一致,都创建对象
创建对象是单实例还是多实例: spring默认是单实例的,线程不安全的,若要多实例则使用@Scope注解如:
注解注入属性
1 创建service类,创建dao类,在service得到dao对象 注入属性第一个注解 @Autowired (1)创建dao和service对象 (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