Spring的bean的生命周期主要分为以下4个过程。
1.实例化 Instantiation
2.属性赋值 Populate
3.初始化 Initialization
4.销毁 Destruction
执行过程
1.在创建Bean的方法中,首先执行了resolveBeforeInstantiation,实现实例化bean之前的前置操作
2.执行doCreateBean(),会调用createBeanInstance实例化Bean
3.执行populateBean()属性赋值,但是在属性赋值前,会调用ibp.postProcessAfterInstantiation,实现实例化的后置操作;
4.执行populcateBean()中的属性赋值
5.执行initializeBean()Bean的初始化,会先调用applyBeanPostProcessorsBeforeInitialization(),实现初始化Bean之前的前置操作
6.执行initializeBean()中的invokeInitMethods()实现Bean的初始操作
7.执行initializeBean()中的applyBeanPostProcessorsAfterInitialization()实现初始化Bean之后的后置操作
createBean(){
Object bean = resolveBeforeInstantiation(beanName, mbdToUse); //前置操作(InstantiationAwareBeanPostProcessor.postProcessBeforeInstantiation)
doCreateBean(){
createBeanInstance() //1.实例化
populateBean(){ //2.赋值操作
ibp.postProcessAfterInstantiation //后置操作(InstantiationAwareBeanPostProcessor.postProcessAfterInstantiation)
}
initializeBean(){ //初始化
(1)applyBeanPostProcessorsBeforeInitialization//前置操作(AbstractAutowireCapableBeanFactory.postProcessBeforeInstantiation)
(2)invokeInitMethods(beanName, wrappedBean, mbd); //初始化
(3)applyBeanPostProcessorsAfterInitialization//后置操作(InstantiationAwareBeanPostProcessor.postProcessAfterInitialization
}
}
}
扩展点
1.实例化Bean的前后扩展点
InstantiationAwareBeanPostProcessor.postProcessBeforeInstantiation
InstantiationAwareBeanPostProcessor.postProcessAfterInstantiation
.前置扩展点
createBean(){
...
//resolveBeforeInstantiation方法中实现实例化bean之前的前置操作
try {
// Give BeanPostProcessors a chance to return a proxy instead of the target bean instance.
Object bean = resolveBeforeInstantiation(beanName, mbdToUse);
if (bean != null) {
return bean;
}
}
}
//创建Bean
Object beanInstance = doCreateBean(beanName, mbdToUse, args);
.后置扩展点
populateBean(){
...
//属性赋值前调用postProcessAfterInstantiation,实现实例化bean的后置操作
if (!mbd.isSynthetic() && hasInstantiationAwareBeanPostProcessors()) {
for (BeanPostProcessor bp : getBeanPostProcessors()) {
if (bp instanceof InstantiationAwareBeanPostProcessor) {
InstantiationAwareBeanPostProcessor ibp = (InstantiationAwareBeanPostProcessor) bp;
if (!ibp.postProcessAfterInstantiation(bw.getWrappedInstance(), beanName)) {
continueWithPropertyPopulation = false;
break;
}
}
}
}
}
2.初始化Bean的前后的扩展点
initializeBean(){
//前置扩展点
wrappedBean = applyBeanPostProcessorsBeforeInitialization(wrappedBean, beanName);
//初始化Bean
invokeInitMethods(beanName, wrappedBean, mbd);
//后置扩展点
wrappedBean = applyBeanPostProcessorsAfterInitialization(wrappedBean, beanName);
}
各种Aware
1.分类1
BeanNameAware
BeanClassLoaderAware
BeanFactoryAware
initializeBean(){
//针对BeanNameAware,BeanClassLoaderAware,BeanFactoryAware赋值
invokeAwareMethods(beanName, bean);
}
private void invokeAwareMethods(String beanName, Object bean) {
if (bean instanceof Aware) {
if (bean instanceof BeanNameAware) {
((BeanNameAware) bean).setBeanName(beanName);
}
if (bean instanceof BeanClassLoaderAware) {
ClassLoader bcl = getBeanClassLoader();
if (bcl != null) {
((BeanClassLoaderAware) bean).setBeanClassLoader(bcl);
}
}
if (bean instanceof BeanFactoryAware) {
((BeanFactoryAware) bean).setBeanFactory(AbstractAutowireCapableBeanFactory.this);
}
}
}
2.分类2
(1)EnvironmentAware
(2)EmbeddedValueResolverAware
(3)ApplicationContextAware
initializeBean(){
//在ApplicationContextAwareProcessor.postProcessBeforeInitialization对Aware赋值
applyBeanPostProcessorsBeforeInitialization
}
@Override
@Nullable
public Object postProcessBeforeInitialization(final Object bean, String beanName) throws BeansException {
AccessControlContext acc = null;
if (System.getSecurityManager() != null &&
(bean instanceof EnvironmentAware || bean instanceof EmbeddedValueResolverAware ||
bean instanceof ResourceLoaderAware || bean instanceof ApplicationEventPublisherAware ||
bean instanceof MessageSourceAware || bean instanceof ApplicationContextAware)) {
acc = this.applicationContext.getBeanFactory().getAccessControlContext();
}
if (acc != null) {
AccessController.doPrivileged((PrivilegedAction<Object>) () -> {
invokeAwareInterfaces(bean);
return null;
}, acc);
}
else {
invokeAwareInterfaces(bean);
}
return bean;
}
private void invokeAwareInterfaces(Object bean) {
if (bean instanceof Aware) {
if (bean instanceof EnvironmentAware) {
((EnvironmentAware) bean).setEnvironment(this.applicationContext.getEnvironment());
}
if (bean instanceof EmbeddedValueResolverAware) {
((EmbeddedValueResolverAware) bean).setEmbeddedValueResolver(this.embeddedValueResolver);
}
if (bean instanceof ResourceLoaderAware) {
((ResourceLoaderAware) bean).setResourceLoader(this.applicationContext);
}
if (bean instanceof ApplicationEventPublisherAware) {
((ApplicationEventPublisherAware) bean).setApplicationEventPublisher(this.applicationContext);
}
if (bean instanceof MessageSourceAware) {
((MessageSourceAware) bean).setMessageSource(this.applicationContext);
}
if (bean instanceof ApplicationContextAware) {
((ApplicationContextAware) bean).setApplicationContext(this.applicationContext);
}
}
}
参考: https://www.jianshu.com/p/1dec08d290c1