zoukankan      html  css  js  c++  java
  • <context:annotationconfig/>

    <context:annotation-config/>

    他的作用是隐式地向 Spring 容器注册

    AutowiredAnnotationBeanPostProcessor、

    CommonAnnotationBeanPostProcessor、

    PersistenceAnnotationBeanPostProcessor、

    RequiredAnnotationBeanPostProcessor 

    这 4 个BeanPostProcessor。

    例如:

    如果想使用@ Resource 、@ PostConstruct、@ PreDestroy等注解就必须声明CommonAnnotationBeanPostProcessor。

    如果想使用@PersistenceContext注解,就必须声明PersistenceAnnotationBeanPostProcessor的Bean。

    如果你想使用@Autowired注解,那么就必须事先在 Spring 容器中声明 AutowiredAnnotationBeanPostProcessor Bean。传统声明方式如下:

    <bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor "/> 

    如果想使用 @Required的注解,就必须声明RequiredAnnotationBeanPostProcessor的Bean。同样,传统的声明方式如下:

    <bean class="org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor"/> 

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

     

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

     

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

     

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

          eg:  public void setUserDAO(@Qualifier("u") UserDAO userDAO) 

     

      @Autowired 可以对成员变量、方法以及构造函数进行注释,而 @Qualifier 的标注对象是成员变量、方法入参、构造函数入参。

    记得,使用注解一般都会配置扫描包路径选项

    <context:component-scan base-package=”XX.XX”/> 
  • 相关阅读:
    工业网络的物理隔离与数据采集
    从勒索软件到工控系统网络安全
    数据结构导论之第五章图
    数据结构导论之第六章查找表
    数据结构导论之第七章排序
    数据结构导论之第三章(栈、队列、数组)
    第八章、网络安全基础
    第七章、无线与移动网络
    第六章、物理层
    第五章、数据链路层与局域网
  • 原文地址:https://www.cnblogs.com/a892647300/p/2655667.html
Copyright © 2011-2022 走看看