Spring context:annotation-config/ 解说
在基于主机方式配置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/ 解说相关推荐
- Spring Security Java Config Preview--官方
原文地址:[1]https://spring.io/blog/2013/07/02/spring-security-java-config-preview-introduction/ [2]https ...
- Arthas实践--获取到Spring Context,然后为所欲为
背景 Arthas 是Alibaba开源的Java诊断工具,深受开发者喜爱. https://github.com/alibaba/arthas Arthas提供了非常丰富的关于调用拦截的命令,比如 ...
- Spring context:component-scan代替context:annotation-config
Spring context:component-scan代替context:annotation-config XML: <?xml version="1.0" encod ...
- Spring Enable annotation – writing a custom Enable annotation
原文地址:https://www.javacodegeeks.com/2015/04/spring-enable-annotation-writing-a-custom-enable-annotati ...
- Spring @Autowired Annotation教程
Spring @Autowired Annotation教程 Spring @Autowired注释用于自动依赖注入.Spring框架是基于依赖注入构建的,我们通过spring bean配置文件注入类 ...
- Spring @Autowired Annotation
Spring @Autowired Annotation Spring @Autowired注释用于自动依赖注入.Spring框架是基于依赖注入构建的,我们通过spring bean配置文件注入类依赖 ...
- 已解决org.springframework.context.annotation.ConflictingBeanDefinitionException异常的正确解决方法,亲测有效!!!
已解决org.springframework.context.annotation.ConflictingBeanDefinitionException异常的正确解决方法,亲测有效!!! 文章目录 报 ...
- org.springframework.context.annotation.ConflictingBeanDefinitionException异常处理
问题描述: 项目启动时,报了这个错: org.springframework.context.annotation.ConflictingBeanDefinitionException:标记为Bean ...
- 实践讲解Spring配置中心config(图+文,本地文件方式)
1 缘起 微服务的学习过程中,发现了许多服务的配置是相同的,并且项目稳定运行期间不会轻易变更, 于是,自己开始做实验,将这些相同的配置提取出来放在配置中心, 各个服务需要时,通过这个配置中心获取,Sp ...
- org.springframework.context.annotation.AnnotationConfigApplicationContext has not been refreshed yet
代码如下(为了演示这个报错): AnnotationConfigApplicationContext annotationConfigApplicationContext = new Annotati ...
最新文章
- Android Studio查看Gradle版本
- HQL中出现XXX is not mapped的错误
- 未来流媒体工作流的核心技术
- 电路基本概念有哪些?
- 盛严谨,严谨,再严谨。_评估员工调查的统计严谨性
- 按季度分类汇总_2019年纯碱行业相关上市公司季报 与半年报情况汇总
- python使用默认参数
- VSCode使用记录一:ubuntu 16.04下安装、编译文件、制作桌面图标和卸载
- 小鬼授权系统源码全解密源码 附授权代码
- arping指令linux,arping
- 最新列表!国内外核心期刊数据库收录范围汇总介绍
- 入门算法题——数学篇(一)
- 51单片机模拟I2C读写M24LR04
- php如何数据库配置文件,php数据库配置文件一般做法分享
- 无路可逃java攻略_孤岛惊魂5
- matlab绘制抛物线,MATLAB中绘制抛物线的图像,请补充完成下面代码: clc,clear; x=linspace(...
- 一文看懂:华为管理内控体系,到底厉害在哪里
- linux修改证书通用名,linux – 证书通用名称`* .c.ssl.fastly.net’与请求的主机名不匹配...
- 景联文科技:深度了解语音识别之发音词典及语音数据采集标注
- 对于“int a[] = new int[3];”,下列哪个叙述是错误的?