Spring配置項解釋說明

在基於主機方式配置Spring的配置文件中,你可能會見到<context:annotation-config/>這樣一條配置,他的做用是式地向 Spring 容器註冊 spring

AutowiredAnnotationBeanPostProcessorCommonAnnotationBeanPostProcessor ui

PersistenceAnnotationBeanPostProcessor 以及 RequiredAnnotationBeanPostProcessor  4 BeanPostProcessor spa

註冊這4 BeanPostProcessor的做用,就是爲了你的系統可以識別相應的註解。 code

例如: component

若是你想使用@Autowired註解,那麼就必須事先在 Spring 容器中聲明 AutowiredAnnotationBeanPostProcessor Bean。傳統聲明方式以下: io

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

若是想使用@ Resource @ PostConstruct@ PreDestroy等註解就必須聲明CommonAnnotationBeanPostProcessor class

若是想使用@PersistenceContext註解,就必須聲明PersistenceAnnotationBeanPostProcessorBean 容器

若是想使用 @Required的註解,就必須聲明RequiredAnnotationBeanPostProcessorBean。一樣,傳統的聲明方式以下: 配置

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

通常來講,這些註解咱們仍是比較經常使用,尤爲是Antowired的註解,在自動注入的時候更是常常使用,因此若是老是須要按照傳統的方式一條一條配置顯得有些繁瑣和沒有必要,因而spring給咱們提供<context:annotation-config/>的簡化配置方式,自動幫你完成聲明。 sso

   不過,呵呵,咱們使用註解通常都會配置掃描包路徑選項

 
  1. <context:component-scan base-package=」XX.XX」/> 

    該配置項其實也包含了自動注入上述processor的功能,所以當使用 <context:component-scan/> 後,就能夠將 <context:annotation-config/> 移除了。

相關文章
相關標籤/搜索