Spring扩展点
1、SpringIOC扩展点
1.1、bean的生命周期
-
BeanNameAware's {@code setBeanName}
-
BeanClassLoaderAware's {@code setBeanClassLoader}
-
BeanFactoryAware's {@code setBeanFactory}
-
EnvironmentAware's {@code setEnvironment}
-
EmbeddedValueResolverAware's {@code setEmbeddedValueResolver}
-
ResourceLoaderAware's {@code setResourceLoader}
============ 应用程序上下文运行时可用================ start
ApplicationEventPublisherAware's {@code setApplicationEventPublisher}
MessageSourceAware's {@code setMessageSource}
ApplicationContextAware's {@code setApplicationContext}
============ 应用程序上下文运行时可用================ end
============ web应用程序上下文运行时可用================ start
ServletContextAware's {@code setServletContext}
============ web应用程序上下文运行时可用================ end
============ 初始化阶段============================================
{@code postProcessBeforeInitialization} methods of BeanPostProcessors
InitializingBean's {@code afterPropertiesSet}
a custom init-method definition
{@code postProcessAfterInitialization} methods of BeanPostProcessors
============ 销毁阶段============================================
{@code postProcessBeforeDestruction} methods of DestructionAwareBeanPostProcessors
DisposableBean's {@code destroy}
a custom destroy-method definition
解释下xxxAware
我们看到bean
的生命周期是先加载了一堆的Aware
结尾的类。Aware意思为具有xxx
的能力,假如你实现了该类型接口,你的类就具有了Aware
前面描述的能力,例如
我实现了EnvironmentAware
我的当前类就拥有了Environment
的能力。
public class CustomEnvironment implements EnvironmentAware{
private static Environment environment;
@Override
public void setEnvironment(Environment environment) {
CustomEnvironment.environment = environment;
}
}
这些类的作用就是让你在做扩展的时候能够很方便直接使用IOC
容器的一系列能力。
执行完Aware之后接下来就是初始化阶段执行实现了BeanPostProcessors
接口的postProcessBeforeInitialization
方法,然后再执行InitializingBean
的afterPropertiesSet
方法初始化,然后执行自定义初始化方法,然后再执行BeanPostProcessors
的postProcessAfterInitialization
方法。销毁阶段先执行DestructionAwareBeanPostProcessor
的postProcessBeforeDestruction
方法销毁需要回收的bean实例,如listener
实例。接着执行DisposableBean
接口的destroy
方法,最后执行自定义的销毁方法。
简单的验证例子
定义一个CustomBeanPostProcessor
/**
* @Description TODO
* @Author 姚仲杰#80998699
* @Date 2021/3/16 14:27
*/
@Component
public class CustomBeanPostProcessor implements BeanPostProcessor {
@Bean(initMethod = "customInit",destroyMethod = "customDestroy")
TestBean testBean(){
return new TestBean();
}
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName)
throws BeansException {
if (bean instanceof TestBean) {
System.out.println("2、BeanPostProcessor:postProcessBeforeInitialization");
}
return bean;
}
@Override
public Object postProcessAfterInitialization(Object bean, String beanName)
throws BeansException {
if (bean instanceof TestBean) {
System.out.println("5、BeanPostProcessor:postProcessAfterInitialization");
}
return bean;
}
在定义一个TestBean,并在CustomBeanPostProcessor
使用@Bean注解初始化设置自定义初始化以及销毁方法。按我们所知道的顺序给他们标上序号。
/**
* @Description TODO
* @Author 姚仲杰#80998699
* @Date 2021/3/16 14:38
*/
public class TestBean implements InitializingBean, DisposableBean, ApplicationContextAware {
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
System.out.println("1、setApplicationContext");
}
@Override
public void afterPropertiesSet() throws Exception {
System.out.println("3、InitializingBean:afterPropertiesSet");
}
/**
* 自定义初始化
*/
public void customInit() {
System.out.println("4、TestBean:custom init");
}
@Override
public void destroy() throws Exception {
System.out.println("6、DisposableBean:destroy");
}
/**
* 自定义销毁
*/
public void customDestroy() {
System.out.println("7、TestBean:custom destroy");
}
}
然后直接运行输出结果如下。
1、setApplicationContext
2、BeanPostProcessor:postProcessBeforeInitialization
3、InitializingBean:afterPropertiesSet
4、TestBean:custom init
5、BeanPostProcessor:postProcessAfterInitialization
停止时打印如下信息
6、DisposableBean:destroy
7、TestBean:custom destroy
小提示:不要把
@PostConstruct
与@PreDestroy
当作自定义初始化和销毁的注解。他们不是。这两个注解执行时机时在InitializingBean:afterPropertiesSet
之前和DisposableBean:destroy
之前。