文章目录
容器刷新前的SpringFactories扩展
Spring启动过程监听扩展接口:SpringApplicationRunListener,实现案例:事件发布器EventPublishingRunListenerdefault 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监听了事件后还有一个后置处理扩展类:EnvironmentPostProcessorpublic 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; }