文章目录
容器刷新前的SpringFactories扩展
-
Spring
启动过程监听扩展接口:SpringApplicationRunListener
,实现案例:事件发布器EventPublishingRunListener
default void starting() {} default void environmentPrepared(ConfigurableEnvironment environment) {} default void contextPrepared(ConfigurableApplicationContext context) {} default void contextLoaded(ConfigurableApplicationContext context) {} default void started(ConfigurableApplicationContext context) {} default void running(ConfigurableApplicationContext context) {} default void failed(ConfigurableApplicationContext context, Throwable exception) {}
-
Spring
事件监听器,就是(1)
中EventPublishingRunListener
每种事件的再一次扩展:ApplicationListener
,根据类型ApplicationEvent
事件类型区分。public interface ApplicationListener<E extends ApplicationEvent> extends EventListener { void onApplicationEvent(E event); }
-
ApplicationEnvironmentPreparedEvent
环境准备事件有实现类BootstrapApplicationListener
、ConfigFileApplicationListener
,其中ConfigFileApplicationListener
监听了事件后还有一个后置处理扩展类:EnvironmentPostProcessor
public interface EnvironmentPostProcessor { void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application); }
-
SpringBootExceptionReporter
异常通知器,实现FailureAnalyzer
接或者AbstractFailureAnalyzer
抽象类处理Spring启动过程中的制定异常。 -
容器初始化扩展:
ApplicationContextInitializer
和容器准备事件的前一步触发
public interface ApplicationContextInitializer<C extends ConfigurableApplicationContext> { /** * Initialize the given application context. * @param applicationContext the application to configure */ void initialize(C applicationContext); }
容器刷新中的可扩展功能
在容器刷新周期的可扩展功能,根据执行顺序和,接口的最顶级记录
BeanFactoryPostProcessors
工厂的后置处理扩展接口
-
BeanFactoryPostProcessor
对BeanFactory
后置处理public interface BeanFactoryPostProcessor { void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException; }
-
BeanDefinitionRegistryPostProcessor
对BeanDefinitionRegistry
后置处理public interface BeanDefinitionRegistryPostProcessor extends BeanFactoryPostProcessor { void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException }
Bean创建的相关可扩展处理器
-
InstantiationAwareBeanPostProcessor
对象创建前的后置处理器,发生在doCreateBean
之前,给对象一个机会提前返回对象。下面源码只放出相关的可扩展方法public interface InstantiationAwareBeanPostProcessor extends BeanPostProcessor { @Nullable default Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) throws BeansException { return null; } default boolean postProcessAfterInstantiation(Object bean, String beanName) throws BeansException { return true; } ......省略不相关方法 }
-
SmartInstantiationAwareBeanPostProcessor
的determineCandidateConstructors
方法发生在实例对象创建的时候,用来获取对象的构造函数。default Constructor<?>[] determineCandidateConstructors(Class<?> beanClass, String beanName) throws BeansException { return null; }
-
MergedBeanDefinitionPostProcessor
的postProcessMergedBeanDefinition
的方法,发生在对象刚创建之后,还有没填充属性之前。void postProcessMergedBeanDefinition(RootBeanDefinition beanDefinition, Class<?> beanType, String beanName);
-
在
populateBean
填充属性方法中会先执行InstantiationAwareBeanPostProcessor
的postProcessAfterInstantiation
后置处理操作。default boolean postProcessAfterInstantiation(Object bean, String beanName) throws BeansException { return true; }
-
在
populateBean
开始填充前,会执行InstantiationAwareBeanPostProcessor
的postProcessPropertyValues
方法进行对象属性的扩展。可以返回扩展的属性填充配置,然后开始正式的属性填充default PropertyValues postProcessPropertyValues( PropertyValues pvs, PropertyDescriptor[] pds, Object bean, String beanName) throws BeansException { return pvs; }
-
对象的初始化,先执行三个
Aware
接口BeanNameAware
、BeanClassLoaderAware
、BeanFactoryAware
。然后执行初始化逻辑,执行顺序如下:1、
BeanPostProcessor
的postProcessBeforeInitialization
初始化前处逻辑
2、InitializingBean
的afterPropertiesSet
方法,或者执行BeanDefinition
的initMethodName
方法。
3、BeanPostProcessor
的postProcessAfterInitialization
初始化后置逻辑 -
对象初始化完就算创建了,然后根据有没有实现
DisposableBean
、AutoCloseable
或者设置destroyMethodName
参数决定是否创建DisposableBean
,它会在容器销毁的时候调用对应的销毁方法。 -
在全部单例对象都创建完后,会判断对象有没有实现
SmartInitializingSingleton
这个接口,如果有会调用它的afterSingletonsInstantiated
方法。public interface SmartInitializingSingleton { void afterSingletonsInstantiated(); }
容器刷新后的可扩展功能
单例对象创建完后呢会调用finishRefresh
完成容器刷新的收尾工作。
-
会先执行生命周期的后置处理器
LifecycleProcessor
的onRefresh
方法,可以自己定义注入。默认有一个DefaultLifecycleProcessor
实现类。在默认的实现类里面可以 实现Lifecycle
接口在容器对象创建好后调用,和容器关闭的时候调用,执行生命周期内的处理逻辑。还有进一步扩展的SmartLifecycle
的接口。public interface Lifecycle { void start(); void stop(); boolean isRunning(); }
收尾扩展
-
在容器启动后,用启动参数执行以下
ApplicationRunner
、CommandLineRunner
这两个接口的后置处理逻辑。public interface ApplicationRunner { void run(ApplicationArguments args) throws Exception; } public interface CommandLineRunner { void run(String... args) throws Exception; }