在基于主机方式配置Spring的配置文件中,你可能会见到<context:annotation-config/>这样一条配置,他的作用是式地向 Spring 容器注册

AutowiredAnnotationBeanPostProcessor、CommonAnnotationBeanPostProcessor、

PersistenceAnnotationBeanPostProcessor 以及 RequiredAnnotationBeanPostProcessor 这 4 个BeanPostProcessor。

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

例如:

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

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

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

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

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

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

一般来说,这些注解我们还是比较常用,尤其是Antowired的注解,在自动注入的时候更是经常使用,所以如果总是需要按照传统的方式一条一条配置显得有些繁琐和没有必要,于是spring给我们提供<context:annotation-config/>的简化配置方式,自动帮你完成声明。

不过,呵呵,我们使用注解一般都会配置扫描包路径选项

<context:component-scan base-package=”XX.XX”/>

该配置项其实也包含了自动注入上述processor的功能,因此当使用 <context:component-scan/> 后,就可以将 <context:annotation-config/> 移除了。

Spring context:annotation-config/ 解说相关推荐

  1. Spring Security Java Config Preview--官方

    原文地址:[1]https://spring.io/blog/2013/07/02/spring-security-java-config-preview-introduction/ [2]https ...

  2. Arthas实践--获取到Spring Context,然后为所欲为

    背景 Arthas 是Alibaba开源的Java诊断工具,深受开发者喜爱. https://github.com/alibaba/arthas Arthas提供了非常丰富的关于调用拦截的命令,比如 ...

  3. Spring context:component-scan代替context:annotation-config

    Spring context:component-scan代替context:annotation-config XML: <?xml version="1.0" encod ...

  4. Spring Enable annotation – writing a custom Enable annotation

    原文地址:https://www.javacodegeeks.com/2015/04/spring-enable-annotation-writing-a-custom-enable-annotati ...

  5. Spring @Autowired Annotation教程

    Spring @Autowired Annotation教程 Spring @Autowired注释用于自动依赖注入.Spring框架是基于依赖注入构建的,我们通过spring bean配置文件注入类 ...

  6. Spring @Autowired Annotation

    Spring @Autowired Annotation Spring @Autowired注释用于自动依赖注入.Spring框架是基于依赖注入构建的,我们通过spring bean配置文件注入类依赖 ...

  7. 已解决org.springframework.context.annotation.ConflictingBeanDefinitionException异常的正确解决方法,亲测有效!!!

    已解决org.springframework.context.annotation.ConflictingBeanDefinitionException异常的正确解决方法,亲测有效!!! 文章目录 报 ...

  8. org.springframework.context.annotation.ConflictingBeanDefinitionException异常处理

    问题描述: 项目启动时,报了这个错: org.springframework.context.annotation.ConflictingBeanDefinitionException:标记为Bean ...

  9. 实践讲解Spring配置中心config(图+文,本地文件方式)

    1 缘起 微服务的学习过程中,发现了许多服务的配置是相同的,并且项目稳定运行期间不会轻易变更, 于是,自己开始做实验,将这些相同的配置提取出来放在配置中心, 各个服务需要时,通过这个配置中心获取,Sp ...

  10. org.springframework.context.annotation.AnnotationConfigApplicationContext has not been refreshed yet

    代码如下(为了演示这个报错): AnnotationConfigApplicationContext annotationConfigApplicationContext = new Annotati ...

最新文章

  1. Java多线程-Callable和Future
  2. 编译器在处理const变量跟一般变量时的区别
  3. 用Word写博客园文章
  4. python string转int_我用Python搞资源 [ 02 ]
  5. 3. PowerShell --基本操作,Alias,输出
  6. 通过c# 实现自定义属性改变触发自定义事件 ,理解自定义事件及其触发过程
  7. 【报告分享】抖音蓝V账号定位及外化罗盘.pdf(附下载链接)
  8. AI在中国,还没到抢切蛋糕的时候
  9. HUE与Oozie的集成
  10. Linux下载Java包,Linux环境Java包的安装和环境配置
  11. 【铨顺宏项目推荐】RFID无线射频识别技术的设计思路
  12. 电气领域相关数据集(目标检测,分类图像数据及负荷预测),输电线路图像数据
  13. 手机模拟器安装证书抓包
  14. buctoj2021年ACM竞赛班训练(四)全题解
  15. 父级fixed_position:fixed相对父级元素定位而不是浏览器
  16. android 常用URI
  17. 某猫电影 css 加密解决方案
  18. android pos机对接微信刷脸支付时如何做到双屏异显-2020年10月9日
  19. 幂级数和函数经典例题_函数项级数和幂级数 习题课
  20. SQL server 2012 下载,安装,磁力链接,下载地址 2020.11.28

热门文章

  1. 英语口语练习系列-C13-聚会
  2. windows下python subprocess.call使用ffmpeg的问题记录
  3. python 并行执行_python 串行执行和并行执行实例
  4. 计算机组成原理oe表示什么意思,计算机组成原理课后习题答案解析
  5. 未来科学技十幻想画计算机,未来世界科幻画图片 你憧憬过这样的未来吗
  6. SuperMap Objects组件式开发
  7. Vue实现图形化积木式编程(十一)
  8. 【网站架构】一招搞定90%的分布式事务,实打实介绍数据库事务、分布式事务的工作原理应用场景
  9. html盒模型中border的写法,【前端】盒子模型的边框样式属性和应用技巧讲解
  10. 【注入】C# 构造注入的方法