转载:http://www.cnblogs.com/iuranus/archive/2012/07/19/2599084.html

当我们需要使用BeanPostProcessor时,直接在Spring配置文件中定义这些Bean显得比较笨拙,例如:
  使用@Autowired注解,必须事先在Spring容器中声明AutowiredAnnotationBeanPostProcessor的Bean:

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

  使用 @Required注解,就必须声明RequiredAnnotationBeanPostProcessor的Bean:

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

  类似地,使用@Resource、@PostConstruct、@PreDestroy等注解就必须声明 CommonAnnotationBeanPostProcessor;使用@PersistenceContext注解,就必须声明 PersistenceAnnotationBeanPostProcessor的Bean。
  这样的声明未免太不优雅,而Spring为我们提供了一种极为方便注册这些BeanPostProcessor的方式,即使用<context:annotation- config/>隐式地向 Spring容器注册AutowiredAnnotationBeanPostProcessor、RequiredAnnotationBeanPostProcessor、CommonAnnotationBeanPostProcessor以及PersistenceAnnotationBeanPostProcessor这4个BeanPostProcessor。如下:

<context:annotation-config/> 

  另,在我们使用注解时一般都会配置扫描包路径选项:

<context:component-scan base-package="pack.pack"/>

  该配置项其实也包含了自动注入上述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>

最新文章

  1. web服务器选择Apache还是Nginx
  2. HMC V7.9.0升级至SP2版本
  3. Unix 初步(一)
  4. 快速编译system.img、userdata.img、boot.img的方法
  5. Python 统计文本中单词的个数
  6. Chrome浏览器允许跨域请求配置
  7. error C2220: warning treated as error - no &#39;object&#39; file generated解决方法
  8. ios app开发步骤
  9. Find命令, find用法,
  10. ScrollView嵌套ListView只显示一行
  11. [Swift]LeetCode76. 最小覆盖子串 | Minimum Window Substring
  12. python 排序算法
  13. python 线程,进程28原则
  14. BN(Batch Normalization)
  15. 设计模式之组合模式(composite)
  16. SE-Net要点
  17. 2018/04/18 每日一学Linux 之 ssh关闭密码登录
  18. git移除文件夹的版本控制
  19. DPHARD
  20. 7-13 Power Calculus 快速幂计算 uva1374

热门文章

  1. ssd算法论文理解
  2. Android 启动界面的实现(转载)
  3. Disillusioning #1 水题+原题赛(被虐瞎)
  4. 【BZOJ】3315: [Usaco2013 Nov]Pogo-Cow(dp)
  5. linux less使用方法
  6. vue-cli 打包(npm run build) 出现 ERROR in xx..js from UglifyJs Unexpected token: punc (()
  7. MathType与Origin是怎么兼容的
  8. IPOL图像处理分析经典在线(文献+源码)
  9. 【转】VC++计算当前时间点间隔N天的时间(不使用CTimeSpan类)
  10. SQLAllocStmt与SQLFreeStmt