@Autowired和@Resource等注解是将Spring容器中的bean注入到属性,而@Component等注解是将bean放入Spring容器中管理。

@Autowired

spring2.1中允许用户通过@Autowired注解对Bean的属性变量.属性Setter方法以及构造函数进行标注,配合AutowiredAnnotationBeanProcessor完成Bean的自动配置。使用@Autowired注释进行byType注入。

在applicationContext.xml中加入:



[xml] view plain copy


1. <!-- 该 BeanPostProcessor 将自动对标注 @Autowired 的 Bean 进行注入 -->  
2. <bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>

通过 @Autowired的使用来消除 set ,get方法。



[xml] view plain copy


1. @Autowired  
2. private UserDao userdao;

这样就可以删除set ,get方法和spring中的相关配制了。



[java] view plain copy


1. <bean id="userDao" class="..."/>  
2. <bean id="userService" class="...">  
3. "userDao">  
4. "userDao"/>  
5.     </property>  
6. </bean>

通过@Autowired属的Setter方法给父类中的属性注入值。



[java] view plain copy



    1. @Autowired  
    2. public void setDataSource(DataSource dataSource)  
    3. {  
    4. super.setDataSource(dataSource);  
    5. }



    @Autowired(required = false)

    当不能确定 Spring 容器中一定拥有某个类的 Bean 时,可以在需要自动注入该类 Bean 的地方可以使用 @Autowired(required = false) ,这等于告诉 Spring:在找不到匹配 Bean 时也不报错。

    当然,一般情况下,使用 @Autowired 的地方都是需要注入 Bean 的,使用了自动注入而又允许不注入的情况一般仅会在开发期或测试期碰到(如为了快速启动 Spring 容器,仅引入一些模块的 Spring 配置文件),所以 @Autowired(required = false) 会很少用到。

    @Qualifier

    使用@Autowired注释进行byType注入,如果需要byName(byName就是通过id去标识)注入,增加@Qualifier注释。一般在候选Bean数目不为1时应该加@Qualifier注释。

    在默认情况下使用 @Autowired 注释进行自动注入时,Spring 容器中匹配的候选 Bean 数目必须有且仅有一个。当找不到一个匹配的 Bean 时,Spring 容器将抛出

    BeanCreationException 异常,并指出必须至少拥有一个匹配的 Bean。

    和找不到一个类型匹配 Bean 相反的一个错误是:如果 Spring 容器中拥有多个候选 Bean,Spring 容器在启动时也会抛出 BeanCreationException 异常。

    Spring 允许我们通过 @Qualifier 注释指定注入 Bean 的名称,这样歧义就消除了,可以通过下面的方法解决异常:



    [java] view plain copy


    1. @Autowired  
    2. public void setOffice(@Qualifier("office")Office office)  
    3. {  
    4. this.office =office;  
    5. }


    也可以直接注入到属性:



    [java] view plain copy



      1. @Autowired   
      2. @Qualifier("office")   
      3. private Office office;


      @Qualifier(“office”)中的office是Bean的名称,所以@Autowired和@Qualifier结合使用时,自动注入的策略就从byType转变成byName了。

      @Autowired可以对成员变量、方法以及构造函数进行注释,而@Qualifier的标注对象是成员变量、方法入参、构造函数入参。正是由于注释对象的不同,所以Spring不将 @Autowired和@Qualifier统一成一个注释类。

      @Qualifier 只能和@Autowired 结合使用,是对@Autowired有益的补充。

      一般来讲,@Qualifier对方法签名中入参进行注释会降低代码的可读性,而对成员变量注释则相对好一些。

      @Resource

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

      @Resource的作用相当于@Autowired,只不过@Autowired按byType自动注入,而@Resource默认按 byName自动注入罢了。@Resource有两个属性是比较重要的,分是name和type,Spring将@Resource注解的name属性解析为bean的名字,而type属性则解析为bean的类型。所以如果使用name属性,则使用byName的自动注入策略,而使用type属性时则使用byType自动注入策略。如果既不指定name也不指定type属性,这时将通过反射机制使用byName自动注入策略。

      @Resource装配顺序

      1. 如果同时指定了name和type,则从Spring上下文中找到唯一匹配的bean进行装配,找不到则抛出异常

      2. 如果指定了name,则从上下文中查找名称(id)匹配的bean进行装配,找不到则抛出异常

      3. 如果指定了type,则从上下文中找到类型匹配的唯一bean进行装配,找不到或者找到多个,都会抛出异常

      4. 如果既没有指定name,又没有指定type,则自动按照byName方式进行装配;如果没有匹配,则回退为一个原始类型进行匹配,如果匹配则自动装配;

      @Component、@Repository、@Service、@Controller

      Spring 2.5 中除了提供 @Component 注释外,还定义了几个拥有特殊语义的注释,它们分别是:@Repository、@Service 和 @Controller。

      在目前的 Spring 版本中,这 3 个注释和 @Component 是等效的,但是从注释类的命名上,很容易看出这 3 个注释分别和持久层、业务层和控制层(Web 层)相对应。

      虽然目前这3 个注释和 @Component 相比没有什么新意,但 Spring 将在以后的版本中为它们添加特殊的功能。

      所以,如果 Web 应用程序采用了经典的三层分层结构的话,最好在持久层、业务层和控制层分别采用上述注解对分层中的类进行注释。

      @Service用于标注业务层组件

      @Controller用于标注控制层组件(如struts中的action)

      @Repository用于标注数据访问组件,即DAO组件

      @Component泛指组件,当组件不好归类的时候,我们可以使用这个注解进行标注。



      [java] view plain copy

      1. @Service  
      2. public class VentorServiceImpl implements iVentorService {     
      3. }  
      4. @Repository  
      5. public class VentorDaoImpl implements iVentorDao {   
      6. }


      在一个稍大的项目中,如果组件采用xml的bean定义来配置,显然会增加配置文件的体积,查找以及维护起来也不太方便。

      Spring2.5为我们引入了组件自动扫描机制,他在类路径下寻找标注了上述注解的类,并把这些类纳入进spring容器中管理。

      它的作用和在xml文件中使用bean节点配置组件时一样的。要使用自动扫描机制,我们需要打开以下配置信息:



      [xml] view plain copy



        1. <?xml version="1.0" encoding="UTF-8" ?>   
        2. <beans xmlns="http://www.springframework.org/schema/beans"  
        3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
        4. xmlns:context="http://www.springframework.org/schema/context"  
        5. xsi:schemaLocation="http://www.springframework.org/schema/beans  
        6.   
        7. http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  
        8.   
        9.   
        10. http://www.springframework.org/schema/context  
        11.   
        12. >   
        13.   
        14. <context:annotation-config />  
        15. <context:component-scan base-package=”com.iteedu.spring”>     
        16. </beans>



        annotation-config是对标记了@Required、@Autowired、@PostConstruct(实例化bean后执行的方法(init()))、@PreDestroy(关闭资源后执行的方法( destroy()))、@Resource、@WebServiceRef、@EJB、@PersistenceContext、@PersistenceUnit等注解的类进行对应的操作使注解生效。

        base-package为需要扫描的包(含所有子包),负责扫描那些有@Component、@Repository、@Service 和 @Controller注解的类。

        可以使用以下方式指定初始化方法和销毁方法:



        [java] view plain copy


        1. @PostConstruct  
        2. public void init() {   
        3. }   
        4. @PreDestroy  
        5. public void destory() {   
        6. }