跟
<context:annotation-config/>和<context:component-scan/>
在配置文件中加上这个配置后,就会在spring中注册下面4个BeanPostProcessor
1. AutowiredAnnotationBeanPostProcessor
2. CommonAnnotationBeanPostProcessor
3. PersistenceAnnotationBeanPostProcessor
4. RequiredAnnotationBeanPostProcessor
为什么要注册这些BeanPostProcessor
1. 如果要使用@Autowired,就要实现在容器中注册 AutowiredAnnotationBeanPostProcessor
2. 如果想使用@ Resource 、@ PostConstruct、@ PreDestroy等注解就必须注册 CommonAnnotationBeanPostProcessor
3. 如果想使用@PersistenceContext注解,就必须注册PersistenceAnnotationBeanPostProcessor
4. 如果想使用 @Required的注解,就必须声明RequiredAnnotationBeanPostProcessor
当然,可以用下面这种方式注册
<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor "/>
这种方式的优点是单独明确,缺点是每个使用都要注册一次
因为这些注释通常都会用到,所以直接用<context:annotation-config/>,自动帮你注册了这些,更加方便开发
还有个不需要<context:annotation-config/>的方法
一般我们要用<context:component-scan base-package=”XX.XX”/> 来扫描包,有了该配置就能自动注入上面的几个BeanPostProcessor,所以加上这个扫描包的配置后,就能把<context:annotation-config/>去掉了。