今天在闲逛CSDN论坛时,看到一位博主写的一篇关于《Spring中IOC的Annotation的实现》的文章, 于是点击进去看了下, 发现在说明中对Spring配置文件中的有些配置节点模凌两可,表述的存在问题,于是自己在Demo中实现并实际操作了下,再次做个笔录。

我们一般在含有Spring的项目中,可能会看到配置项中包含这个配置节点<context:annotation-config>,这是一条向Spring容器中注册

AutowiredAnnotationBeanPostProcessor

CommonAnnotationBeanPostProcessor

PersistenceAnnotationBeanPostProcessor

RequiredAnnotationBeanPostProcessor

这4个BeanPostProcessor.注册这4个BeanPostProcessor的作用,就是为了你的系统能够识别相应的注解。

那么那些注释依赖这些Bean呢。

如果想使用@ Resource 、@ PostConstruct、@ PreDestroy等注解就必须声明CommonAnnotationBeanPostProcessor。
如果想使用@PersistenceContext注解,就必须声明PersistenceAnnotationBeanPostProcessor的Bean。
如果想使用@Autowired注解,那么就必须事先在 Spring 容器中声明 AutowiredAnnotationBeanPostProcessor Bean。
如果想使用 @Required的注解,就必须声明RequiredAnnotationBeanPostProcessor的Bean。

同样,传统的声明方式如下:

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

但是,就一般而言,这些注解我们是经常使用,比如Antowired,Resuource等注解,如果总是按照传统的方式一条一条的配置,感觉比较繁琐和机械。于是Spring给我们提供了<context:annotation-config/>的简化的配置方式,自动帮助你完成声明,并且还自动搜索@Component , @Controller , @Service , @Repository等标注的类。

<context:component-scan base-package="com.**.impl"/>

因此当使用 <context:component-scan/> 后,就可以将 <context:annotation-config/> 移除了。

Spring配置项<context:annotation-config>的解释说明的更多相关文章

  1. Spring配置项<context:annotation-config/>解释说明

    转自:https://blog.csdn.net/techbirds_bao/article/details/9241371 在基于主机方式配置Spring的配置文件中,你可能会见到<conte ...

  2. Spring配置项<context:annotation-config/>说明

    配置applicationContext.xml时经常会看到: <context:annotation-config/> 它的作用是隐式地向Spring容器注册AutowiredAnnot ...

  3. spring 2.5.6 错误:Context namespace element 'component-scan' and its parser class [org.springframework.context.annotation.ComponentScanBeanDefinitionParser] are only available on JDK 1.5 and higher

    在运行一个第三方公司交付的项目的时候, 出现: Caused by: java.lang.IllegalStateException: Context namespace element 'annot ...

  4. Spring配置之context:annotation与、component-scan以及annotation-driven

    spring boot帮助我们隐藏了大量的细节,有些配置spring boot都是开启的,因此当我们查看遗留项目使用spring时候遇见问题一定细心排查 <!-- context:annotat ...

  5. Spring框架context的注解管理方法之二 使用注解注入基本类型和对象属性 注解annotation和配置文件混合使用(半注解)

    首先还是xml的配置文件 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns=" ...

  6. Spring Cloud(Dalston.SR5)--Config 集群配置中心-刷新配置

    远程 SVN 服务器上面的配置修改后,需要通知客户端来改变配置,需要增加 spring-boot-starter-actuator 依赖并将 management.security.enabled 设 ...

  7. Spring Cloud Alibaba Nacos Config 实战

    Nacos 提供用于存储配置和其他元数据的 key/value 存储,为分布式系统中的外部化配置提供服务器端和客户端支持.使用 Spring Cloud Alibaba Nacos Config,您可 ...

  8. Spring AOP Schema aop:config、tx:advice

    Spring AOP Schema  aop:config.tx:advice 一.      利用aop:config标签实现AOP 首先看个例子,如下 接口代码: package com.lei. ...

  9. Mingyang.net:org.springframework.context.annotation.ConflictingBeanDefinitionException

    org.springframework.context.annotation.ConflictingBeanDefinitionException: Annotation-specified bean ...

随机推荐

  1. python panda库自动去重

    http://blog.csdn.net/xinxing__8185/article/details/48022401

  2. 2018.10.16 NOIP模拟 长者(主席树+hash)

    传送门 考试的时候开始sb的以为需要可持久化trietrietrie树,发现建树时空都是O(n2)O(n^2)O(n2)的. 然后发现由于每次只从原来的字符串改一个字符. 因此直接主席树维护区间has ...

  3. hdu-1141

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1141 参考文章:https://blog.csdn.net/fei____fei/article/de ...

  4. java获取变量的类型

    对于简单类型变量,是无法直接获得变量类型的://要想获取,必须自定义函数进行返回 对于包装类型变量,是可以直接获得的,变量名称.getClass().getName(); java中获取变量的类型可以 ...

  5. Java编程模板

    package Campus; import java.util.Scanner; public class Main{ public static void main(String args[]){ ...

  6. Hdu1978 How many ways 2017-01-18 14:32 40人阅读 评论(0) 收藏

    How many ways Time Limit : 3000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Total ...

  7. C++中的关键字用法--- explicit

    1. C++中的explicit C++提供了关键字explicit,可以阻止不应该允许的经过转换构造函数进行的隐式转换的发生.声明为explicit的构造函数不能在隐式转换中使用. C++中, 一个 ...

  8. 博客搬家到 http://leijun00.github.io

    博客园对markdown支持不太好,搬家到 http://leijun00.github.io

  9. hdu 5003 模拟水题

    http://acm.hdu.edu.cn/showproblem.php?pid=5003 记得排序后输出 #include <cstdio> #include <cstring& ...

  10. PHP 生成16 uuid

    从c# 翻译而来 <?php $i=1; $guidStr=string_make_guid(); echo $guidStr."\n"; $byte_array = unp ...