bean的生命周期
bean创建--初始化--销毁的过程
容器管理bean的生命周期
我们可以自定义初始化和销毁方法:容器在bean进行到当前生命周期的时候来调用我们自定义的初始化和销毁方法。
构造(对象创建)
单实例:在容器启动的时候创建对象
多实例:在每次获取的时候创建对象
BeanPostProcessor.postProcessBeforeInitialization
初始化:
对象创建完成,并赋值好,调用初始化方法 :也就是一些init-method
BeanPostProcessor.postProcessAfterInitialization
销毁:
单实例:容器关闭的时候
多实例,容器不会管理这个bean,容器不会调用销毀方法
1)、指定初始化和销毀方法
通过@Bean指定 init-method和 destroy- method
2)、通过让Bean实现InitializingBean(定义初始化逻辑)
DisposableBean(定义销毁逻辑);
3)、可以使用JsR250
Postconstruct:在bean创建完成并且属性赋值完成;来执行初始化方法
PreDestroy在容器销毁bean之前通知我们进行清理工作
4)、 BeanPostProcessor【 interface】:bean的后置处理器;
在bean初始化前后进行一些处理工作
postProcessBeforeInitialization:在初始化之前工作
postProcessAfterInitiailization:在初始化之后工作
BeanPostProcessor 原理
package cn.jsycnqz.bean;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.stereotype.Component;
/**
* @author Ni Qingzhan
* @create 2020-07-21-16:25
*/
@Component
public class MyBeanPostProcessor implements BeanPostProcessor {
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
System.out.println("postProcessBeforeInitialization..." + beanName);
return bean;
}
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
System.out.println("postProcessAfterInitialization..." + beanName);
return bean;
}
}
public class MainTest {
public static void main(String[] args) {
AnnotationConfigApplicationContext annotationConfigApplicationContext
= new AnnotationConfigApplicationContext(MainConfig.class);
System.out.println("容器创建完成");
annotationConfigApplicationContext.close();
}
}
源码结构 debug的栈结构。
在 E:\maven_repository\org\springframework\spring-beans\5.0.2.RELEASE\spring-beans-5.0.2.RELEASE.jar!\org\springframework\beans\factory\support\AbstractAutowireCapableBeanFactory.class
文件中:
Object wrappedBean = bean;
if (mbd == null || !mbd.isSynthetic()) {
wrappedBean = this.applyBeanPostProcessorsBeforeInitialization(bean, beanName);
}
try {
this.invokeInitMethods(beanName, wrappedBean, mbd);
} catch (Throwable var6) {
throw new BeanCreationException(mbd != null ? mbd.getResourceDescription() : null, beanName, "Invocation of init method failed", var6);
}
if (mbd == null || !mbd.isSynthetic()) {
wrappedBean = this.applyBeanPostProcessorsAfterInitialization(wrappedBean, beanName);
}
applyBeanPostProcessorsBeforeInitialization
invokeInitMethods
applyBeanPostProcessorsAfterInitialization
发现在初始化之前之后,有applyBeanPostProcessorsBeforeInitialization和applyBeanPostProcessorsAfterInitialization方法
applyBeanPostProcessorsBeforeInitialization
具体就是遍历所有的BeanPostProcessor,执行它们的postProcessBeforeInitialization方法,一旦有一个返回null,直接返回。
跳出for循环。
public Object applyBeanPostProcessorsBeforeInitialization(Object existingBean, String beanName) throws BeansException {
Object result = existingBean;
Object current;
for(Iterator var4 = this.getBeanPostProcessors().iterator(); var4.hasNext(); result = current) {
BeanPostProcessor beanProcessor = (BeanPostProcessor)var4.next();
current = beanProcessor.postProcessBeforeInitialization(result, beanName);
if (current == null) {
return result;
}
}
return result;
}
try {
this.populateBean(beanName, mbd, instanceWrapper);
exposedObject = this.initializeBean(beanName, exposedObject, mbd);
} catch (Throwable var18) {
if (var18 instanceof BeanCreationException && beanName.equals(((BeanCreationException)var18).getBeanName())) {
throw (BeanCreationException)var18;
}
throw new BeanCreationException(mbd.getResourceDescription(), beanName, "Initialization of bean failed", var18);
}
返回上一层函数,上述三个方法都在this.initializeBean中,之前有一个populatedBean方法。其作用是给bean进行属性赋值。