Spring 注解注入详解

1.@Autowired注解(不推荐使用,建议使用@Resource) 
     @Autowired能够对成员变量、方法和构造函数进行标注,来完成自动装配的工做。@Autowired的标注位置不一样,它们都会在Spring在初始化这个bean时,自动装配这个属性。要使@Autowired可以工做,还须要在配置文件中加入如下
web

<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor" />正则表达式

2. @Qualifier注解 
    @Autowired是根据类型进行自动装配的。例如,若是当Spring上下文中存在不止一个UserDao类型的bean时,就会抛出 BeanCreationException异常;若是Spring上下文中不存在UserDao类型的bean,也会抛出 BeanCreationException异常。咱们可使用@Qualifier配合@Autowired来解决这些问题。以下: 
   1). 可能存在多个UserDao实例
spring

@Autowired  express

@Qualifier("userServiceImpl")  session

public IUserService userService;函数

或者ui

@Autowired    this

public void setUserDao(@Qualifier("userDao") UserDao userDao) {     spa

this.userDao = userDao;     component

}

  这样,Spring会找到id为userServiceImpl和userDao的bean进行装配。
   2). 可能不存在UserDao实例

@Autowired(required = false)     

public IUserService userService; 

3. @Resource注解 
   JSR-250标准注解,推荐使用它来代替Spring专有的@Autowired注解。@Resource的做用至关于@Autowired,只不过 @Autowired按byType自动注入,而@Resource默认按byName自动注入罢了。@Resource有两个属性是比较重要的,分别是 name和type,Spring将 @Resource注解的name属性解析为bean的名字,而type属性则解析为bean的类型。因此若是使用name属性,则使用byName的自 动注入策略,而使用type属性时则使用byType自动注入策略。若是既不指定name也不指定type属性,这时将经过反射机制使用byName自动 注入策略。要使@Autowired可以工做,还须要在配置文件中加入如下:
<bean class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor" />

@Resource装配顺序: 
    a.若是同时指定了name和type,则从Spring上下文中找到惟一匹配的bean进行装配,找不到则抛出异常 
    b.若是指定了name,则从上下文中查找名称(id)匹配的bean进行装配,找不到则抛出异常 
    c.若是指定了type,则从上下文中找到类型匹配的惟一bean进行装配,找不到或者找到多个,都会抛出异常 
    d.若是既没有指定name,又没有指定type,则自动按照byName方式进行装配(见2);若是没有匹配,则回退为一个原始类型(UserDao)进行匹配,若是匹配则自动装配; 

4. @PostConstruct(JSR-250)注解 
    在方法上加上注解@PostConstruct,这个方法就会在Bean初始化以后被Spring容器执行(注:Bean初始化包括,实例化Bean,并 装配Bean的属性(依赖注入))。它的一个典型的应用场景是,当你须要往Bean里注入一个其父类中定义的属性,而你又没法复写父类的属性或属性的 setter方法时,如:

public class UserDaoImpl extends HibernateDaoSupport implements UserDao {     

   private SessionFactory mySessionFacotry;     

   @Resource  

  public void setMySessionFacotry(SessionFactory sessionFacotry)  {     

       this.mySessionFacotry = sessionFacotry;     

   }     

    @PostConstruct    

    public void injectSessionFactory()    {     

        super.setSessionFactory(mySessionFacotry);     

   }  

}

这里经过@PostConstruct,为UserDaoImpl的父类里定义的一个sessionFactory私有属性,注入了咱们本身定义的 sessionFactory(父类的setSessionFactory方法为final,不可复写),以后咱们就能够经过调用 super.getSessionFactory()来访问该属性了。 

5. @PreDestroy(JSR-250)注解 
    在方法上加上注解@PreDestroy,这个方法就会在Bean初始化以后被Spring容器执行。其用法同@PostConstruct。和 @PostConstruct 区别在于:@PostConstruct注释的方法将在类实例化后调用,而标注了 @PreDestroy 的方法将在类销毁以前调用。 

6. @Component注解(不推荐使用) 
    只须要在对应的类上加上一个@Component注解,就将该类定义为一个Bean了。Spring还提供了更加细化的注解形 式:@Repository、@Service、@Controller,它们分别对应存储层Bean,业务层Bean,和展现层Bean。目前版本 (2.5)中,这些注解与@Component的语义是同样的,彻底通用,在Spring之后的版本中可能会给它们追加更多的语义。因此,咱们推荐使用 @Repository、@Service、@Controller来替代@Component。 

