Spring配置项的解释说明

今天在闲逛CSDN论坛时,看到一位博主写的一篇关于《Spring中IOC的Annotation的实现》的文章, 因而点击进去看了下, 发如今说明中对Spring配置文件中的有些配置节点模凌两可,表述的存在问题,因而本身在Demo中实现并实际操做了下,再次作个笔录。html

咱们通常在含有Spring的项目中,可能会看到配置项中包含这个配置节点<context:annotation-config>,这是一条向Spring容器中注册spring

AutowiredAnnotationBeanPostProcessorui

CommonAnnotationBeanPostProcessorspa

PersistenceAnnotationBeanPostProcessorcode

RequiredAnnotationBeanPostProcessor component

这4个BeanPostProcessor.注册这4个BeanPostProcessor的做用,就是为了你的系统可以识别相应的注解。htm

那么那些注释依赖这些Bean呢。 blog

若是想使用@ Resource 、@ PostConstruct、@ PreDestroy等注解就必须声明CommonAnnotationBeanPostProcessor。
若是想使用@PersistenceContext注解,就必须声明PersistenceAnnotationBeanPostProcessor的Bean。
若是想使用@Autowired注解,那么就必须事先在 Spring 容器中声明 AutowiredAnnotationBeanPostProcessor Bean。
若是想使用 @Required的注解,就必须声明RequiredAnnotationBeanPostProcessor的Bean。rem

一样,传统的声明方式以下: rpc

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

 可是,就通常而言,这些注解咱们是常常使用,好比Antowired,Resuource等注解,若是老是按照传统的方式一条一条的配置,感受比较繁琐和机械。因而Spring给咱们提供了<context:annotation-config/>的简化的配置方式,自动帮助你完成声明,而且还自动搜索@Component , @Controller , @Service , @Repository等标注的类。

<context:component-scan base-package="com.**.impl"/>

所以当使用 <context:component-scan/> 后,就能够将 <context:annotation-config/> 移除了。

相关文章
相关标签/搜索