1 使用配置文件的方法来完成自动装配
我们编写spring 框架的代码时候。一直遵循是这样一个规则:所有在spring中注入的bean 都建议定义成私有的域变量。并且要配套写上 get 和 set方法。
比如:Boss 拥有 Office 和 Car 类型的两个属性:
public class Boss {
private Car car;
private Office office;

// 省略 get/setter

@Override
public String toString() {
return "car:" + car + "/n" + "office:" + office;
}
}
我们在 Spring 容器中将 Office 和 Car 声明为 Bean,并注入到 Boss Bean 中。下面是使用传统 XML 完成这个工作的配置文件 beans.xml:
beans.xml 将以上三个类配置成 Bean,然后Spring容器会自动装配起来。
<?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">
<bean id="boss" class="com.baobaotao.Boss">
<property name="car" ref="car"/>
<property name="office" ref="office" />
</bean>
<bean id="office" class="com.baobaotao.Office">
<property name="officeNo" value="002"/>
</bean>
<bean id="car" class="com.baobaotao.Car" scope="singleton">
<property name="brand" value=" 红旗 CA72"/>
<property name="price" value="2000"/>
</bean>
</beans>
当我们运行以下代码时,控制台将正确打出 boss 的信息:
public class AnnoIoCTest {

public static void main(String[] args) {
String[] locations = {"beans.xml"};
ApplicationContext ctx =
new ClassPathXmlApplicationContext(locations);
Boss boss = (Boss) ctx.getBean("boss");
System.out.println(boss);
}
}
这说明 Spring 容器已经正确完成了 Bean 创建和装配的工作。

2 使用@Autowired 注解来完成自动装配
Spring 2.5 引入了 @Autowired 注释,它可以对类成员变量、方法及构造函数进行标注,完成自动装配的工作。 通过 @Autowired的使用来消除 set ,get方法,简化程序代码。
要实现我们要精简程序的目的。需要这样来处理:
① 在applicationContext.xml中加入:
<!-- 该 BeanPostProcessor 将自动对标注 @Autowired 的 Bean 进行注入 -->
<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>
Spring 通过一个 BeanPostProcessor 对 @Autowired 进行解析,所以要让 @Autowired 起作用必须事先在 Spring 容器中声明 AutowiredAnnotationBeanPostProcessor Bean。

②修改在原来注入spirng容器中的bean的方法。
在域变量上加上标签@Autowired,并且去掉 相应的get 和set方法
public class Boss {

@Autowired
private Car car;

@Autowired
private Office office;


}

③在applicatonContext.xml中 把原来引用的<porpery >标签也去掉。
<?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 对方法或构造函数进行标注。
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。而下面的使用方法则对构造函数进行标注:
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。

注意:在第一步中,在applicationContext.xml中加入的那个bean是对@Autowired注解进行解析的处理器,但只能实现对标注了@Autowired 的 Bean 进行注入,无法对其他注解的注入,所以如果项目中还使用了别的注解的话,是不推荐这样做的!一般在applicationContext.xml中常用开启注解配置的是显式的配置<context:annotation-config/>,就可以开启多个注解,因为该配置隐式注册了多个对注解进行解析的处理器,如下列举 :
AutowiredAnnotationBeanPostProcessor CommonAnnotationBeanPostProcessor
PersistenceAnnotationBeanPostProcessor RequiredAnnotationBeanPostProcessor
更通用的方法就是:显式的配置<context:component-scan base-package=" "/>。因为component-scan标签默认情况下自动扫描指定路径下的包(含所有子包),将带有@Component、@Repository、@Service、@Controller标签的类自动注册到spring容器。对标记了@Required、@Autowired(spring);@PostConstruct、@PreDestroy、@Resource(JSR250);@WebServiceRef(JAX-WS);@EJB(EJB3); @PersistenceContext、@PersistenceUnit(JPA)等注解的类进行对应的操作使注解生效(这其实就包含了annotation-config标签的作用,所以配置了<context:component-scan base-package=""/>之后,便无需再配置<context:annotation-config/>)。

