Spring和SpringBoot的重要回调机制

一、启动回调

应用场景:

1.启动前环境检测?

2.启动时配置初始化?

3.启动后数据初始化?

 

类型
注入方式
回调时机
总体回调顺序
应用场景
ApplicationContextInitializer
spring.factories
等3种
IOC容器初始化时被回调
1
启动前环境检测
SpringApplicationRunListener
spring.factories
springboot启动过程中被多次回调
贯穿整个过程,最开始到最后
非常灵活,比如启动时配置初始化
ApplicationRunner
IOC容器,及类加注解@Component
容器启动完成后被回调
2
启动后数据初始化
CommandLineRunner
IOC容器,及类加注解@Component
ApplicationRunner后被回调
3
启动后数据初始化
ApplicationReadyEvent
IOC容器,及类加注解@Component
implements ApplicationListener<ApplicationReadyEvent>
在CommandLineRunner后
SpringApplicationRunListener...running前
4
启动后数据初始化

 

回调顺序:

SpringApplicationRunListener...starting...

SpringApplicationRunListener...environmentPrepared..Windows 10

ApplicationContextInitializer:...run....

SpringApplicationRunListener...contextPrepared...

SpringApplicationRunListener...contextLoaded...

SpringApplicationRunListener...started...

ApplicationRunner1...run....

ApplicationRunner2...run....

CommandLineRunner...run...[]

onApplicationEvent-ApplicationReadyEvent...run....

SpringApplicationRunListener...running...

 

二、Bean后置处理

 BeanFactoryPostProcessor和BeanPostProcessor

  •  BeanFactoryPostProcessor

1..功能:对BeanFactory扩展后置处理

2.子接口扩展:BeanDefinitionRegistryPostProcessor,增加对BeanDefinitionRegistry的扩展处理

3.典型实现类:ConfigurationClassPostProcessor

4.源码实现:

org.springframework.context.support.AbstractApplicationContext#invokeBeanFactoryPostProcessors

protected void invokeBeanFactoryPostProcessors(ConfigurableListableBeanFactory beanFactory) {
     // 添加和执行BeanFactoryPostProcessor PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(beanFactory, getBeanFactoryPostProcessors());
// Detect a LoadTimeWeaver and prepare for weaving, if found in the meantime // (e.g. through an @Bean method registered by ConfigurationClassPostProcessor) if (beanFactory.getTempClassLoader() == null && beanFactory.containsBean(LOAD_TIME_WEAVER_BEAN_NAME)) { beanFactory.addBeanPostProcessor(new LoadTimeWeaverAwareProcessor(beanFactory)); beanFactory.setTempClassLoader(new ContextTypeMatchClassLoader(beanFactory.getBeanClassLoader())); } }

 

关键方法:PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors

invokeBeanFactoryPostProcessors主要做的就是执行BeanDefinitionRegistryPostProcessor、BeanFactoryPostProcessor的2个扩展方法,按照主要排序、排序、无顺序要求的三类BeanFactoryPostProcessor执行。这些BeanFactoryPostProcessors可能是内部Spring实现添加好的,也可能是来自ClassPath扫描出来的BeanFactoryPostProcessors

  • BeanPostProcessor

1.功能:对Bean扩展后置处理

2.子接口扩展:

MergedBeanDefinitionPostProcessor
InstantiationAwareBeanPostProcessor
DestructionAwareBeanPostProcessor

3.典型实现类:

AutowiredAnnotationBeanPostProcessor
InitDestroyAnnotationBeanPostProcessor
CommonAnnotationBeanPostProcessor

4.源码实现:

org.springframework.context.support.AbstractApplicationContext#registerBeanPostProcessors

protected void registerBeanPostProcessors(ConfigurableListableBeanFactory beanFactory) {
  PostProcessorRegistrationDelegate.registerBeanPostProcessors(beanFactory, this);
}

  

关键方法:PostProcessorRegistrationDelegate.registerBeanPostProcessors

 

三、Bean生命周期回调

  • Spring接口扩展InitializingBean.afterPropertiesSet(),DisposableBean.destroy()
  • 基于JSR- 250注解@PostConstruct,@PreDestroy
  • 基于Spring xml配置init,destroy方法

四、Aware回调操作

比如:ApplicationContextAware、EnvironmentAware等
posted @ 2022-08-14 09:51  windge  阅读(851)  评论(0)    收藏  举报