首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

Spring@Autowired引文与自动装配

2013-08-06 
Spring@Autowired注解与自动装配1配置文件的方法 我们编写spring 框架的代码时候。一直遵循是这样一个规则:

Spring@Autowired注解与自动装配
1   配置文件的方法

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

Boss 拥有 Office 和 Car 类型的两个属性:  


清单 3. Boss.java

[java] view plaincopy
package com.baobaotao;      
      
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:

清单 4. beans.xml 将以上三个类配置成 Bean 

[html] view plaincopy
<?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" ref="car"/>    
        <property name="office" ref="office" />    
    </bean>    
    <bean id="office" value="002"/>    
    </bean>    
    <bean id="car" scope="singleton">    
        <property name="brand" value=" 红旗 CA72"/>    
        <property name="price" value="2000"/>    
    </bean>    
</beans>    

当我们运行以下代码时,控制台将正确打出 boss 的信息:  
清单 5. 测试类:AnnoIoCTest.java  
[java] view plaincopy
import org.springframework.context.ApplicationContext;    
import org.springframework.context.support.ClassPathXmlApplicationContext;    
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中加入:

[html] view plaincopy
<!-- 该 BeanPostProcessor 将自动对标注 @Autowired 的 Bean 进行注入 -->    
  <bean 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 value="001"/>    
    </bean>    
    <bean id="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 中删除。

热点排行