栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Spring可扩展接口总结

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Spring可扩展接口总结

文章目录
  • 容器刷新前的SpringFactories扩展
  • 容器刷新中的可扩展功能
    • `BeanFactoryPostProcessors`工厂的后置处理扩展接口
    • Bean创建的相关可扩展处理器
  • 容器刷新后的可扩展功能
  • 收尾扩展

容器刷新前的SpringFactories扩展
  1. 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) {}
    
  2. Spring事件监听器,就是(1)中EventPublishingRunListener每种事件的再一次扩展:ApplicationListener,根据类型ApplicationEvent事件类型区分。

    public interface ApplicationListener extends EventListener {
    	void onApplicationEvent(E event);
    }
    
  3. ApplicationEnvironmentPreparedEvent环境准备事件有实现类BootstrapApplicationListener、ConfigFileApplicationListener,其中ConfigFileApplicationListener监听了事件后还有一个后置处理扩展类:EnvironmentPostProcessor

    public interface EnvironmentPostProcessor {
    
    
    	void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application);
    }
    
  4. SpringBootExceptionReporter异常通知器,实现FailureAnalyzer接或者AbstractFailureAnalyzer抽象类处理Spring启动过程中的制定异常。

  5. 容器初始化扩展:ApplicationContextInitializer和容器准备事件的前一步触发

    public interface ApplicationContextInitializer {
    
    	
    	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;
    
    }
    
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/821063.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号