死磕Spring系列:Bean实例化原理

news/2024/10/30 23:23:51/

本文将从源码的角度来分析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&123serviceA&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;}}

这个方法比较长,让我们来一步一步分析:

  1. 首先,这个方法有三个参数,String beanName, BeanDefinition bd, BeanDefinition containingBd,分别为bean名称,beanName对应的BeanDefinition,beanName所包含的BeanDefinition。比如beanName对应的是一个内部bean,那么containingBd则是其外层bean的定义。
  2. 现在来对mbd进行赋值,如果bd的parentName为null,那就将bd包装成一个RootBeanDefinition,并赋值给mbd。如果parentName不为null,那就需要考虑到父子级联容器的处理了,判断此beanName对应的beanDefinition存在于当前容器中还是父容器中,并从对应的容器中的得到parentName进行merge之后的BeanDefinition,并包装成一个RootBeanDefinition,赋值给mbd。
  3. 给mbd的scope赋值,默认赋值SCOPE_SINGLETON,如果外层bean定义不为null,且其scope不是单例,但是这个bean又是单例,则将mbd的scope设置为跟外层bean的scope一致。
  4. 将合并结果存入mergedBeanDefinitions缓存。
  5. 复制属性到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去判断是否应该执行BeanPostProcessorpostProcessAfterInitialization方法。这里在对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后置处理器,那么就调用InstantiationAwareBeanPostProcessorpostProcessBeforeInstantiation方法得到其返回的bean,如果有多个InstantiationAwareBeanPostProcessor,那么取第一个有返回的值。再调用beanPostProcessors这个Map中的BeanPostProcessorpostProcessAfterInitialization方法对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实现了BeanNameAwareBeanClassLoaderAwareBeanFactoryAware接口,那么就会分别回调其实现的方法。
  • 执行BeanPostProcessorpostProcessBeforeInitialization方法。
  • 如果bean实现了InitializingBean接口,那么就会回调其afterPropertiesSet方法。如果指定了init方法,则用反射执行。
  • 执行BeanPostProcessorpostProcessAfterInitialization方法。

实例化,初始化完成之后,就需要对之前曝光的操作做处理了。

   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进行处理。


http://www.ppmy.cn/news/463970.html

相关文章

不分手,好吗?

你提出分手&#xff0c;显得那么平静&#xff0c;不带一丝惋惜。  当你来到我的身旁&#xff0c;我发现&#xff0c;平静的你是如此的美丽&#xff01;真的想抱住你&#xff0c;吻你……但我不能&#xff01;  你知道吗&#xff0c;你的这份美丽让我凭添了许多痛苦&#xf…

这个, ..., 男方也太背了吧, 尽碰上极品女方...

发信人: jungleford (風淸揚╬孤城斩菜羊), 信区: Joke 标 题: 我生活在上海&#xff0c;我相亲无数次&#xff0c;那些极品女方父母 (转载) 发信站: 水木社区 (Sun Aug 22 20:58:53 2010), 站内 【 以下文字转载自 Shanghai 讨论区 】 发信人: adi (杨过), 信区: Shanghai 标…

spring的beanfactory与applicationContext的区别以及继承关系

applicationContext继承关系 首先可以看一张图 ListableBeanFactory 可列举的bean工厂 hierarchical 分层bean工厂 messageSource 国际化信息 //国际化&#xff08;internationalization&#xff09;是设计和…

从分手厨房看拓扑排序

点击上方 "程序员小乐"关注, 星标或置顶一起成长 每天凌晨00点00分, 第一时间与你相约 每日英文 Dont judge people by their outlook for you dont know what stories behind their eyes. 不要以貌取人&#xff0c;因为你不知道他们的双眼后面藏着什么故事。 每日掏…

分手厨房2联机得买两份吗_你在厨房里表现出色吗?

分手厨房2联机得买两份吗 These days, I dont have to cook too often, but company is coming tonight. So, I dug out a slow cooker recipe, and got ready to brown the meat. 这些天&#xff0c;我不必经常做饭&#xff0c;但是公司今晚就要来了。 因此&#xff0c;我挖出…

掌握Spring Cloud:打造高效可靠的微服务生态系统

1、SpringCloud概述 Spring Cloud是一个用于构建分布式系统的开源框架&#xff0c;它提供了一系列的组件和工具&#xff0c;用于实现微服务架构中的各项核心功能。本文将重点介绍Spring Cloud中的关键组件&#xff0c;并详细探讨它们的功能和作用。 网关&#xff1a;Zuul/Gat…

如何利用数据化营销助力新零售企业发展?”

​“新零售”这个概念诞生至今已有5年&#xff0c;但对于其具体的定义&#xff0c;行业内仍然有许多争议。有人认为“新零售”是对传统零售模式的颠覆&#xff1b;也有人认为“新零售”就是将线上和线下相结合。不论如何&#xff0c;在这个不断变化的行业中&#xff0c;新零售企…

各品牌服务器的默认带外BMC管理地址

连接网段到sugon服务器的带外BMC管理端&#xff0c;在本地网络的属性中&#xff0c;选择ipv4更改使用固定IP地址10.0.0.5和默认的10.0.0.10为同一个网段&#xff0c; 然后在浏览器输入默认的地址:10.0.0.10 sugon TC6600 默认的带外管理地址 10.0.0.10 帐号&#xff1a;adm…