本文将从源码的角度来分析SpringBean的创建过程
1. BeanDefinition
大家都知道,在Spring IOC启动时,会扫描类的信息,将相关信息封装为BeanDefinition
存入到beanDefinitionMap
中,后续用其中的描述信息来生成bean。
2. getBean
首先从AbstractBeanFactory
类入手,因为它是IOC容器的顶层实现类,基本上都是用其中的getBean
去获取bean的。
public Object getBean(String name) throws BeansException {return doGetBean(name, null, null, false);
}
其中重写了四个getBean
方法,仅仅只是传入的参数不同。其中都是调用的doGetBean
方法。
在Spring中,方法命名都挺统一,真正干事儿的方法,基本都是以do开头,优秀的代码习惯值得学习!
3. doGetBean
以下是doGetBean
方法的代码:
protected <T> T doGetBean(String name, @Nullable Class<T> requiredType, @Nullable Object[] args, boolean typeCheckOnly)throws BeansException {String beanName = transformedBeanName(name);Object beanInstance;// Eagerly check singleton cache for manually registered singletons.Object sharedInstance = getSingleton(beanName);if (sharedInstance != null && args == null) {if (logger.isTraceEnabled()) {if (isSingletonCurrentlyInCreation(beanName)) {logger.trace("Returning eagerly cached instance of singleton bean '" + beanName +"' that is not fully initialized yet - a consequence of a circular reference");}else {logger.trace("Returning cached instance of singleton bean '" + beanName + "'");}}beanInstance = getObjectForBeanInstance(sharedInstance, name, beanName, null);}else {// Fail if we're already creating this bean instance:// We're assumably within a circular reference.if (isPrototypeCurrentlyInCreation(beanName)) {throw new BeanCurrentlyInCreationException(beanName);}// Check if bean definition exists in this factory.BeanFactory parentBeanFactory = getParentBeanFactory();if (parentBeanFactory != null && !containsBeanDefinition(beanName)) {// Not found -> check parent.String nameToLookup = originalBeanName(name);if (parentBeanFactory instanceof AbstractBeanFactory) {return ((AbstractBeanFactory) parentBeanFactory).doGetBean(nameToLookup, requiredType, args, typeCheckOnly);}else if (args != null) {// Delegation to parent with explicit args.return (T) parentBeanFactory.getBean(nameToLookup, args);}else if (requiredType != null) {// No args -> delegate to standard getBean method.return parentBeanFactory.getBean(nameToLookup, requiredType);}else {return (T) parentBeanFactory.getBean(nameToLookup);}}if (!typeCheckOnly) {markBeanAsCreated(beanName);}StartupStep beanCreation = this.applicationStartup.start("spring.beans.instantiate").tag("beanName", name);try {if (requiredType != null) {beanCreation.tag("beanType", requiredType::toString);}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);try {getBean(dep);}catch (NoSuchBeanDefinitionException ex) {throw new BeanCreationException(mbd.getResourceDescription(), beanName,"'" + beanName + "' depends on missing bean '" + dep + "'", ex);}}}// Create bean instance.if (mbd.isSingleton()) {sharedInstance = 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;}});beanInstance = getObjectForBeanInstance(sharedInstance, name, beanName, mbd);}else if (mbd.isPrototype()) {// It's a prototype -> create a new instance.Object prototypeInstance = null;try {beforePrototypeCreation(beanName);prototypeInstance = createBean(beanName, mbd, args);}finally {afterPrototypeCreation(beanName);}beanInstance = getObjectForBeanInstance(prototypeInstance, name, beanName, mbd);}else {String scopeName = mbd.getScope();if (!StringUtils.hasLength(scopeName)) {throw new IllegalStateException("No scope name defined for bean '" + beanName + "'");}Scope scope = this.scopes.get(scopeName);if (scope == null) {throw new IllegalStateException("No Scope registered for scope name '" + scopeName + "'");}try {Object scopedInstance = scope.get(beanName, () -> {beforePrototypeCreation(beanName);try {return createBean(beanName, mbd, args);}finally {afterPrototypeCreation(beanName);}});beanInstance = getObjectForBeanInstance(scopedInstance, name, beanName, mbd);}catch (IllegalStateException ex) {throw new ScopeNotActiveException(beanName, scopeName, ex);}}}catch (BeansException ex) {beanCreation.tag("exception", ex.getClass().toString());beanCreation.tag("message", String.valueOf(ex.getMessage()));cleanupAfterBeanCreationFailure(beanName);throw ex;}finally {beanCreation.end();}}return adaptBeanInstance(name, beanInstance, requiredType);
}
我们来一步一步的对代码进行解读:
1、获取去除了&
符号的bean名称,再在aliasMap
中寻找别名。
2、根据beanName从单例缓存中查找已经存在的对象。
3、如果单例缓存中存在bean,且传入的args为null,则按需打印日志,如果bean是FactoryBean
,则返回getObject
中的对象,否则返回此bean。
4、但对于单例缓存中不存在bean或者传入的args不为null的处理:
-
如果beanName是正在创建中的原型bean,则抛出错误。
-
由于
AbstractBeanFactory
实现了HierarchicalBeanFactory
,即父子级联 IOC容器的接口,所以可以对parentBeanFactory
进行操作。 -
如果parentBeanFactory不为null,而且当前的beanFactory中不包含此beanName的BeanDefinition,那就去parentBeanFactory中去寻找此bean的定义信息。如果typeCheckOnly为false,则将bean标记为已经创建或者将要创建。
-
获取一个合并了的RootBeanDefinition,如果该bean是一个abstract类型的,就抛出错误。
-
获取当前bean中依赖的(dependsOn)bean,逐个检查,如果A依赖于B,且B又以来于A,那么就抛出BeanCreationException错误。保存Dependent信息,其中
dependenciesForBeanMap
保存当前bean依赖于哪些bean,dependentBeanMap
保存当前被依赖的bean被哪些bean所依赖。举个例子:A dependsOn B、C, D dependsOn B、E,那么dependenciesForBeanMap
就会保存A -> B、C,D -> B、E,而dependentBeanMap
中保存的是B -> A、D,C -> A,E -> D。接着调用getBean先获取依赖的bean。 -
对单例bean的处理
- 去单例缓存中找对应的bean。
- 如果没有找到就通过createBean进行创建bean。
- 创建的bean可能是FactoryBean,这时需要调用其getObject获取到bean。
-
对原型bean的处理
- 将当前beanName标记为正在创建中的原型bean,其实就是利用了一个变量名为prototypesCurrentlyInCreation的ThreadLocal。
- 通过createBean进行创建bean。
- 对beanName进行取消创建中的原型bean标记,就是从prototypesCurrentlyInCreation中移除。
- 创建的bean可能是FactoryBean,这时需要调用其getObject获取到bean。
-
对其他scope的处理
- 根据BeanDefinition保存的scope名称从scopes(一个Map)中获取到Scope。
- 调用scope的get方法,不同的Scope有不同的操作方式,但是创建从ObjectFactory中调用getObject来获取bean的途径都是一样的,同Prototype(原型)bean的处理。如SessionScope的处理方式是将beanName和从ObjectFactory取到的bean及范围标识符SCOPE_SESSION存入RequestAttributes中去,每次取时,都要先从RequestAttributes取beanName对应的bean,没有才创建,这就保证了在同一SESSION下bean的唯一性。
5、检查所需类型是否与实际bean实例的类型匹配,必要时进行转换。
为了更加了解每一个细节,我们来对doGetBean中部分方法来做一个详细的说明。
3.1 transformedBeanName
protected String transformedBeanName(String name) {return canonicalName(BeanFactoryUtils.transformedBeanName(name));}public String canonicalName(String name) {String canonicalName = name;// Handle aliasing...String resolvedName;do {resolvedName = this.aliasMap.get(canonicalName);if (resolvedName != null) {canonicalName = resolvedName;}}while (resolvedName != null);return canonicalName;}// BeanFactoryUtilspublic static String transformedBeanName(String name) {Assert.notNull(name, "'name' must not be null");if (!name.startsWith(BeanFactory.FACTORY_BEAN_PREFIX)) {return name;}return transformedBeanNameCache.computeIfAbsent(name, beanName -> {do {beanName = beanName.substring(BeanFactory.FACTORY_BEAN_PREFIX.length());}while (beanName.startsWith(BeanFactory.FACTORY_BEAN_PREFIX));return beanName;});}
这个方法用于获取规范的beanName。
BeanFactoryUtils.transformedBeanName
中主要是去除beanName前面的&
符号,如入参&&&serviceA&123
,经过处理后会返回serviceA&123
。并将&&&serviceA&123
与serviceA&123
的关系保存到transformedBeanNameCache
这个Map中。
canonicalName中主要是从别名map中获取最基本的beanName,如果beanName为serviceA
的bean具有别名alias1
,而别名alias1
又保存了一个别名alias2
,那么就可以通过alias2
得到serviceA
。
3.2 getSingleton
protected Object getSingleton(String beanName, boolean allowEarlyReference) {// Quick check for existing instance without full singleton lockObject singletonObject = this.singletonObjects.get(beanName);if (singletonObject == null && isSingletonCurrentlyInCreation(beanName)) {singletonObject = this.earlySingletonObjects.get(beanName);if (singletonObject == null && allowEarlyReference) {synchronized (this.singletonObjects) {// Consistent creation of early reference within full singleton locksingletonObject = this.singletonObjects.get(beanName);if (singletonObject == null) {singletonObject = this.earlySingletonObjects.get(beanName);if (singletonObject == null) {ObjectFactory<?> singletonFactory = this.singletonFactories.get(beanName);if (singletonFactory != null) {singletonObject = singletonFactory.getObject();this.earlySingletonObjects.put(beanName, singletonObject);this.singletonFactories.remove(beanName);}}}}}}return singletonObject;
}
这个方法是用于获取单例bean。
先从singletonObjects这个map取,如果还没有生成且这个bean被标记为了正在创建中,则尝试从earlySingletonObjects中取,如果这里面也没有且容器允许早期引用,则先锁住singletonObjects,防止多个线程造成singletonObjects中生成多个对象,从而违反了单例的初衷;再次进行判断(线程同步知识点),如果还是没有,就需要去生成bean了,这里是从singletonFactories中获取到这个bean的ObjectFactory,再调用其getObject方法去创建一个bean。创建完成后,将bean存入到earlySingletonObjects中去,并删除其ObjectFactory对象。
这里讲到的singletonObjects、earlySingletonObjects、singletonObjects就是三个Map,分别称之为第一级、第二级、第三级缓存。为什么要使用如此多的变量呢?这是为了解决Spring中的循环依赖问题,具体将在后续的文章中进行说明。
3.3 getObjectForBeanInstance
protected Object getObjectForBeanInstance(Object beanInstance, String name, String beanName, @Nullable RootBeanDefinition mbd) {// Don't let calling code try to dereference the factory if the bean isn't a factory.if (BeanFactoryUtils.isFactoryDereference(name)) {if (beanInstance instanceof NullBean) {return beanInstance;}if (!(beanInstance instanceof FactoryBean)) {throw new BeanIsNotAFactoryException(beanName, beanInstance.getClass());}if (mbd != null) {mbd.isFactoryBean = true;}return beanInstance;}// Now we have the bean instance, which may be a normal bean or a FactoryBean.// If it's a FactoryBean, we use it to create a bean instance, unless the// caller actually wants a reference to the factory.if (!(beanInstance instanceof FactoryBean)) {return beanInstance;}Object object = null;if (mbd != null) {mbd.isFactoryBean = true;}else {object = getCachedObjectForFactoryBean(beanName);}if (object == null) {// Return bean instance from factory.FactoryBean<?> factory = (FactoryBean<?>) beanInstance;// Caches object obtained from FactoryBean if it is a singleton.if (mbd == null && containsBeanDefinition(beanName)) {mbd = getMergedLocalBeanDefinition(beanName);}boolean synthetic = (mbd != null && mbd.isSynthetic());object = getObjectFromFactoryBean(factory, beanName, !synthetic);}return object;}
从BeanInstance中得到Object,因为得到的beanInstance可能是一个FactoryBean
,这时需要调用其getObject才能得到真正需要的bean实例。
BeanFactoryUtils.isFactoryDereference
就是判断beanName是否以&
开头,如果是,那就是要获取这个FactoryBean。如果bean实例不是一个FactoryBean,那么就是普通bean的获取,直接返回。
Object object = null;
if (mbd != null) {mbd.isFactoryBean = true;
}
else {object = getCachedObjectForFactoryBean(beanName);
}
if (object == null) {// Return bean instance from factory.FactoryBean<?> factory = (FactoryBean<?>) beanInstance;// Caches object obtained from FactoryBean if it is a singleton.if (mbd == null && containsBeanDefinition(beanName)) {mbd = getMergedLocalBeanDefinition(beanName);}boolean synthetic = (mbd != null && mbd.isSynthetic());object = getObjectFromFactoryBean(factory, beanName, !synthetic);
}
如果传入的mbd为null,则先尝试从factoryBeanObjectCache
缓存中取对象,没有的话再进行创建。如果mbd不为null,则直接进行创建。
创建的流程为:
- 如果mbd为null,且当前上下文中存在bean定义,则合并
BeanDefinition
。 - 从
FactoryBean
中生成bean,并将其放在factoryBeanObjectCache
缓存中。
这里有两个主要的分支方法,继续解读。
3.3.1 getMergedLocalBeanDefinition
protected RootBeanDefinition getMergedLocalBeanDefinition(String beanName) throws BeansException {// Quick check on the concurrent map first, with minimal locking.RootBeanDefinition mbd = this.mergedBeanDefinitions.get(beanName);if (mbd != null && !mbd.stale) {return mbd;}return getMergedBeanDefinition(beanName, getBeanDefinition(beanName));
}
首先从mergedBeanDefinitions
缓存中取,如果能取到合并后的BeanDefinition
,且为不可被合并的状态,则直接返回缓存对象。否则进入合并流程。
protected RootBeanDefinition getMergedBeanDefinition(String beanName, BeanDefinition bd, @Nullable BeanDefinition containingBd)throws BeanDefinitionStoreException {synchronized (this.mergedBeanDefinitions) {RootBeanDefinition mbd = null;RootBeanDefinition previous = null;// Check with full lock now in order to enforce the same merged instance.if (containingBd == null) {mbd = this.mergedBeanDefinitions.get(beanName);}if (mbd == null || mbd.stale) {previous = mbd;if (bd.getParentName() == null) {// Use copy of given root bean definition.if (bd instanceof RootBeanDefinition) {mbd = ((RootBeanDefinition) bd).cloneBeanDefinition();}else {mbd = new RootBeanDefinition(bd);}}else {// Child bean definition: needs to be merged with parent.BeanDefinition pbd;try {String parentBeanName = transformedBeanName(bd.getParentName());if (!beanName.equals(parentBeanName)) {pbd = getMergedBeanDefinition(parentBeanName);}else {BeanFactory parent = getParentBeanFactory();if (parent instanceof ConfigurableBeanFactory) {pbd = ((ConfigurableBeanFactory) parent).getMergedBeanDefinition(parentBeanName);}else {throw new NoSuchBeanDefinitionException(parentBeanName,"Parent name '" + parentBeanName + "' is equal to bean name '" + beanName +"': cannot be resolved without a ConfigurableBeanFactory parent");}}}catch (NoSuchBeanDefinitionException ex) {throw new BeanDefinitionStoreException(bd.getResourceDescription(), beanName,"Could not resolve parent bean definition '" + bd.getParentName() + "'", ex);}// Deep copy with overridden values.mbd = new RootBeanDefinition(pbd);mbd.overrideFrom(bd);}// Set default singleton scope, if not configured before.if (!StringUtils.hasLength(mbd.getScope())) {mbd.setScope(SCOPE_SINGLETON);}// A bean contained in a non-singleton bean cannot be a singleton itself.// Let's correct this on the fly here, since this might be the result of// parent-child merging for the outer bean, in which case the original inner bean// definition will not have inherited the merged outer bean's singleton status.if (containingBd != null && !containingBd.isSingleton() && mbd.isSingleton()) {mbd.setScope(containingBd.getScope());}// Cache the merged bean definition for the time being// (it might still get re-merged later on in order to pick up metadata changes)if (containingBd == null && isCacheBeanMetadata()) {this.mergedBeanDefinitions.put(beanName, mbd);}}if (previous != null) {copyRelevantMergedBeanDefinitionCaches(previous, mbd);}return mbd;}}
这个方法比较长,让我们来一步一步分析:
- 首先,这个方法有三个参数,
String beanName
,BeanDefinition bd
,BeanDefinition containingBd
,分别为bean名称,beanName对应的BeanDefinition,beanName所包含的BeanDefinition。比如beanName对应的是一个内部bean,那么containingBd则是其外层bean的定义。 - 现在来对mbd进行赋值,如果bd的parentName为null,那就将bd包装成一个RootBeanDefinition,并赋值给mbd。如果parentName不为null,那就需要考虑到父子级联容器的处理了,判断此beanName对应的beanDefinition存在于当前容器中还是父容器中,并从对应的容器中的得到parentName进行merge之后的BeanDefinition,并包装成一个RootBeanDefinition,赋值给mbd。
- 给mbd的scope赋值,默认赋值
SCOPE_SINGLETON
,如果外层bean定义不为null,且其scope不是单例,但是这个bean又是单例,则将mbd的scope设置为跟外层bean的scope一致。 - 将合并结果存入mergedBeanDefinitions缓存。
- 复制属性到mbd中。
3.3.2 getObjectFromFactoryBean
protected Object getObjectFromFactoryBean(FactoryBean<?> factory, String beanName, boolean shouldPostProcess) {if (factory.isSingleton() && containsSingleton(beanName)) {synchronized (getSingletonMutex()) {Object object = this.factoryBeanObjectCache.get(beanName);if (object == null) {object = doGetObjectFromFactoryBean(factory, beanName);// Only post-process and store if not put there already during getObject() call above// (e.g. because of circular reference processing triggered by custom getBean calls)Object alreadyThere = this.factoryBeanObjectCache.get(beanName);if (alreadyThere != null) {object = alreadyThere;}else {if (shouldPostProcess) {if (isSingletonCurrentlyInCreation(beanName)) {// Temporarily return non-post-processed object, not storing it yet..return object;}beforeSingletonCreation(beanName);try {object = postProcessObjectFromFactoryBean(object, beanName);}catch (Throwable ex) {throw new BeanCreationException(beanName,"Post-processing of FactoryBean's singleton object failed", ex);}finally {afterSingletonCreation(beanName);}}if (containsSingleton(beanName)) {this.factoryBeanObjectCache.put(beanName, object);}}}return object;}}else {Object object = doGetObjectFromFactoryBean(factory, beanName);if (shouldPostProcess) {try {object = postProcessObjectFromFactoryBean(object, beanName);}catch (Throwable ex) {throw new BeanCreationException(beanName, "Post-processing of FactoryBean's object failed", ex);}}return object;}
}
这里是从FactoryBean中获取到bean,主要有两个分支,一个是如果这个FactoryBean是单例的,且beanName对应的bean是存在于singletonObjects
单例缓存中的,那就需要控制从FactoryBean的getObject方法生成的bean也是单例的;如果非单例,那么每次都是使用新生成的bean。
控制单例,最好的方法就是加缓存,这次生成过后,下次再来生成时优先使用缓存中已经存在的对象,没有才去考虑新生成对象。所以这里用了一个factoryBeanObjectCache
的Map来作为getObject生成的bean的缓存。
这里生成的过程就是先从FactoryBean的getObject方法的到一个原始的对象,再根据传入的shouldPostProcess
去判断是否应该执行BeanPostProcessor
的postProcessAfterInitialization
方法。这里在对bean执行Bean的后置处理器之前,需要将bean标记为正在创建中(将beanName放入singletonsCurrentlyInCreation
的Set集合),执行完后撤销标记(在singletonsCurrentlyInCreation
中移除beanName)。
3.4 createBean
这一步是创建bean的具体方法,也是至关重要的一步。
@Override
protected Object createBean(String beanName, RootBeanDefinition mbd, @Nullable Object[] args)throws BeanCreationException {if (logger.isTraceEnabled()) {logger.trace("Creating instance of bean '" + beanName + "'");}RootBeanDefinition mbdToUse = mbd;// Make sure bean class is actually resolved at this point, and// clone the bean definition in case of a dynamically resolved Class// which cannot be stored in the shared merged bean definition.Class<?> resolvedClass = resolveBeanClass(mbd, beanName);if (resolvedClass != null && !mbd.hasBeanClass() && mbd.getBeanClassName() != null) {mbdToUse = new RootBeanDefinition(mbd);mbdToUse.setBeanClass(resolvedClass);}// Prepare method overrides.try {mbdToUse.prepareMethodOverrides();}catch (BeanDefinitionValidationException ex) {throw new BeanDefinitionStoreException(mbdToUse.getResourceDescription(),beanName, "Validation of method overrides failed", ex);}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;}}catch (Throwable ex) {throw new BeanCreationException(mbdToUse.getResourceDescription(), beanName,"BeanPostProcessor before instantiation of bean failed", ex);}try {Object beanInstance = doCreateBean(beanName, mbdToUse, args);if (logger.isTraceEnabled()) {logger.trace("Finished creating instance of bean '" + beanName + "'");}return beanInstance;}catch (BeanCreationException | ImplicitlyAppearedSingletonException ex) {// A previously detected exception with proper bean creation context already,// or illegal singleton state to be communicated up to DefaultSingletonBeanRegistry.throw ex;}catch (Throwable ex) {throw new BeanCreationException(mbdToUse.getResourceDescription(), beanName, "Unexpected exception during bean creation", ex);}
}
这里时创建bean和初始化bean的方法,这里生成bean有两种途径,一种是通过InstantiationAwareBeanPostProcessor
后置处理器生成的,另一种是常规通过反射来生成的。
接下来对两个主要的方法来进行解析:
3.4.1 resolveBeforeInstantiation
@Nullable
protected Object resolveBeforeInstantiation(String beanName, RootBeanDefinition mbd) {Object bean = null;if (!Boolean.FALSE.equals(mbd.beforeInstantiationResolved)) {// Make sure bean class is actually resolved at this point.if (!mbd.isSynthetic() && hasInstantiationAwareBeanPostProcessors()) {Class<?> targetType = determineTargetType(beanName, mbd);if (targetType != null) {bean = applyBeanPostProcessorsBeforeInstantiation(targetType, beanName);if (bean != null) {bean = applyBeanPostProcessorsAfterInitialization(bean, beanName);}}}mbd.beforeInstantiationResolved = (bean != null);}return bean;
}
如果RootBeanDefinition的beforeInstantiationResolved
属性为true,如果此bean定义不是合成的,且存在InstantiationAwareBeanPostProcessor
类型的bean后置处理器,那么就调用InstantiationAwareBeanPostProcessor
的postProcessBeforeInstantiation
方法得到其返回的bean,如果有多个InstantiationAwareBeanPostProcessor
,那么取第一个有返回的值。再调用beanPostProcessors
这个Map中的BeanPostProcessor
的postProcessAfterInitialization
方法对bean进行处理。最后返回bean。
3.4.2 doCreateBean
protected Object doCreateBean(String beanName, RootBeanDefinition mbd, @Nullable Object[] args)throws BeanCreationException {// Instantiate the bean.BeanWrapper instanceWrapper = null;if (mbd.isSingleton()) {instanceWrapper = this.factoryBeanInstanceCache.remove(beanName);}if (instanceWrapper == null) {instanceWrapper = createBeanInstance(beanName, mbd, args);}Object bean = instanceWrapper.getWrappedInstance();Class<?> beanType = instanceWrapper.getWrappedClass();if (beanType != NullBean.class) {mbd.resolvedTargetType = beanType;}// Allow post-processors to modify the merged bean definition.synchronized (mbd.postProcessingLock) {if (!mbd.postProcessed) {try {applyMergedBeanDefinitionPostProcessors(mbd, beanType, beanName);}catch (Throwable ex) {throw new BeanCreationException(mbd.getResourceDescription(), beanName,"Post-processing of merged bean definition failed", ex);}mbd.postProcessed = true;}}// Eagerly cache singletons to be able to resolve circular references// even when triggered by lifecycle interfaces like BeanFactoryAware.boolean earlySingletonExposure = (mbd.isSingleton() && this.allowCircularReferences &&isSingletonCurrentlyInCreation(beanName));if (earlySingletonExposure) {if (logger.isTraceEnabled()) {logger.trace("Eagerly caching bean '" + beanName +"' to allow for resolving potential circular references");}addSingletonFactory(beanName, () -> getEarlyBeanReference(beanName, mbd, bean));}// Initialize the bean instance.Object exposedObject = bean;try {populateBean(beanName, mbd, instanceWrapper);exposedObject = initializeBean(beanName, exposedObject, mbd);}catch (Throwable ex) {if (ex instanceof BeanCreationException && beanName.equals(((BeanCreationException) ex).getBeanName())) {throw (BeanCreationException) ex;}else {throw new BeanCreationException(mbd.getResourceDescription(), beanName, "Initialization of bean failed", ex);}}if (earlySingletonExposure) {Object earlySingletonReference = getSingleton(beanName, false);if (earlySingletonReference != null) {if (exposedObject == bean) {exposedObject = earlySingletonReference;}else if (!this.allowRawInjectionDespiteWrapping && hasDependentBean(beanName)) {String[] dependentBeans = getDependentBeans(beanName);Set<String> actualDependentBeans = new LinkedHashSet<>(dependentBeans.length);for (String dependentBean : dependentBeans) {if (!removeSingletonIfCreatedForTypeCheckOnly(dependentBean)) {actualDependentBeans.add(dependentBean);}}if (!actualDependentBeans.isEmpty()) {throw new BeanCurrentlyInCreationException(beanName,"Bean with name '" + beanName + "' has been injected into other beans [" +StringUtils.collectionToCommaDelimitedString(actualDependentBeans) +"] in its raw version as part of a circular reference, but has eventually been " +"wrapped. This means that said other beans do not use the final version of the " +"bean. This is often the result of over-eager type matching - consider using " +"'getBeanNamesForType' with the 'allowEagerInit' flag turned off, for example.");}}}}// Register bean as disposable.try {registerDisposableBeanIfNecessary(beanName, bean, mbd);}catch (BeanDefinitionValidationException ex) {throw new BeanCreationException(mbd.getResourceDescription(), beanName, "Invalid destruction signature", ex);}return exposedObject;
}
为了得到instanceWrapper
, 首先从factoryBeanInstanceCache
中找,看此beanName是否是一个单例的factoryBean,是则移除并取出,如果不是,则进入createBeanInstance
去创建一个BeanWrapper
。
创建BeanWrapper有多种方式,如工厂方法、构造函数自动装配或简单实例化,createBeanInstance中根据配置去使用不同的策略去创建。
然后是使用MergedBeanDefinitionPostProcessor
这种类型的后置处理器去允许修改合并后的BeanDefinition
。
接着如果允许bean早期曝光,那就将这个beanName和创建的ObjectFactory对象存入第三级缓存singletonFactories中。
addSingletonFactory(beanName, () -> getEarlyBeanReference(beanName, mbd, bean));protected void addSingletonFactory(String beanName, ObjectFactory<?> singletonFactory) {Assert.notNull(singletonFactory, "Singleton factory must not be null");synchronized (this.singletonObjects) {if (!this.singletonObjects.containsKey(beanName)) {this.singletonFactories.put(beanName, singletonFactory);this.earlySingletonObjects.remove(beanName);this.registeredSingletons.add(beanName);}}
}
这里() -> getEarlyBeanReference(beanName, mbd, bean)
实际就是一个ObjectFactory对象。
protected Object getEarlyBeanReference(String beanName, RootBeanDefinition mbd, Object bean) {Object exposedObject = bean;if (!mbd.isSynthetic() && hasInstantiationAwareBeanPostProcessors()) {for (SmartInstantiationAwareBeanPostProcessor bp : getBeanPostProcessorCache().smartInstantiationAware) {exposedObject = bp.getEarlyBeanReference(exposedObject, beanName);}}return exposedObject;
}
其中就是返回已经被new出来的对象,只是这里可以用SmartInstantiationAwareBeanPostProcessor
这个后置处理器对new出来的对象进行处理。
接着populateBean,其中是对bean属性的赋值,包括@Autowired等自动注入的属性,有兴趣的可以参考文章《死磕Spring系列:@Autowired工作原理》。
接着initializeBean,对bean进行初始化,大概流程如下:
- 如果bean实现了
BeanNameAware
、BeanClassLoaderAware
、BeanFactoryAware
接口,那么就会分别回调其实现的方法。 - 执行
BeanPostProcessor
的postProcessBeforeInitialization
方法。 - 如果bean实现了
InitializingBean
接口,那么就会回调其afterPropertiesSet
方法。如果指定了init
方法,则用反射执行。 - 执行
BeanPostProcessor
的postProcessAfterInitialization
方法。
实例化,初始化完成之后,就需要对之前曝光的操作做处理了。
if (earlySingletonExposure) {Object earlySingletonReference = getSingleton(beanName, false);if (earlySingletonReference != null) {if (exposedObject == bean) {exposedObject = earlySingletonReference;}else if (!this.allowRawInjectionDespiteWrapping && hasDependentBean(beanName)) {String[] dependentBeans = getDependentBeans(beanName);Set<String> actualDependentBeans = new LinkedHashSet<>(dependentBeans.length);for (String dependentBean : dependentBeans) {if (!removeSingletonIfCreatedForTypeCheckOnly(dependentBean)) {actualDependentBeans.add(dependentBean);}}if (!actualDependentBeans.isEmpty()) {throw new BeanCurrentlyInCreationException(beanName,"Bean with name '" + beanName + "' has been injected into other beans [" +StringUtils.collectionToCommaDelimitedString(actualDependentBeans) +"] in its raw version as part of a circular reference, but has eventually been " +"wrapped. This means that said other beans do not use the final version of the " +"bean. This is often the result of over-eager type matching - consider using " +"'getBeanNamesForType' with the 'allowEagerInit' flag turned off, for example.");}}}}
此时去调用getSingleton
,注意这里getSingleton(beanName, false)
,第二个allowEarlyReference
参数为false,那就只会从一二级缓存中查找数据,并不会从ObjectFactory中生成对象。
最开始创建A的时候,这里是肯定不能从二级缓存中获得到A bean的,那在什么情况下这里的二级缓存能取到A bean呢?
@Component
public class A {@Autowiredprivate B b;
}
@Component
public class B {@Autowiredprivate A a;
}
比如在这种循环依赖的场景下,A通过getSingleton去创建A Bean时,会将自身的ObjectFactory放入singletonFactories,到了赋值B的这一步,会去创建B bean,B到了赋值A时,会去调用getSingleton获取A bean,而这时,A会从singletonFactories中获取到早期对象放入earlySingletonObjects中去。A完成了属性b的赋值,就会走到当前这步,而这里的二级缓存earlySingletonObjects中就会有值。
由于现在的exposedObject是经过BeanPostProcessor
的处理了的,所以现在的exposedObject和存在二级缓存earlySingletonObjects
中的bean可能已经是两个不同的对象了,所以需要进行判断,如果前后两个对象还是还是同一个对象,那就把经过SmartInstantiationAwareBeanPostProcessor
处理后的bean给exposedObject用。
如果原始的bean、经过BeanPostProcessor处理的bean、SmartInstantiationAwareBeanPostProcessor处理的bean这三者都不一致,但是这个Bean已经被其他Bean依赖了,但是又不知道到底是使用哪一个bean实例了,所以不得不抛出异常来终止程序运行。
最后注册DisposableBean,返回需要曝光的对象。
经过方法的处理,如果是单例模式,生成的对象已经存在于二级缓存earlySingletonObjects
中了,那是什么时候放入到一级缓存中的呢?我们来继续分析。
3.5 getSingleton(beanName, objectFactory)
if (mbd.isSingleton()) {sharedInstance = 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;}});beanInstance = getObjectForBeanInstance(sharedInstance, name, beanName, mbd);}
回到这段逻辑,我们进入到这里的getSingleton
方法去看看:
public Object getSingleton(String beanName, ObjectFactory<?> singletonFactory) {Assert.notNull(beanName, "Bean name must not be null");synchronized (this.singletonObjects) {Object singletonObject = this.singletonObjects.get(beanName);if (singletonObject == null) {if (this.singletonsCurrentlyInDestruction) {throw new BeanCreationNotAllowedException(beanName,"Singleton bean creation not allowed while singletons of this factory are in destruction " +"(Do not request a bean from a BeanFactory in a destroy method implementation!)");}if (logger.isDebugEnabled()) {logger.debug("Creating shared instance of singleton bean '" + beanName + "'");}beforeSingletonCreation(beanName);boolean newSingleton = false;boolean recordSuppressedExceptions = (this.suppressedExceptions == null);if (recordSuppressedExceptions) {this.suppressedExceptions = new LinkedHashSet<>();}try {singletonObject = singletonFactory.getObject();newSingleton = true;}catch (IllegalStateException ex) {// Has the singleton object implicitly appeared in the meantime ->// if yes, proceed with it since the exception indicates that state.singletonObject = this.singletonObjects.get(beanName);if (singletonObject == null) {throw ex;}}catch (BeanCreationException ex) {if (recordSuppressedExceptions) {for (Exception suppressedException : this.suppressedExceptions) {ex.addRelatedCause(suppressedException);}}throw ex;}finally {if (recordSuppressedExceptions) {this.suppressedExceptions = null;}afterSingletonCreation(beanName);}if (newSingleton) {addSingleton(beanName, singletonObject);}}return singletonObject;}
}
可以看到,这里逻辑不复杂,就是先从一级缓存singletonObjects
中查找,如果有则返回,没有则创建并放入一级缓存。
创建前需要将beanName放入到singletonsCurrentlyInCreation
中,来表示这个单例bean正在创建,创建之后从这个Set中移除,表示不处于创建过程中了。
而真正创建的过程,就是从我们上面分析的ObjectFactory中生成的,在生成之后,会将得到的bean进行addSingleton处理。
protected void addSingleton(String beanName, Object singletonObject) {synchronized (this.singletonObjects) {this.singletonObjects.put(beanName, singletonObject);this.singletonFactories.remove(beanName);this.earlySingletonObjects.remove(beanName);this.registeredSingletons.add(beanName);}
}
可以得知,得到bean之后,会放入一级缓存singletonObjects
,并清除二级缓存earlySingletonObjects
,三级缓存singletonFactories
中的缓存数据。
4、总结
简而言之,Spring Bean的创建过程就是:
先从缓存中取,如果取不到,就去创建,如果这个bean的定义是在父容器中的,那就去父容器中继续从头开始执行。
将beanName标记为alreadyCreated,将父子容器和外层bean的BeanDefinition进行合并,执行依赖bean的初始化流程,根据singleton、prototype、其他scope进行bean的实例化和初始化。
知识点:
-
可以在未进行bean的实例化之前就通过InstantiationAwareBeanPostProcessor直接返回一个bean。
-
可以在实例化bean之后,初始化之前,通过MergedBeanDefinitionPostProcessor对合并后的BeanDefinition进行处理。
-
在将bean放入二级缓存earlySingletonObjects之前,可以通过SmartInstantiationAwareBeanPostProcessor对生成的bean进行处理。