- <context:annotation-config/>
例如:
- 如果想使用@Autowired註解,需要在Spring容器中聲明AutowiredAnnotationBeanPostProcessor Bean。傳統的聲明方式:<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>
- 如果想使用@PersistenceContext註解,需要在Spring容器中聲明PersistenceAnnotationBeanPostProcessor Bean。傳統的聲明:<bean class="org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor"/>
- 如果想使用@Required註解,需要在Spring容器中聲明RequiredAnnotationBeanPostProcessor Bean。傳統聲明方式: <bean class="org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor"/>
- 如果想使用@Resource、@ PostConstruct、@ PreDestroy等註解就必須聲明CommonAnnotationBeanPostProcessor。傳統申明方式: <bean class="org.springframework.beans.factory.annotation.CommonAnnotationBeanPostProcessor"/>
因此如果在Spring的配置文件中事先加上<context:annotation-config/>這樣一條配置的話,那麼所有註解的傳統聲明就可以被 忽略,即不用在寫傳統的聲明,Spring會自動完成聲明。
-
<context:component-scan base-package="com.xx" />
值得注意的是<context:component-scan/>不但啓用了對類包進行掃描以實施註釋驅動 Bean 定義的功能,同時還啓用了註釋驅動自動注入的功能(即還隱式地在內部註冊了 AutowiredAnnotationBeanPostProcessor 和 CommonAnnotationBeanPostProcessor),因此當使用 <context:component-scan/> 後,就可以將 <context:annotation-config/> 移除了。