生命周期回调方法
Spring在容器初始化bean之后(完成依赖注入后)和销毁前都提供了回调的方法,我们称之为生命周期的回调方法。Spring中提供了三种方式来完成生命周期的回调。
官网解释
直接看Spring的官网,在 Combining Lifecycle Mechanisms处有关于生命周期回调的介绍,具体如下图:
从Spring2.5开始,有三个控制bean生命周期行为的选项:
1、InitializingBean和DisposableBean回调接口
2、自定义init()和destroy()方法
3、@PostConstruct和@PreDestroy注释
回调接口方式
初始化方法:实现InitializingBean接口中afterPropertiesSet方法
销毁方法:实现DisposableBean接口中destroy方法
public class MyBean implements InitializingBean, DisposableBean {
@Override
public void afterPropertiesSet() throws Exception {
System.out.println("MyBean 实现InitializingBean接口执行");
}
@Override
public void destroy(){
System.out.println("MyBean 实现DisposableBean接口执行");
}
}
但spring官方文档中指出,建议不要使用InitializingBean接口,因为它不必要地将代码耦合到Spring。我们建议使用@PostConstruct注释或指定POJO初始化方法。对于基于XML的配置元数据,可以使用init method属性指定具有void no-argument签名的方法的名称。
自定义方法方式
初始化方法:XML中标签init-method参数或者@Bean注解initMethod 属性
销毁方法:XML中标签destroy-method参数或者@Bean注解destroyMethod 属性
<bean init-method="init" destroy-method="destory" id="user" class="com.suning.xml.UserBean"></bean>
@Bean(initMethod = "customInit",destroyMethod = "customDestroy")
若觉得每个bean都需配置init方法比较麻烦,可在标签中添加默认初始化方法default-init-method
<beans default-init-method="init">
<bean id="blogService" class="com.something.DefaultBlogService">
<property name="blogDao" ref="blogDao" />
</bean>
</beans>
注释方式
初始化方法:@PostConstruct注解
销毁方法:@PreDestroy注解
@PostConstruct
public void annotationInit(){
System.out.println("Bean 注解方式初始化方法执行");
}
@PreDestroy
public void annotationDestroy(){
System.out.println("Bean 注解方式销毁方法执行");
}
官方文档中指出,@PostConstruct和@PreDestroy注解为JSR-250注解,Spring2.5中引入了对这些注释的支持,为初始化回调和销毁回调中描述的生命周期回调机制提供了一种替代方法。CommonAnnotationBeanPostProcessor会识别@Resource注释、JSR-250生命周期注释。如果CommonAnnotationBeanPostProcessor在SpringApplicationContext中注册,则在生命周期中与相应的SpringLifecycle接口方法或显式声明的回调方法相同的点上调用带有这些注释之一的方法。
总结一句话:Spring2.5支持,CommonAnnotationBeanPostProcessor处理注解。
组合生命周期机制
Spring支持上述3中方式并存,那都存在时执行顺序怎样,我们看官方文档。
初始化:
1、@PostConstruct注解方法
2、实现InitializingBean接口中afterPropertiesSet方法
3、xml或bean中自定义的init方法
销毁:
1、@PreDestroy注解方法
2、实现DisposableBean 接口中destroy方法
3、xml或bean中自定义的destroy方法
接下来代码演示:
@Configuration
public class Config {
@Bean(initMethod = "customInit",destroyMethod = "customDestroy")
public MyBean myBean(){
return new MyBean();
}
}
public class MyBean implements InitializingBean, DisposableBean {
@Override
public void afterPropertiesSet() throws Exception {
System.out.println("MyBean 实现InitializingBean接口执行");
}
@Override
public void destroy(){
System.out.println("MyBean 实现DisposableBean接口执行");
}
@PostConstruct
public void annotationInit(){
System.out.println("MyBean 注解方式初始化方法执行");
}
@PreDestroy
public void annotationDestroy(){
System.out.println("MyBean 注解方式销毁方法执行");
}
public void customInit(){
System.out.println("MyBean 自定义初始化方法执行");
}
public void customDestroy(){
System.out.println("MyBean 自定义销毁方法执行");
}
}
@ComponentScan
@Component
public class SpringApplicationContext {
public static void main(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(SpringApplicationContext.class);
context.close();
}
}
我们看运行结果
从结果可知,运行结果和上述描述的一致。
初始化执行顺序源码解析
AbstractAutowireCapableBeanFactory.doCreateBean() -->initializeBean()
protected Object initializeBean(final String beanName, final Object bean, @Nullable RootBeanDefinition mbd) {
//省略部分不重要代码
Object wrappedBean = bean;
if (mbd == null || !mbd.isSynthetic()) {
//后置处理器处理,此处循环所有后置处理器处理
//1、CommonAnnotationBeanPostProcessor处理@PostConstruct注解
wrappedBean = applyBeanPostProcessorsBeforeInitialization(wrappedBean, beanName);
}
try {
invokeInitMethods(beanName, wrappedBean, mbd);
}
catch (Throwable ex) {
throw new BeanCreationException(
(mbd != null ? mbd.getResourceDescription() : null),
beanName, "Invocation of init method failed", ex);
}
if (mbd == null || !mbd.isSynthetic()) {
wrappedBean = applyBeanPostProcessorsAfterInitialization(wrappedBean, beanName);
}
return wrappedBean;
}
@Override
public Object applyBeanPostProcessorsBeforeInitialization(Object existingBean, String beanName)
throws BeansException {
Object result = existingBean;
//取出所有后置处理器,循环处理
for (BeanPostProcessor processor : getBeanPostProcessors()) {
//调用postProcessBeforeInitialization方法
Object current = processor.postProcessBeforeInitialization(result, beanName);
if (current == null) {
return result;
}
result = current;
}
return result;
}
继续看invokeInitMethods方法
protected void invokeInitMethods(String beanName, final Object bean, @Nullable RootBeanDefinition mbd)
throws Throwable {
//2、判断bean是否实现了InitializingBean接口
boolean isInitializingBean = (bean instanceof InitializingBean);
if (isInitializingBean && (mbd == null || !mbd.isExternallyManagedInitMethod("afterPropertiesSet"))) {
if (System.getSecurityManager() != null) {
try {
//调用afterPropertiesSet方法
AccessController.doPrivileged((PrivilegedExceptionAction<Object>) () -> {
((InitializingBean) bean).afterPropertiesSet();
return null;
}, getAccessControlContext());
}
catch (PrivilegedActionException pae) {
throw pae.getException();
}
}
else {
((InitializingBean) bean).afterPropertiesSet();
}
}
//3、获取InitMethodName,判断后执行
if (mbd != null && bean.getClass() != NullBean.class) {
String initMethodName = mbd.getInitMethodName();
if (StringUtils.hasLength(initMethodName) &&
!(isInitializingBean && "afterPropertiesSet".equals(initMethodName)) &&
!mbd.isExternallyManagedInitMethod(initMethodName)) {
invokeCustomInitMethod(beanName, bean, mbd);
}
}
}
总结:
1、initializeBean方法中先调用后置处理器执行@PostConstruct
2、invokeInitMethods中执行InitializingBean接口中afterPropertiesSet方法
3、最后执行我们自定义的init方法
初始化执行顺序源码解析
DefaultSingletonBeanRegistry.destroyBean() -->bean.destroy()
–>DisposableBeanAdapter.destroy()
@Override
public void destroy() {
//beanPostProcessors循环处理
//1、CommonAnnotationBeanPostProcessor处理@PreDestroy注解
if (!CollectionUtils.isEmpty(this.beanPostProcessors)) {
for (DestructionAwareBeanPostProcessor processor : this.beanPostProcessors) {
processor.postProcessBeforeDestruction(this.bean, this.beanName);
}
}
//2、调用DisposableBean接口的destroy方法
if (this.invokeDisposableBean) {
try {
if (System.getSecurityManager() != null) {
AccessController.doPrivileged((PrivilegedExceptionAction<Object>) () -> {
((DisposableBean) this.bean).destroy();
return null;
}, this.acc);
}
else {
((DisposableBean) this.bean).destroy();
}
}
catch (Throwable ex) {
}
}
//3、执行自定义的destroyMethod方法,destroyMethod即为自定义的销毁方法名
if (this.destroyMethod != null) {
invokeCustomDestroyMethod(this.destroyMethod);
}
else if (this.destroyMethodName != null) {
Method methodToCall = determineDestroyMethod(this.destroyMethodName);
if (methodToCall != null) {
invokeCustomDestroyMethod(methodToCall);
}
}
}
总结:
1、destroy方法中先调用后置处理器执行@PreDestroy
2、后执行DisposableBean接口中destroy方法
3、最后执行我们自定义的destroy方法