Spring Annotation(三)
使用 @Autowired 注释
Spring 2.5 引入了 @Autowired
注释,它可以对类成员变量、方法及构造函数进行标注,完成自动装配的工作。来看一下使用 @Autowired
进行成员变量自动注入的代码:
清单 6. 使用 @Autowired 注释的 Boss.java
package com.baobaotao;
import org.springframework.beans.factory.annotation.Autowired;
public class Boss {
@Autowired
private Car car;
@Autowired
private Office office;
…
}
|
Spring 通过一个 BeanPostProcessor
对 @Autowired
进行解析,所以要让 @Autowired
起作用必须事先在 Spring 容器中声明 AutowiredAnnotationBeanPostProcessor
Bean。
清单 7. 让 @Autowired 注释工作起来
<?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-2.5.xsd">
<!-- 该 BeanPostProcessor 将自动起作用,对标注 @Autowired 的 Bean 进行自动注入 -->
<bean class="org.springframework.beans.factory.annotation.
AutowiredAnnotationBeanPostProcessor"/>
<!-- 移除 boss Bean 的属性注入配置的信息 -->
<bean id="boss" class="com.baobaotao.Boss"/>
<bean id="office" class="com.baobaotao.Office">
<property name="officeNo" value="001"/>
</bean>
<bean id="car" class="com.baobaotao.Car" scope="singleton">
<property name="brand" value=" 红旗 CA72"/>
<property name="price" value="2000"/>
</bean>
</beans>
|
这样,当 Spring 容器启动时,AutowiredAnnotationBeanPostProcessor
将扫描 Spring 容器中所有 Bean,当发现 Bean 中拥有 @Autowired
注释时就找到和其匹配(默认按类型匹配)的 Bean,并注入到对应的地方中去。
按照上面的配置,Spring 将直接采用 Java 反射机制对 Boss 中的 car
和 office
这两个私有成员变量进行自动注入。所以对成员变量使用 @Autowired
后,您大可将它们的 setter 方法(setCar()
和 setOffice()
)从 Boss 中删除。
当然,您也可以通过 @Autowired
对方法或构造函数进行标注,来看下面的代码:
清单 8. 将 @Autowired 注释标注在 Setter 方法上
package com.baobaotao;
public class Boss {
private Car car;
private Office office;
@Autowired
public void setCar(Car car) {
this.car = car;
}
@Autowired
public void setOffice(Office office) {
this.office = office;
}
…
}
|
这时,@Autowired
将查找被标注的方法的入参类型的 Bean,并调用方法自动注入这些 Bean。而下面的使用方法则对构造函数进行标注:
清单 9. 将 @Autowired 注释标注在构造函数上
package com.baobaotao;
public class Boss {
private Car car;
private Office office;
@Autowired
public Boss(Car car ,Office office){
this.car = car;
this.office = office ;
}
…
}
|
由于 Boss()
构造函数有两个入参,分别是 car
和 office
,@Autowired
将分别寻找和它们类型匹配的 Bean,将它们作为 Boss(Car car ,Office office)
的入参来创建 Boss
Bean。
分享到:
相关推荐
Spring annotation 样例
Spring Annotation
spring annotation注解 使用Spring注解来注入属性
spring的Annotation注解.
NULL 博文链接:https://cyhcheng.iteye.com/blog/390551
Spring的Annotation配置相关讲义
详解 Spring 3.0 基于 Annotation 的依赖注入实现。。详解 Spring 3.0 基于 Annotation 的依赖注入实现。。
花了些时间做了一个实验,彻底弄懂了spring Annotation注入的方式。凡带有@Component,@Controller,@Service,@Repository 标志的等于告诉Spring这类将自动产生对象,而@Resource则等于XML配置中的ref,告诉spring此处...
Spring_Annotation_AOP
关于Spring annotation的详细介绍,包括 @Component,@Autowired 等,感觉还不错。
Spring+mybatis annotation形式 进行增删查改,使用maven容器,配置完整,自己运行多次无问题
SpringMVC Spring MyBatis 框架整合 Annotation Maven Project
NULL 博文链接:https://dolphin-ygj.iteye.com/blog/548314
Spring IOC Annotation 注入 学习实例
spring annotation 从入门到精通
spring annotation 注解 maven 配套的源码
spring annotation example source code java maven
Spring注解开发代码+脑图+源码分析+spingmvc源码分析
spring使用annotation整合dwr笔记 spring使用annotation整合dwr笔记 spring使用annotation整合dwr笔记