一、基于注解的IOC注入
1.1 对应的XML配置文件的Spring注入
public class Student {
private Integer number ;
private String name ;
private Date date ;
public void init(){
System.out.println("Student对象被初始化。。。");
}
public void destroy(){
System.out.println("Student对象被销毁。。。");
}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="student" class="com.json.pojo.Student"
scope="singleton" init-method="init" destroy-method="destroy">
<property name="number" value="001"></property>
<property name="name" value="json"></property>
<property name="date" ref="now"></property>
</bean>
<bean id="now" class="java.util.Date"/>
</beans>
改变对应的注解分为四类:
- 创建对象的注解
- 注入数据的注解
- 改变作用范围的注解
- 生命周期的注解
1.2 创建对象的注解
- 作用和在XML配置文件中编写一个**< bean >**标签实现的功能一样
- Component:
- 作用:用于把当前类对象存入spring容器中
- 属性:value:用于指定bean的id(不写默认为当前类名首字母小写)
- Controller:一般用在表现层
- Service:一般用在业务层
- Repository:一般用在持久层
以上三个注解他们的作用和属性与Component是一模一样,他们三个是Spring框架为我们提供明确的三层使用的注解,使我们的三层对象更加清晰
@Service("accountService")
public class AccountService {
}
1.3 注入数据的注解
- 作用就和在XML配置文件中编写一个**< property>**标签实现的功能一样
- Autowired:可以标注变量,也可以标注方法
- 作用:自动按照类型注入(容器中配置的其他bean)
- 容器中有唯一的一个bean对象类型和要注入的变量类型匹配,就可以注入成功
- 如果IOC容器中没有任何bean的类型和要注入的变量类型匹配,则报错
- 如果IOC容器中有多个类型匹配时,则在匹配的bean的id中,根据要注入的变量名确定,若所有匹配的bean的id没有与要注入的变量名匹配,则注入失败
- 使用注解注入时,不需指定set方法
- 作用:自动按照类型注入(容器中配置的其他bean)
- Qualifier:
- 属性:value:用于指定注入bean的id
- 在自动按照类型注入的基础之上,再指定Bean的id注入
- 在给成员变量注入时不能独立使用,必须和 @Autowire 一起使用
- 给方法参数注入时,可以独立使用
- Resource:
- 作用:直接按照bean的id注入。它可以独立使用
- 属性:name:用于指定bean的id
- Value:
- 作用:用于注入基本类型和String类型的数据
- 属性:value:用于指定数据的值
- 可以使用spring中SpEL(也就是spring的el表达式)
- SpEL的写法:${表达式}
public class Student {
@Value("23")
private Integer number ;
@Value("json")
private String name ;
@Autowired
@Qualifier("now")
@Resource(name="now")
private Date date ;
}
1.4 改变作用范围的注解
- 作用就和在bean标签中使用scope属性实现的功能是一样的
- Scope:
- 作用:用于指定bean的作用范围
- 属性:value:指定范围的取值。常用取值:singleton prototype
@Component
@Scope("prototype")
public class Student {
}
1.5 生命周期的注解
- 作用就和在bean标签中使用init-method和destroy-method的作用是一样的
- PreDestroy:
- 作用:用于指定销毁方法
- PostConstruct:
- 作用:用于指定初始化方法
@Component
public class Student {
@PostConstruct
public void init(){
System.out.println("Student对象被初始化。。。");
}
@PreDestroy
public void destroy(){
System.out.println("Student对象被销毁。。。");
}
}
1.6 注解创建Bean与注入数据
基于注解,需要一个扫描器扫描注解才能生效
导入约束时需要多导入一个 context 名称空间下的约束,配置扫描器,扫描注解
<?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" >
<!--告知spring在创建容器时要扫描的包,配置所需要的标签不是在beans的约束中,而是一个名称为
context名称空间和约束中,该包下及子包所有的注解均被扫描到-->
<context:component-scan base-package="com.json" />
<bean id="now" class="java.util.Date"></bean>
</beans>
@Component
@Scope("singleton")
public class Student {
@Value("23")
private Integer number ;
@Value("json")
private String name ;
@Autowired
@Qualifier("now")
private Date date ;
@PostConstruct
public void init(){
System.out.println("Student对象被初始化。。。");
}
@PreDestroy
public void destroy(){
System.out.println("Student对象被销毁。。。");
}
}
注意:集合类型无法使用注解注入
二、配置类
基于注解的IOC 配置已经完成,但是依然离不开 spring 的 xml 配 置文件
之所以离不开 xml 配置文件,是因为有一个很关键的配置,即需要在配置文件配置扫描注解的扫描器,而且需要配置第三方jar的Bean对象也需要xml文件
<?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="com.json" />
</beans>
那么能不能不写xml 配置文件,所有的配置都用注解来实现,扫描配置使用注解,第三方jar的Bean也使用注解配置
2.1 @Configuration
@Configuration:
-
作用:用于指定当前类为一个Spring的配置类,该配置类取代XML配置文件
-
该配置类的Bean的获取需要用AnnotationConfigApplicationContext对象
@Configuration
public class SpringConfiguration {
}
2.2 @ComponentScan
@ComponentScan:
- 作用:用于指定Spring 要扫描的包或配置类,作用和在 Spring 的 xml 配置文件中的扫描器一样
- 属性:
- basePackages[]:用于指定要扫描的包
- value[]:用于指定要扫描的包(常用)
- basePackageClasses[]:扫描指定的类(数组)
@Component
public class Student {
}
@Configuration
//扫描com.json包及子包所有注解
@ComponentScan("com.json")
public class SpringConfiguration