在基于主机方式配置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/> 移除了。

备注:
在配置文件中使用 context 命名空间之前,必须在 <beans> 元素中声明 context 命名空间。

复制代码

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
...
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
...
<context:annotation-config/>
</beans>

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. 物体检测中的Objectness是什么?
  2. 新兴机器学习算法:在线学习
  3. html遮罩实例,给原生html中添加水印遮罩层的实现示例
  4. python条形图颜色设置_python – 根据值在matplotlib中更改3D条形图中的条形颜色
  5. freebsd 6.2 安装配置笔记[转]
  6. 电脑操作精典密笈60式
  7. 中考计算机考试exce,中考信息技术EXCEL操作.doc
  8. 【Python实例第33讲】单变量特征选择
  9. jetty 找不到html页面,记一次jetty 404问题排查修复
  10. 好用的局域网共享工具有哪些?win10系统如何设置?
  11. Unity打包APK多语言包名的适配
  12. excel VBA会说话的工作表
  13. 如何管理网络营销渠道冲突?
  14. 坐标中国|中国速度,挑战极限驱动发展“快车”
  15. EXPDP命令行选项
  16. Python实现网页自动化-浏览器查找元素(二)
  17. Python爬虫教程——入门一之爬虫基础了解
  18. sql语句进阶教程(学习sql这一篇就够了)
  19. 机器人产业化给导电环行业带来的机遇
  20. 计算机没有开启还原的功能,win10系统无法开启系统还原功能的详细教程

热门文章

  1. Android——列表选择框(Spinner)
  2. emblog博客打开显示 数据库密码错误,请返回主页的解决办法!
  3. 游戏组件——Game类
  4. SpringData-JPA
  5. Seek and Destroy
  6. 个人作业week7——前端开发感想总结
  7. JAVA线程池shutdown和shutdownNow的区别
  8. 【第40题】2019年OCP认证12C题库062考试最新考试原题
  9. ListenalbeFuture的使用总结
  10. Bfs 逃脱(牛客网)