spring config

<context:annotation-config>

注册了如下的BeanPostProcessor

  1. AutowiredAnnotationBeanPostProcessor
    autowires annotated fields, setter methods and arbitrary config methods. Such members to be injected are detected through a Java 5 annotation: by default, Spring's @Autowired and @Value annotations.
    Also supports JSR-330's @Inject annotation, if available, as a direct alternative to Spring's own @Autowired

  2. CommonAnnotationBeanPostProcessor
    This post-processor includes support for the PostConstruct and PreDestroy
    annotations
    The central element is the Resource annotation for annotation-driven injection of named beans
    JSR250注解(默认byName,与@Autowire的和@Inject相对)

  3. PersistenceAnnotationBeanPostProcessor
    BeanPostProcessor that processes PersistenceUnit and PersistenceContext annotations, for injection of the corresponding JPA resources EntityManagerFactory and EntityManager.

  4. RequiredAnnotationBeanPostProcessor
    BeanPostProcessor implementation that enforces required JavaBean properties to have been configured. Required bean properties are detected through a Java 5 annotation: by default, Spring's Required
    annotation.

可以看出,这些Annotation都是用在BeanDefinition已经注册,在实例化Bean时用的,如设置属性,初始化,销毁等。

<context:component-scan>

componentScan是Annotation-config的超集,除了注册了以上的BeanPostProcessor以外,更主要的是它会扫描basePackage,然后根据注解注册BeanDefinition。
可以识别的注解包括了@Component, @Service, and @Controller, @Repository。
可以通过<context:include-filter><context:exclude-filter>来进一步定制。
具体的工作由ComponentScanBeanDefinitionParser或者ComponentScanAnnotationParser来完成。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容