7.@Scope注解 
    在使用XML定义Bean时,咱们可能还须要经过bean的scope属性来定义一个Bean的做用范围,咱们一样能够经过@Scope注解来完成这项工做:

@Scope("session")     

@Component()     

public class UserSessionBean implements Serializable{   

 ... ...  

}

2、配置启用注解(注意如下配置须要使用spring2.5的头文件,在spring3.0中不适用) 
1.使用<context:annotation-config />简化配置 
      Spring2.1添加了一个新的context的Schema命名空间,该命名空间对注释驱动、属性文件引入、加载期织入等功能提供了便捷的配置。咱们 知道注释自己是不会作任何事情的,它仅提供元数据信息。要使元数据信息真正起做用,必须让负责处理这些元数据的处理器工做起来。 
    AutowiredAnnotationBeanPostProcessor和CommonAnnotationBeanPostProcessor就是 处理这些注释元数据的处理器。可是直接在Spring配置文件中定义这些Bean显得比较笨拙。Spring为咱们提供了一种方便的注册这些 BeanPostProcessor的方式,这就是<context:annotation-config />,如下是spring的配置。

<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-2.5.xsd     

    http://www.springframework.org/schema/context     

   http://www.springframework.org/schema/context/spring-context-2.5.xsd">     

   <context:annotation-config />     

</beans>

context:annotation-config />将隐式地向Spring容器注册了 
AutowiredAnnotationBeanPostProcessor、 
CommonAnnotationBeanPostProcessor、 
PersistenceAnnotationBeanPostProcessor 
RequiredAnnotationBeanPostProcessor
 
这4个BeanPostProcessor。 

2.使用<context:component-scan />让Bean定义注解工做起来

<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-2.5.xsd     

    http://www.springframework.org/schema/context     

    http://www.springframework.org/schema/context/spring-context-2.5.xsd">     

   <context:component-scan base-package="com.kedacom.ksoa" />     

</beans>  

这里,全部经过<bean>元素定义Bean的配置内容已经被移除,仅须要添加一行<context:component-scan />配置就解决全部问题了——Spring XML配置文件获得了极致的简化(固然配置元数据仍是须要的,只不过以注释形式存在罢了)。<context:component-scan />的base-package属性指定了须要扫描的类包,类包及其递归子包中全部的类都会被处理。 
<context:component-scan />还容许定义过滤器将基包下的某些类归入或排除。Spring支持如下4种类型的过滤方式:
过滤器类型 | 表达式范例 | 说明 
注解 | org.example.SomeAnnotation | 将全部使用SomeAnnotation注解的类过滤出来 
类名指定 | org.example.SomeClass | 过滤指定的类 
正则表达式 | com\.kedacom\.spring\.annotation\.web\..* | 经过正则表达式过滤一些类 
AspectJ表达式 | org.example..*Service+ | 经过AspectJ表达式过滤一些类 

以正则表达式为例,我列举一个应用实例:

<context:component-scan base-package="com.casheen.spring.annotation">     

     <context:exclude-filter type="regex" expression="com\.casheen\.spring\.annotation\.web\..*" />     

</context:component-scan>

值得注意的是<context:component-scan />配置项不但启用了对类包进行扫描以实施注释驱动Bean定义的功能,同时还启用了注释驱动自动注入的功能(即还隐式地在内部注册了 AutowiredAnnotationBeanPostProcessor和CommonAnnotationBeanPostProcessor), 所以当使用<context:component-scan />后,就能够将<context:annotation-config />移除了。 


3.<tx:annotation-driven/> 
   <context:annotation-config />是不支持spring的@Transcation和EJB的Spring's @Transactional or EJB3's @TransactionAttribute annotation。用此配置能够达到目的。

4. 使用@Scope来定义Bean的做用范围 
在使用XML定义Bean时,咱们可能还须要经过bean的scope属性来定义一个Bean的做用范围,咱们一样能够经过@Scope注解来完成这项工做:

@Scope("session")     

@Component()     

public class UserSessionBean implements Serializable {     

    ...     

相关文章
相关标签/搜索