的作用


在傳統聲明方式中
類似這樣的注入IOC容器中

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

1.如果想使用@ Resource 、@ PostConstruct、@ PreDestroy等注解就必須聲明CommonAnnotationBeanPostProcessor。

2.如果想使用@PersistenceContext注解,就必須聲明PersistenceAnnotationBeanPostProcessor的Bean。

3.如果想使用@Autowired注解,那么就必須事先在 Spring 容器中聲明 AutowiredAnnotationBeanPostProcessor Bean。

4.如果想使用 @Required的注解,就必須聲明RequiredAnnotationBeanPostProcessor的Bean。

context:annotation-config

而使用context:annotation-config/ 就可以隱式地自動向Spring容器注冊4個BeanPostProcessor:

AutowiredAnnotationBeanPostProcessor
CommonAnnotationBeanPostProcessor
PersistenceAnnotationBeanPostProcessor
RequiredAnnotationBeanPostProcessor 

這樣就可以使用@ Resource 、@ PostConstruct、@ PreDestroy、@PersistenceContext、@Autowired、@Required等注解了,就可以實現自動注入
注冊這4個 BeanPostProcessor的作用,就是為了你的系統能夠識別相應的注解。

context:component-scan

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

Spring給我們提供了context:annotation-config 的簡化的配置方式,自動幫助你完成聲明,並且還自動搜索@Component , @Controller , @Service , @Repository等標注的類。

context:component-scan除了具有context:annotation-config的功能之外,context:component-scan還可以在指定的package下掃描以及注冊javabean 。還具有自動將帶有@component,@service,@Repository等注解的對象注冊到spring容器中的功能。

因此當使用 context:component-scan 后,就可以將 context:annotation-config移除。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM