1. 调用doGetBean方法,进入doGetBean方法
2. 首先从缓存中获取bean,如果有则获取返回
// Eagerly check singleton cache for manually registered singletons.
Object sharedInstance = getSingleton(beanName);
if (sharedInstance != null && args == null) {
if (logger.isDebugEnabled()) {
if (isSingletonCurrentlyInCreation(beanName)) {
logger.debug("Returning eagerly cached instance of singleton bean '" + beanName +
"' that is not fully initialized yet - a consequence of a circular reference");
}
else {
logger.debug("Returning cached instance of singleton bean '" + beanName + "'");
}
}
bean = getObjectForBeanInstance(sharedInstance, name, beanName, null);
}
注:这个机制保证了单实例bean只会被创建一次,查看 getSingleton 就会发现是从Map<String, Object> singletonObjects 中获取的
3. 如果缓存中没有,就走创建bean的流程(略过一些代码,只关注单实例bean的创建)
- 标记bean创建了
if (!typeCheckOnly) { markBeanAsCreated(beanName); }
- 获取bean定义,且判断是否有依赖的bean,如果有先创建依赖的bean
final RootBeanDefinition mbd = getMergedLocalBeanDefinition(beanName); checkMergedBeanDefinition(mbd, beanName, args); // Guarantee initialization of beans that the current bean depends on. String[] dependsOn = mbd.getDependsOn(); if (dependsOn != null) { for (String dep : dependsOn) { if (isDependent(beanName, dep)) { throw new BeanCreationException(mbd.getResourceDescription(), beanName, "Circular depends-on relationship between '" + beanName + "' and '" + dep + "'"); } registerDependentBean(dep, beanName); getBean(dep); } }
注:此处正是dependsOn属性,即@DependsOn在源码处的体现
- 如果是单实例bean就走
createBean
创建bean- 解析bean,
resolveBeanClass(mbd, beanName);
- 给bean的后置处理器一个机会创建bean的代理对象 - 即
InstantiationAwareBeanPostProcessor
提前执行postProcessBeforeInstantiation
注:使用切面后可以关注此方法// Give BeanPostProcessors a chance to return a proxy instead of the target bean instance. Object bean = resolveBeforeInstantiation(beanName, mbdToUse);
- 创建bean,进入
doCreateBean
- 创建bean实例
instanceWrapper = createBeanInstance(beanName, mbd, args);
- 如果有工厂方法,则使用构造方法或工厂方法创建bean
instantiateUsingFactoryMethod(beanName, mbd, args);
- 如果有工厂方法,则使用构造方法或工厂方法创建bean
-
MergedBeanDefinitionPostProcessor
回调postProcessMergedBeanDefinition
合并bean定义applyMergedBeanDefinitionPostProcessors(mbd, beanType, beanName);
- 为bean的属性赋值
populateBean(beanName, mbd, instanceWrapper);
- 处理实现
InstantiationAwareBeanPostProcessor
接口的后置处理器回调postProcessAfterInstantiation
- 处理实现
InstantiationAwareBeanPostProcessor
接口的后置处理器回调postProcessPropertyValues
- 注:
InstantiationAwareBeanPostProcessor
接口至此三个方法都执行了
- 处理实现
- 初始化bean
exposedObject = initializeBean(beanName, exposedObject, mbd);
- 执行Aware 方法
invokeAwareMethods(beanName, bean);
-
applyBeanPostProcessorsBeforeInitialization(wrappedBean, beanName);
执行后置处理器初始化之前的方法 其实就是循环回调方法 -
invokeInitMethods(beanName, wrappedBean, mbd);
执行初始化方法
是InitializingBean接口的实现,则实现接口规定的初始化
有自定义初始化 执行自定义初始化的方法 -
applyBeanPostProcessorsAfterInitialization(wrappedBean, beanName);
执行后置处理器初始化之后的方法
- 执行Aware 方法
-
registerDisposableBeanIfNecessary(beanName, bean, mbd);
注册bean销毁方法
- 创建bean实例
-
getSingletonadd
的addSingleton(beanName, singletonObject);
将单实例bean加入缓存中
- 解析bean,
- 所有bean创建完成后,检查所有的bean是否是
SmartInitializingSingleton
- 如果是执行
afterSingletonsInstantiated
- 如果是执行
网友评论