Bean创建过程:
一、版本
5.3.10
二、学习内容
Bean创建过程源码
三、Bean生命周期
时间轴地址:点击
四、bean创建过程脑图总结
脑图地址:点击
五、源码过程
说明:
bean创建入口一般都是通过getBean(xxx);方法进入的,进入后就会调用doGetBean方法,
咱们就直接从AbstractBeanFactory类的doGetBean开始
doGetBean()
//1、执行getSingleton(beanName);从单例池里获取该bean,咱们新建肯定没有,直接往下
getSingleton(beanName);
//2、判断如果是单例直接执行(备注:本文只讲常规单例bean创建):
getSingleton(beanName, () -> {
try {
return createBean(beanName, mbd, args);
}
catch (BeansException ex) {
// Explicitly remove instance from singleton cache: It might have been put there
// eagerly by the creation process, to allow for circular reference resolution.
// Also remove any beans that received a temporary reference to the bean.
destroySingleton(beanName);
throw ex;
}
});
getSingleton(String beanName,ObjectFactory<?> singletonFactory)方法:
//1、创建当前bean的BeanCurrentlyInCreationException对象;
beforeSingletonCreation(beanName);
//2、实际创建,这里是用函数式(@FunctionalInterface)接口ObjectFactory<T>写的
//.getObject() == createBean(); 【createBean()方法比较关键,后面细说】
singletonFactory.getObject();
//3、this.singletonsCurrentlyInCreation.remove(beanName)
afterSingletonCreation(beanName);
//4、将创建的单例bean放入单例池中:
//this.singletonObjects.put(beanName, singletonObject);
//this.singletonFactories.remove(beanName);
//this.earlySingletonObjects.remove(beanName);
//this.registeredSingletons.add(beanName);
addSingleton(beanName, singletonObject);
//5、创建完成返回Bean; 下面的内容主要是补充剖析第二点。
createBean()方法:
//1、实例化前执行:这里主要执行所有实现了InstantiationAwareBeanPostProcessor的处理器,
//执行方法为:postProcessBeforeInstantiation();如果执行了上面方法获得了对象还会执行:
//postProcessAfterInitialization();方法最终返回bean对象。
resolveBeforeInstantiation(beanName, mbdToUse);
//2、重点方法,详解在下面:
//1)、执行创建
//2)、属性注入
//3)、初始化 等
Object beanInstance = doCreateBean(beanName, mbdToUse, args);
doCreateBean()方法
//1、创建bean实例,这里边会有有一个获取构造方法的逻辑,这里就不多讲了,默认取无参构造。
createBeanInstance(beanName, mbd, args);
//2、实例化后执行BeanPostProcessor:
//执行所有MergedBeanDefinitionPostProcessor 类型beanPostProcessor的postProcessMergedBeanDefinition方法();
applyMergedBeanDefinitionPostProcessors(mbd, beanType, beanName);
//3、将bean实例存入三级缓存中,这里对循环依赖有所有帮助的。
//一级缓存:singletonObjects
//二级缓存:earlySingletonObjects
//三级缓存:singletonFactories
addSingletonFactory(beanName, () -> getEarlyBeanReference(beanName, mbd, bean));
//4、属性填充:主要依赖于bean后置处理器来完成
//4.1、属性填充执行BeanPostProcessor处理1:
//执行所有InstantiationAwareBeanPostProcessor类型的bean处理器postProcessAfterInstantiation()方法
//4.2、属性填充执行BeanPostProcessor处理2:
//执行所有InstantiationAwareBeanPostProcessor类型的bean处理器postProcessProperties()方法,
//这里有非常重要的处理AutowiredAnnotationBeanPostProcessor和CommonAnnotationBeanPostProcessor 处理器,会执行依赖注入功能。
populateBean(beanName, mbd, instanceWrapper);
//5、初始化
initializeBean(beanName, exposedObject, mbd);
//5.1、执行Aware(部分)接口实现的功能:
//BeanNameAware
//BeanClassLoaderAware
//BeanFactoryAware
invokeAwareMethods(beanName, bean);
//5.2、初始化前BeanPostProcessor处理:
//获取所有实现BeanPostProcessor的处理器
//执行postProcessBeforeInitialization()方法
//特殊说明:这里有个ApplicationContextAwareProcessor处理器,主要处理部分Aware接口实现类的功能:
//EnvironmentAware
//EmbeddedValueResolverAware
//ResourceLoaderAware
//MessageSourceAware
//ApplicationStartupAware
//ApplicationEventPublisherAware
//ApplicationContextAware
applyBeanPostProcessorsBeforeInitialization(wrappedBean, beanName);
//5.3、初始化:
//1)、执行实现了InitializingBean类的afterPropertiesSet()方法初始化
//2)、执行init-method方法初始化
invokeInitMethods(beanName, wrappedBean, mbd);
//5.4、初始化后执行处理器:
//执行所有实现BeanPostProcessor的处理器postProcessAfterInitialization()方法;
applyBeanPostProcessorsAfterInitialization(wrappedBean, beanName);