Spring配置加载的核心处理器(1)相关BeanDefinition的注册

编程

首先:
SpringApplication.createApplicationContext() 阶段: 

实例化 AnnotationConfigApplicationContext或 AnnotationConfigServletWebServerApplicationContext 的构造方法里:

创建了两个不同类型但都是向 org.springframework.beans.factory.support.BeanDefinitionRegistry (一般就是容器对象本身)里注册org.springframework.beans.factory.config.BeanDefinition的处理类

  1. 类地址扫描器 org.springframework.context.annotation.ClassPathBeanDefinitionScanner
  2. 注解式的类解析器 org.springframework.context.annotation.AnnotatedBeanDefinitionReader

AnnotationConfigApplicationContext 与 AnnotationConfigServletWebServerApplicationContext 的有共同父类:org.springframework.web.context.support.GenericWebApplicationContext

使用装饰模式来实现接口BeanDefinitionRegistry 。实际处理对象是 org.springframework.beans.factory.support.DefaultListableBeanFactory

其次:

AnnotatedBeanDefinitionReader的构造方法内:

方法org.springframework.context.annotation.AnnotationConfigUtils.registerAnnotationConfigProcessors  向容器中注册 配置式注解的处理类

  1. 对 @Autowired 、@Value 的处理: 
    名为“org.springframework.context.annotation.internalAutowiredAnnotationProcessor” 的 org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor
  2. 对 @Import、@ImportResource 、@Configuration 、@Bean、 org.springframework.context.annotation.ImportSelector org.springframework.context.annotation.ImportBeanDefinitionRegistrar 等的处理:
    名为“org.springframework.context.annotation.internalConfigurationAnnotationProcessor” 的 org.springframework.context.annotation.ConfigurationClassPostProcessor  (一个BeanFactoryPostProcessor
    核心方法: processConfigBeanDefinitions
  3. 对 @ PostConstruct、@ PreDestroy、@ Resource 等的处理:
    名为 “org.springframework.context.annotation.internalCommonAnnotationProcessor” 的  org.springframework.context.annotation.CommonAnnotationBeanPostProcessor

    内部类ResourceElement
  4. 对 @

    EventListener的处理:


    名为

    “org.springframework.context.event.internalEventListenerProcessor”  的org.springframework.context.event.

    EventListenerMethodProcessor


    名为

    "org.springframework.context.event.internalEventListenerFactory" 的 org.springframework.context.event.

    DefaultEventListenerFactory 


    (默认的org.springframework.context.

    ApplicationListener工程类,逻辑核心在: 

    org.springframework.context.event.

    ApplicationListenerMethodAdapter

    )

     

以上是 Spring配置加载的核心处理器(1)相关BeanDefinition的注册 的全部内容, 来源链接: utcz.com/z/516991.html

回到顶部