Spring框架中Spring配置文件中context:annotation-config標籤說明

<context:annotation-config/>此標籤的重要作用就是:

  省去系統繁瑣的註解標籤,加上一個此標籤,就可以在此項目程序添加“註解”的功能,使系統識別相應的註解功能!!

詳解:

<context:annotation-config/>標籤的作用是隱式地向 Spring 容器註冊AutowiredAnnotationBeanPostProcessor、

CommonAnnotationBeanPostProcessor、

PersistenceAnnotationBeanPostProcessor 以及 RequiredAnnotationBeanPostProcessor 這4個BeanPostProcessor。

註冊這4個 BeanPostProcessor的作用,就是爲了讓系統能識別相應的註解。

比如系統繁瑣的註解功能:

1:如果使用@Autowired註解:

必須事先在 Spring 容器中聲明 AutowiredAnnotationBeanPostProcessor Bean。傳統聲明方式如下

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

2:如果想使用@ Resource 、@ PostConstruct、@ PreDestroy等註解就必須聲明CommonAnnotationBeanPostProcessor

3:想使用@PersistenceContext註解,就必須聲明PersistenceAnnotationBeanPostProcessor的Bean。

4:想使用 @Required的註解,就必須聲明RequiredAnnotationBeanPostProcessor的Bean。

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

總結:這些註解我們還是比較常用,尤其是Antowired的註解,在自動注入的時候更是經常使用,所以如果總是需要按照傳統的方式一條一條配置顯得有些繁瑣和沒有必要,於是spring給我們提供的簡化配置方式,自動幫你完成聲明。-----即用<context:annotation-config/>標籤即可;

 

附加:使用註解一般還要用到掃描包路徑的選項:<context:component-scan base-package=”XX.XX”/>

發佈了2 篇原創文章 · 獲贊 1 · 訪問量 6226
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章