在spring初始化的过程中会注册六个非常重要的beandefinition,他们的名字分别是:
1.org.springframework.context.annotation.internalConfigurationAnnotationProcessor
2.org.springframework.context.annotation.internalAutowiredAnnotationProcessor
3.org.springframework.context.annotation.internalRequiredAnnotationProcessor
4.org.springframework.context.annotation.internalCommonAnnotationProcessor
5.org.springframework.context.event.internalEventListenerFactory
6.org.springframework.context.event.internalEventListenerProcessor
这六个名字他们分别对应了六个后置类他们分别是:
1.ConfigurationClassPostProcessor
2.AutowiredAnnotationBeanPostProcessor
3.RequiredAnnotationBeanPostProcessor
4.CommonAnnotationBeanPostProcessor
5.EventListenerMethodProcessor
6.DefaultEventListenerFactory
每个类在spring容器启动的过程中都起着至关重要的作用
(1)ConfigurationClassPostProcessor在之前的文章中提到过,他的作用就是扫描所有的类然后放入spring容器中;
(2)AutowiredAnnotationBeanPostProcessor的作用就是解析所有的@Autwried然后在给早期对象填充属性的时候会去使用它;
(3)RequiredAnnotationBeanPostProcessor这个类的作用主要就是针对于@Required注解的解析;
(4)CommonAnnotationBeanPostProcessor这个类的作用就是支持通用Java注解,尤其是JSR-250注解,也就是javax.annotation包内的那些注解。比如 @PostConstruct, @PreDestroy,@Resource和@WebServiceRef,很多人认为 @PostConstruct是由spring提供的其实不是的,执行顺序为 @Autowired(依赖注入) -> @PostConstruct(注释的方法);
(5)EventListenerMethodProcessor他的作用就是将@EventListener注解的方法作为单个ApplicationListener实例注册,它实现了SmartInitializingSingleton会在bean初始化完成以后调用,主要是执行所有@EventListener的方法;
(6)DefaultEventListenerFactory他的作用就是支持@EventListener注解的,上边的类在创建ApplicationListener的时候需要DefaultEventListenerFactory来进行创建。
关于Spring启动早期最先注册的六个BeanDefinition的解释
最新推荐文章于 2022-11-19 11:07:26 发布