1. 程式人生 > >在Spring 中元素的作用

在Spring 中元素的作用

一、介紹

spring的配置檔案中常包含如下元素:
<context:annotation-config/>
此元素的作用是向spring容器中註冊:

註冊這四個BeanPostProcessor的作用是使相應的註解起作用


 

1.AutowiredAnnotationBeanPostProcessor

2.CommonAnnotationBeanPostProcessor

3.PersistenceAnnotationBeanPostProcessor

4.RequiredAnnotationBeanPostProcessor 

如果你想使用

@Autowired註解,那麼就必須事先在 Spring 容器中宣告 AutowiredAnnotationBeanPostProcessor Bean。傳統宣告方式如下

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

如果想使用@ Resource @ PostConstruct@ PreDestroy等註解就必須宣告CommonAnnotationBeanPostProcessor

如果想使用@PersistenceContext註解,就必須宣告PersistenceAnnotationBeanPostProcessorBean

如果想使用 @Required的註解,就必須宣告RequiredAnnotationBeanPostProcessorBean。同樣,傳統的宣告方式如下:

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

  不過,呵呵,我們使用註解一般都會配置

掃描包路徑選項

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

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