SpringApplicationRunListener Interface1,ApplicationListener Interface is ApplicationContext Event listener for 2,EnvironmentPostProcessor Interface Context context postprocessor, call in event
3,PropertySourceLoader Interface Customize the profile loader and resolve the profile properties by yourself 4,ApplicationContextInitializer Interface context initializer
5,BeanFactoryPostProcessor Interface pair BeanFactory The post processor is operated 6,Aware Interface and its sub interfaces Will be refreshContext()Method bean Call, instance Aware Interface method of sub interface. Used to get Spring The objects related to startup are directly called when needed after the project is started 7,BeanPostProcessor Interface be used for bean Initialization method init-method Processing before and after calling.
8,ApplicationRunner and CommandLineRunner Interface
Above
ApplicationListener, ApplicationContextInitializer, beanfactoryprocessor and BeanPostProcessor interfaces belong to the original Spring framework
EnvironmentPostProcessor, PropertySourceLoader, ApplicationRunner and SpringApplicationRunListener interfaces are new extension interfaces of SpringBoot
SpringApplicationRunListener interface
Event broadcasting in SpringBoot is to distribute events through the listener SpringApplicationRunListener interface implementation class EventPublishingRunListener, so as to call the ApplicationListener listener
So we can configure a custom springboot listener
It is configured in meta-inf / spring Configuration in the factories file
# Run Listeners org.springframework.boot.SpringApplicationRunListener=\ org.springframework.boot.context.event.EventPublishingRunListener
1. ApplicationListener interface
yes Spring The event listener in each stage of the application life cycle can trigger the operation we want to perform at any stage
Be responsible for corresponding treatment at each stage
such as
ApplicationEnvironmentPreparedEvent Load the profile properties into the data source in the environment preparation completion event
The custom listener configuration is also configured in meta-inf / spring Factories in files
# Application Listeners org.springframework.context.ApplicationListener=\ org.springframework.boot.ClearCachesApplicationListener,\ org.springframework.boot.builder.ParentContextCloserApplicationListener
2. EnvironmentPostProcessor interface
Context post processor. ApplicationEnvironmentPreparedEvent Call in event initialization systemProperties,systemEnvironment Processing after attribute source( servletConfigInitParams,servletContextInitParams Both attribute sources (uninitialized) such as ConfigFileApplicationListener.postProcessEnvironment() Properties for loading profiles{.xml ,.properties,.yml,.yaml Suffix file}To system property source Add the configuration file to the attribute source after reading propertySources.add(new OriginTrackedMapPropertySource(name + documentNumber, loaded.get(i)));
This custom configuration is also configured in meta-inf / spring In the factories file
# Environment Post Processors
org.springframework.boot.env.EnvironmentPostProcessor=\
3. PropertySourceLoader interface
Customize the profile loader and resolve the profile properties by yourself, For example, parsing.json The suffix's profile attribute is added to the system Reference link: https://blog.csdn.net/catoop/article/details/71157986
It is configured in meta-inf / spring Configuration mode in the factories file
# PropertySource Loaders org.springframework.boot.env.PropertySourceLoader=\ org.springframework.boot.env.PropertiesPropertySourceLoader,\ org.springframework.boot.env.YamlPropertySourceLoader
4. ApplicationContextInitializer interface
stay springBoot At startup prepareContext() These interface pairs are called when the context is prepared applicationContext Do some initialization.
Call time
ApplicationEnvironmentPreparedEvent After the event context is ready
ApplicationPreparedEvent Call before event
such as
ContextIdApplicationContextInitializer Initializer for context generation contextId context id
ConfigurationWarningsApplicationContextInitializer
increase BeanFactory Factory post processor context.addBeanFactoryPostProcessor,Warning information used to report configuration errors during startup postProcessBeanDefinitionRegistry()Processing in method
It is configured in meta-inf / spring Configuration mode in factories file
# Application Context Initializers org.springframework.context.ApplicationContextInitializer=\ org.springframework.boot.context.ConfigurationWarningsApplicationContextInitializer,\ org.springframework.boot.context.ContextIdApplicationContextInitializer
5. Beanfactoryprocessor interface
Sub interface: BeanDefinitionRegistryPostProcessor, which can also implement sub interfaces to operate beanfactory. Post processor that operates on beanfactory
In general: you can register custom beans through the BeanDefinitionRegistryPostProcessor interface method (you can also operate beanFactory to do the corresponding operation)
Trigger time: refreshContext(context) = = "5.4 invokebeanfactoryprocessors (beanfactory)
Can be initialized bean We will customize it before BeanFactoryPostProcessor Implementation class registered to BeanFactory In, BeanDefinitionRegistryPostProcessor Interface implementation class, which can be customized in the bean We will customize it before defining it Bean Define register to BeanFactory in Spring Customize for us Bean The analysis of is also through BeanDefinitionRegistryPostProcessor Interface Implementation class ConfigurationClassPostProcessor To scan and register bean definition The execution process is: First ApplicationContext of beanFactoryPostProcessors Convert list to BeanDefinitionRegistryPostProcessor(registryProcessors (registered processor) and BeanFactoryPostProcessor(regularPostProcessors Rule processor) First, it will be executed registryProcessors Registered processor postProcessBeanDefinitionRegistry(registry)Method bean Defined registration Registration for the first time Will get BeanFactory Built in BeanDefinitionRegistryPostProcessor Interface implementation class, and this class should implement PriorityOrdered Interface==>ConfigurationClassPostProcessor Will call ConfigurationClassPostProcessor Class to scan and register all bean definition. Second registration: Will get BeanFactory Acquired after scanning in BeanDefinitionRegistryPostProcessor Interface implementation class, and this class should implement Ordered Interface Call registration method Third registration: Same acquisition BeanDefinitionRegistryPostProcessor Interface implementation, except for the above implementation classes such as mybatis dependent bean Registration is the third registration( MapperScannerConfigurer)
After the registration operation, the above BeanDefinitionRegistryPostProcessor and BeanFactoryPostProcessor Interface implementation class
Call interface method postProcessor.postProcessBeanFactory(beanFactory),beanFactory Post operation of
BeanFactoryPostProcessor or BeanDefinitionRegistryPostProcessor custom implementation classes can be registered before this is called
That is, refreshContext(context); Before refreshing the context
ApplicationContext can be called when the ApplicationPreparedEvent event event is triggered Addbeanfactoryprocessor() method added to
Beanfactoryprocessors list
Of course, you can also add before this event.
6. Aware interface and its sub interfaces
There are many interfaces to implement Aware interface
Trigger timing: refreshContext(context) = = 6.0, finishBeanFactoryInitialization(beanFactory)
Distinguish according to different calling places:
6.1
BeanNameAware,BeanClassLoaderAware,BeanFactoryAware
The above three interfaces are in
AbstractAutowireCapableBeanFactory of initializeBean(beanName, exposedObject, mbd);method invokeAwareMethods(beanName, bean);Method will handle
6.2
ApplicationContextAwareProcessor Processing implementation in post processor
EnvironmentAware,EmbeddedValueResolverAware,ResourceLoaderAware,
ApplicationEventPublisherAware,MessageSourceAware,ApplicationContextAware Of these interfaces Bean Interface method call
6.3 webapplicationcontextservletcontextawareprocessor post processor processing
bean that implements ServletContextAware interface
7. BeanPostProcessor interface
It is equivalent to interceptors before and after bean initialization method calls
It will be processed before and after the initialization method init method call of the Bean
BeanPostProcessor.postProcessBeforeInitialization(Object bean, String beanName) Call before initialization
BeanPostProcessor.postProcessAfterInitialization(Object bean, String beanName) Call after initialization
8. ApplicationRunner and CommandLineRunner interfaces
It is called before the ApplicationReadyEvent event is triggered after the ApplicationStartedEvent event.
Used to perform post startup operations.
For example, my customized @ EventBusListener annotation is added to beans. After all beans are initialized, I need to obtain this bean list and register it in my event center for asynchronous event subscription, publishing and execution of stand-alone applications
@Component public class MyApplicationRunner implements ApplicationRunner { @Override public void run(ApplicationArguments args) throws Exception { EventBusCenter eventBusCenter = SpringContextUtils.getBean(EventBusCenter.class); // Get all with @EventBusListener of bean,Register them as listeners Map<String, Object> beansWithAnnotation = SpringContextUtils.getApplicationContext().getBeansWithAnnotation(EventBusListener.class); List<Object> listeners = beansWithAnnotation.entrySet().stream().map(Map.Entry::getValue).collect(Collectors.toList()); eventBusCenter.registry(listeners); } }
Of course, we can also use the event listener to perform this operation. Be flexible
To be added