您的位置:首页 > 编程语言 > Java开发

spring <context:component-scan/>与<context:annotation-config/>

2014-07-27 13:42 489 查看
<context:annotation-config/>可以直接注册

AutowiredAnnotationBeanPostProcessor、CommonAnnotationBeanPostProcessor、PersistenceAnnotationBeanPostProcessor、

RequiredAnnotationBeanPostProcessor 这 4 个BeanPostProcessor。

使 用@Autowired注解,那么就必须事先在 Spring 容器中声明 AutowiredAnnotationBeanPostProcessor Bean。 

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

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

使 用 @PersistenceContext注解,就必须声明PersistenceAnnotationBeanPostProcessor。

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

   <bean class="org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor"/>
 
<context:component-scan/> 配置项不但启用了对类包进行扫描以实施注释驱动 Bean 定义的功能,同时还启用了注释驱动自动注入的功能(即还隐式地在内部注测了AutowiredAnnotationBeanPostProcessor 和 CommonAnnotationBeanPostProcessor。

当使用 <context:component-scan/> 后,若只用@Autowired@ Resource 、@ PostConstruct、@ PreDestroy,则可不注册<context:annotation-config/>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  注解 扫描 spring
相关文章推荐