以前看文章说context:component-scan能够替代context:annotation-config,可是一直不知道为何.最近查阅资料才知道为何,作一个总结。ui
首先context:annotation-config能够使四类注解生效,缘由是隐式注册这四个bean.net
1.@autowired,须要声明 AutowiredAnnotationBeanPostProcessor这个Beancomponent
2. @Resource 、@PostConstruct、@PreDestroy等,须要声明CommonAnnotationBeanPostProcessor这个Bean对象
3. @PersistenceContext,须要声明PersistenceAnnotationBeanPostProcessor这个Beanget
4. @Required的注解,须要声明RequiredAnnotationBeanPostProcessor这个Beanit
可是有一个问题,若是一个类没有注册,那么@autowired是不会帮助注册,那么就得不到你须要用到的实例.io
这时候,须要引入context:component-scan.class
首先能支持context:annotation-config所支持的注解,其次还支持@component、@Repository、@controller、@RestController、@ControllerAdvice、@Configuration。sso
最最重要的是扫描base-package,将扫描到的对象注册对应的bean.总结