Spring中@Autowired注解与自动装配的更多相关文章

  1. spring boot 中@Autowired注解无法自动注入的错误

    版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net/huihuilovei/article/de ...

  2. Spring中@Autowired注解、@Resource注解的区别 (zz)

    Spring中@Autowired注解.@Resource注解的区别 Spring不但支持自己定义的@Autowired注解,还支持几个由JSR-250规范定义的注解,它们分别是@Resource.@ ...

  3. 使用Spring的JavaConfig 和 @Autowired注解与自动装配

    1 JavaConfig  配置方法 之前我们都是在xml文件中定义bean的,比如: 1 2 3 4 5 6 7 8 <beans xmlns="http://www.springf ...

  4. Spring@Autowired注解与自动装配

    1   配置文件的方法 我们编写spring 框架的代码时候.一直遵循是这样一个规则:所有在spring中注入的bean 都建议定义成私有的域变量.并且要配套写上 get 和 set方法. Boss ...

  5. 【转】Spring@Autowired注解与自动装配

    1   配置文件的方法 我们编写spring 框架的代码时候.一直遵循是这样一个规则:所有在spring中注入的bean 都建议定义成私有的域变量.并且要配套写上 get 和 set方法. Boss ...

  6. Spring@Autowired注解与自动装配(转发)

    1   配置文件的方法 我们编写spring 框架的代码时候.一直遵循是这样一个规则:所有在spring中注入的bean 都建议定义成私有的域变量.并且要配套写上 get 和 set方法. Boss ...

  7. [转] Spring@Autowired注解与自动装配

    1   配置文件的方法 我们编写spring 框架的代码时候.一直遵循是这样一个规则:所有在spring中注入的bean 都建议定义成私有的域变量.并且要配套写上 get 和 set方法. Boss ...

  8. Spring中 @Autowired注解与J2EE@Resource注解的区别

    在开发中经常使用到@Autowired和@Resource进行装配. 不禁好奇这两个注解的差异在何处??? 相同点: @Resource的作用相当于@Autowired,均可标注在字段或属性的sett ...

  9. Spring中@Autowired注解、@Resource注解的区别

    Spring不但支持自己定义的@Autowired注解,还支持几个由JSR-250规范定义的注解,它们分别是@Resource.@PostConstruct以及@PreDestroy. @Resour ...

随机推荐

  1. Jdk5.0新特性

    增强for循环:foreach语句,foreach简化了迭代器. 格式:// 增强for循环括号里写两个参数,第一个是声明一个变量,第二个就是需要迭代的容器 for( 元素类型 变量名 : Colle ...

  2. php 中 global 与 $GLOBAL 由引用产生的区别

    很多人都认为global和$GLOBALS[]只是写法上面的差别,其实不然. 根据官方的解释是 $GLOBALS['var'] 是外部的全局变量$var本身. global $var 是外部$var的 ...

  3. About using UTF-8 fields in MySQL

    https://www.adayinthelifeof.nl/2010/12/04/about-using-utf-8-fields-in-mysql/ I sometimes hear: “make ...

  4. centos平台openstack spice配置

    配置过程只涉及控制节点(192.168.209.11)和计算节点(192.168.209.31),根据情况修改为实际环境的IP地址.     修改控制节点 安装软件包 yum install spic ...

  5. The Fortified Forest - POJ 1873(状态枚举+求凸包周长)

    题目大意:有个国王他有一片森林,现在他想从这个森林里面砍伐一些树木做成篱笆把剩下的树木围起来,已知每个树都有不同的价值还有高度,求出来砍掉那些树可以做成篱笆把剩余的树都围起来,要使砍伐的树木的价值最小 ...

  6. IntelliJ IDEA自用快捷键 转载

    最常用快捷键- 未分类 command Binding Description defeat - Ctrl+/ 代码提示 No - Ctrl+Alt+L 格式化代码   - Ctrl+B 快速打开光标 ...

  7. winform 菜单项显示历史记录 分类: WinForm 2014-07-11 18:15 196人阅读 评论(0) 收藏

    (1)创建一个项目,将其命名为MenuHistory,默认窗体为Form1. (2)从工具箱中向Form1窗体添加MenuStrip控件,同时向窗体添加OpenFileDialog控件.创建一个&qu ...

  8. qt中使用opencv处理图片 QImage 和 IplImage 相互之间转换问题

    在用opencv处理图片显示在qt label上的时候遇到不是问题 1. qt上要用qimage形式才干显示 IplImage转成 Qimage 彩色图像转换 IplImage  *fram; QIm ...

  9. HUNNU--湖师大--11409--Skill

    Skill Yasser is an Egyptian coach; he will be organizing a training camp in Jordan. At the end of ca ...

  10. cmake 手册系列

    http://www.cnblogs.com/coderfenghc/archive/2012/06/16/CMake_ch_01.html