Spring (eight) core container - Bean creation process

Spring version 5.0.7.RELEASE

Get Bean method is getBean, its AbstractAutowireCapableBeanFactory abstract class inherits from the BeanFactory inherited AbstractBeanFactory abstract class.

1, the overall process

public abstract class AbstractBeanFactory extends FactoryBeanRegistrySupport implements ConfigurableBeanFactory {
    
    ...
    // 通过 beanName 获取 Bean 实例
    @Override
	public Object getBean(String name) throws BeansException {
		return doGetBean(name, null, null, false);
	}

    // 检索所需的 bean 类型
	@Override
	public <T> T getBean(String name, @Nullable Class<T> requiredType) throws BeansException {
		return doGetBean(name, requiredType, null, false);
	}

    // 使用显式参数创建 Bean 实例时要使用的参数
	@Override
	public Object getBean(String name, Object... args) throws BeansException {
		return doGetBean(name, null, args, false);
	}
	
    ...
}

getBean plurality overloaded methods, or can be divided by Bean name Bean object acquired by Class, these methods are invoked doGetBean reload the underlying methods.

protected <T> T doGetBean(final String name, @Nullable final Class<T> requiredType,
			@Nullable final Object[] args, boolean typeCheckOnly) throws BeansException {

    /**
	 * 1、如果获取的 Bean 类型是 FactoryBean,则参数 name 会以“&”为前缀。这里会去掉该修饰符,并返回.
	 * 2、如果是手动注册的别名,则将其解析为规范的名称
	 */
	final String beanName = transformedBeanName(name);
	Object bean;

    /** 
     * 1、单例 Bean 在 Spring 的同一个容器内只会被创建一次,后续再获取 Bean,直接从单例缓存中获取
     * 2、这里先从单例 Bean 的缓存容器中,尝试获取目标 Bean 
     * ( getSingleton 方法中存在解决单例 Bean 循环依赖问题的具体方案,这部分会在后面的章节详细讨论)
     */ 
	Object sharedInstance = getSingleton(beanName);
	
	// 如果存在目标 Bean
    if (sharedInstance != null && args == null) {
        if (logger.isDebugEnabled()) {
        
            // 目标 Bean 是否正在被创建
            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 + "'");
            }
        }

        // 1、不论是单例还是原型的实例对象,最终都要通过 getObjectForBeanInstance 进行转换,最终得到的才是符合要求的bean实例。
        // 2、有时候存在如 FactoryBean 这种并不是直接返回实例本身,而是返回指定方法返回的实例
        bean = getObjectForBeanInstance(sharedInstance, name, beanName, null);
    }

    // 如果不存在目标 Bean
    else {
    
        // 如果当前正在创建原型 Bean,则处于循环依赖中,且原型 Bean 无法解决循环依赖,所以抛出异常
        if (isPrototypeCurrentlyInCreation(beanName)) {
            throw new BeanCurrentlyInCreationException(beanName);
        }

        // 如果 beanDefinitionMap 也就是容器中不存在目标 Bean,则尝试从父级 beanFactory 中获取
        BeanFactory parentBeanFactory = getParentBeanFactory();
        if (parentBeanFactory != null && !containsBeanDefinition(beanName)) {
            // 获取真正 beanName。如果获取的 Bean 类型是 FactoryBean,则去掉 beanName 的修饰符“&”
            String nameToLookup = originalBeanName(name);
            if (parentBeanFactory instanceof AbstractBeanFactory) {
                return ((AbstractBeanFactory) parentBeanFactory).doGetBean(
                        nameToLookup, requiredType, args, typeCheckOnly);
            }
            else if (args != null) {
                // 递归到 BeanFactory 中寻找
                return (T) parentBeanFactory.getBean(nameToLookup, args);
            }
            else {
                // No args -> delegate to standard getBean method.
                return parentBeanFactory.getBean(nameToLookup, requiredType);
            }
        }

        if (!typeCheckOnly) {
            // 将指定的 bean 标记为已经创建(或将要创建),即将 beanName 加入 alreadyCreated 集合中
            markBeanAsCreated(beanName);
        }

        try {
            // 通过 beanName 获取对应的 BeanDefinition,如果获取 BeanDefinition 是子类 BeanDefinition,
            // 则通过与父级合并,返回目标 bean 的 RootBeanDefinition
            final RootBeanDefinition mbd = getMergedLocalBeanDefinition(beanName);
            // 检查bean是否是抽象的,如果是则抛出异常
            checkMergedBeanDefinition(mbd, beanName, args);

            // 获取目标 bean 所依赖的其它 bean 名称
            String[] dependsOn = mbd.getDependsOn();
            if (dependsOn != null) {
                // 若存在依赖则需要递归实例化依赖的 bean
                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);
                    }
                }
            }

            /* 开始创建目标 bean 实例,根据 bean 的 scope  执行不同的创建方式。单例,原型,其他的scope */

            // 这是单例的 bean 创建方式
            if (mbd.isSingleton()) {
                sharedInstance = getSingleton(beanName, () -> {
                    try {
                        // 创建 Bean 的核心方法
                        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;
                    }
                });
                bean = getObjectForBeanInstance(sharedInstance, name, beanName, mbd);
            }

            // prototype 类型的 bean 创建方式
            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);
                }
                bean = getObjectForBeanInstance(prototypeInstance, name, beanName, mbd);
            }

            // 其它类型
            else {
                String scopeName = mbd.getScope();
                final 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);
                        }
                    });
                    bean = getObjectForBeanInstance(scopedInstance, name, beanName, mbd);
                }
                catch (IllegalStateException ex) {
                    throw new BeanCreationException(beanName,
                            "Scope '" + scopeName + "' is not active for the current thread; consider " +
                            "defining a scoped proxy for this bean if you intend to refer to it from a singleton",
                            ex);
                }
            }
        }
        catch (BeansException ex) {
            cleanupAfterBeanCreationFailure(beanName);
            throw ex;
        }
    }

    // 将 Bean 的类型转换为 getBean 时指定的 requireType 类型
    if (requiredType != null && !requiredType.isInstance(bean)) {
        try {
            T convertedBean = getTypeConverter().convertIfNecessary(bean, requiredType);
            if (convertedBean == null) {
                throw new BeanNotOfRequiredTypeException(name, requiredType, bean.getClass());
            }
            return convertedBean;
        }
        catch (TypeMismatchException ex) {
            if (logger.isDebugEnabled()) {
                logger.debug("Failed to convert bean '" + name + "' to required type '" +
                        ClassUtils.getQualifiedName(requiredType) + "'", ex);
            }
            throw new BeanNotOfRequiredTypeException(name, requiredType, bean.getClass());
        }
    }
    return (T) bean;
}

Throughout method can be summarized as:

  1. 解析 beanName
  2. Bean container from the cache singleton instance, Bean attempts to acquire the target, if present, the last step is directly performed, the Bean requireType type conversion specified type is the getBean, then returns
  3. If not, then enter Bean instance creation process
  4. If you are currently creating prototype Bean instance, in circular dependencies, and the prototype Bean unable to resolve circular dependencies, so throw an exception
  5. If the current BeanFactory BeanDefinition target does not exist, acquired from the parent BeanFactory
  6. BeanDefinition acquisition target, if the acquired BeanDefinition BeanDefinition subclasses (such as GenericBeanDefinition), by combining with the parent, return target bean RootBeanDefinition
  7. If there is a dependency of the Bean, the first of these examples rely Bean
  8. Bean scope depending on the current, start an instance of Bean, singleton or prototype
  9. Examples of determining whether the type of the Bean is FactoryBean
  10. The Bean type conversion requireType specified type is getBean
  11. Finally, return Bean instance

These are the general process getBean process in which there are two very important methods and frequent, is a process of getObjectForBeanInstance FactoryBean method, the other is to create a core Bean implementation createBean method.

2, core processes

2.1 parsing FactoryBean

About FactoryBean on a "Spring (seven) core container - hook Interface" article has been discussed, FactoryBean provided by Spring hook interface that belongs to a special Bean, unlike ordinary Bean, which is used to create Bean instance, belongs to the Bean factory, but it's common to create different, it provides a more flexible way, those generally used to create the creation of more complex Bean.

The FactoryBean is parsed by getObjectForBeanlnstance. getObjectForBeanlnstance is a high frequency of use of the method, whether it is to obtain bean or bean to load depending on the scope strategies from the cache. In short, we get the first step is to call this method after the bean instance to be done to detect what correctness, in fact, used to detect whether the current bean is FactoryBean type of bean, if it is, then you need to call an instance of the bean corresponding FactoryBean the getObject () method returns a true value is returned as an object.

So, when we getBean, there are two possibilities, one is the return to normal of Bean, the other is a return to parse Bean FactoryBean returned by the getObjectForBeanInstance method.

protected Object getObjectForBeanInstance(
			Object beanInstance, String name, String beanName, @Nullable RootBeanDefinition mbd) {

	// 如果 beanName 以“&”为前缀,但对应的 bean 不是 FactoryBean 类型,则抛异常
	if (BeanFactoryUtils.isFactoryDereference(name)) {
		if (beanInstance instanceof NullBean) {
			return beanInstance;
		}
		if (!(beanInstance instanceof FactoryBean)) {
			throw new BeanIsNotAFactoryException(transformedBeanName(name), beanInstance.getClass());
		}
	}

	// 校验已获取的 bean 
	// 1、如果该 bean 不是 FactoryBean 类型,直接返回
	// 2、如果是 FactoryBean 类型,且 beanName 以“&”为前缀,说明想获取的是 FactoryBean ,也是直接返回
	if (!(beanInstance instanceof FactoryBean) || BeanFactoryUtils.isFactoryDereference(name)) {
		return beanInstance;
	}

    // 从缓存中尝试获取 FactoryBean 创建的对象
	Object object = null;
	if (mbd == null) {
		object = getCachedObjectForFactoryBean(beanName);
	}
	if (object == null) {
		// 到这里已确定 beanInstance 一定是 FactoryBean 类型,所以进行强转
		FactoryBean<?> factory = (FactoryBean<?>) beanInstance;
		// 获取 bean 对应的 BeanDefinition
		if (mbd == null && containsBeanDefinition(beanName)) {
			mbd = getMergedLocalBeanDefinition(beanName);
		}
		// 当前 bean 是否是用户定义的,而不是应用程序自己定义的
		boolean synthetic = (mbd != null && mbd.isSynthetic());
		
		// 解析 FactoryBean 的核心方法
		object = getObjectFromFactoryBean(factory, beanName, !synthetic);
	}
	return object;
}

This method is very simple, most of these ancillary code as well as some of the functionality of the judgment, the real core code getObjectFromFactoryBean method.

protected Object getObjectFromFactoryBean(FactoryBean<?> factory, String beanName, boolean shouldPostProcess) {
    // 如果是单例 bean 
	if (factory.isSingleton() && containsSingleton(beanName)) {
		synchronized (getSingletonMutex()) {
		    // 尝试从缓存中获取,缓存中存储的是已经通过 FactoryBean 创建的 bean 
			Object object = this.factoryBeanObjectCache.get(beanName);
			if (object == null) {
			
			    // 通过 FactoryBean 创建真正的 bean 
				object = doGetObjectFromFactoryBean(factory, beanName);
				
				// 这里大概是 在执行上一步 doGetObjectFromFactoryBean 方法过程中,该 bean 已被其它线程创建并缓存了起来
				Object alreadyThere = this.factoryBeanObjectCache.get(beanName);
				if (alreadyThere != null) {
					object = alreadyThere;
				}
				else { // 如果没有
					if (shouldPostProcess) { // Bean 是否要进行后置处理
						...
						
						try {
						    // 执行后置处理器(关于后置处理器已在上篇文章讨论过)
							object = postProcessObjectFromFactoryBean(object, beanName);
						}
						
						// catch ...
					}
					...
				}
			}
			return object;
		}
	}
	else { // 如果不是单例 bean 
	    // 通过 FactoryBean 创建真正的 bean 
		Object object = doGetObjectFromFactoryBean(factory, beanName);
		if (shouldPostProcess) {
			try {
			    // 执行后置处理器
				object = postProcessObjectFromFactoryBean(object, beanName);
			}
			// catch ...
		}
		return object;
	}
}

The method then enters a more central doGetObjectFromFactoryBean method:

private Object doGetObjectFromFactoryBean(final FactoryBean<?> factory, final String beanName)
			throws BeanCreationException {

	Object object;
	try {
		// try...catch...
		
		else {
		    // 调用 FactoryBean 的 getObject 方法,返回真正的 bean
			object = factory.getObject();
		}
	}
	
	// try...catch...

	...
	
	return object;
}

We can see, the last call is FactoryBean.getObject method

public interface FactoryBean<T> {
	@Nullable
	T getObject() throws Exception;
}

When a bean instantiation process is complex, may be, will be implemented through the return FactoryBean interface defines a logical instance of the bean and the method getObject overridden, after obtaining the bean by calling methods getObject. It is noteworthy that the underlying mybatis is achieved by FactoryBean.

2.2 From the beginning createBean

Then the next step createBean way to create the Bean:

protected Object createBean(String beanName, RootBeanDefinition mbd, @Nullable Object[] args)
			throws BeanCreationException {

	if (logger.isDebugEnabled()) {
		logger.debug("Creating instance of bean '" + beanName + "'");
	}
	RootBeanDefinition mbdToUse = mbd;

	// 根据设置的 class 属性或 className 来解析得到 Class 引用
	if (resolvedClass != null && !mbd.hasBeanClass() && mbd.getBeanClassName() != null) {
		mbdToUse = new RootBeanDefinition(mbd);
		mbdToUse.setBeanClass(resolvedClass);
	}

	// 对 override 属性进行标记和验证,本质上是处理 lookup-method 和 replaced-method 标签
	try {
		mbdToUse.prepareMethodOverrides();
	}
	// catch...

	try {
		// 执行 BeanPostProcessors 后置处理器,如果有 bean 返回,则不执行接下来创建 bean 的操作,直接返回该 bean 
		Object bean = resolveBeforeInstantiation(beanName, mbdToUse);
		if (bean != null) {
			return bean;
		}
	}
	// catch...

	try {
	    // 创建 Bean 的核心方法
		Object beanInstance = doCreateBean(beanName, mbdToUse, args);
		if (logger.isDebugEnabled()) {
			logger.debug("Finished creating instance of bean '" + beanName + "'");
		}
		return beanInstance;
	}
	// catch...
}

DoCreateBean continue into the method:

protected Object doCreateBean(final String beanName, final RootBeanDefinition mbd, final @Nullable Object[] args)
			throws BeanCreationException {

	// BeanWrapper 包装了 bean 对象,缓存了 bean 的内省结果,并可以访问 bean 的属性、设置 bean 的属性值
	BeanWrapper instanceWrapper = null;
	// 如果是单例,尝试获取对应的 BeanWrapper
	if (mbd.isSingleton()) {
		instanceWrapper = this.factoryBeanInstanceCache.remove(beanName);
	}
	if (instanceWrapper == null) {
	    /*
         * 说明对应的bean还没有创建,用对应的策略(工厂方法、构造函数)创建 bean 实例,以及简单初始化
         *
         * 将 beanDefinition 转成 BeanWrapper,大致流程如下:
         * 1. 如果存在工厂方法,则使用工厂方法初始化
         * 2. 否则,如果存在多个构造函数,则根据参数确定构造函数,并利用构造函数初始化
         * 3. 否则,使用默认构造函数初始化
         */
		instanceWrapper = createBeanInstance(beanName, mbd, args);
	}
	// 从 BeanWrapper 中获取包装的 bean 实例
	final Object bean = instanceWrapper.getWrappedInstance();
	// 从 BeanWrapper 获取包装 bean 的 class 引用
	Class<?> beanType = instanceWrapper.getWrappedClass();
	if (beanType != NullBean.class) {
		mbd.resolvedTargetType = beanType;
	}

	// 执行 MergedBeanDefinitionPostProcessor 后置处理器。
	//(这里涉及一个极其重要的后置处理器实现 AutowiredAnnotationBeanPostProcessor,其主要用来处理 @Autowired 注解,这部分会在后面详细讨论)
	synchronized (mbd.postProcessingLock) {
		if (!mbd.postProcessed) {
			try {
				applyMergedBeanDefinitionPostProcessors(mbd, beanType, beanName);
			}
			// catch...
			mbd.postProcessed = true;
		}
	}

	//  检查是否需要提前曝光,避免循环依赖(循环依赖问题会在后面详细讨论)
	boolean earlySingletonExposure = (mbd.isSingleton() && this.allowCircularReferences &&
			isSingletonCurrentlyInCreation(beanName));
	if (earlySingletonExposure) {
		if (logger.isDebugEnabled()) {
			logger.debug("Eagerly caching bean '" + beanName +
					"' to allow for resolving potential circular references");
		}
		addSingletonFactory(beanName, () -> getEarlyBeanReference(beanName, mbd, bean));
	}

	// 开始初始化 bean 
	Object exposedObject = bean;
	try {
	    // 对 bean 进行填充,将各个属性值注入,如果存在依赖的 bean 则进行递归初始化
		populateBean(beanName, mbd, instanceWrapper);
		// 执行一系列的初始化方法
		exposedObject = initializeBean(beanName, exposedObject, mbd);
	}
	// catch...

    // 再次检查是否循环依赖
	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 
				}
			}
		}
	}

	// 注册DisposableBean
	try {
		registerDisposableBeanIfNecessary(beanName, bean, mbd);
	}
	// catch...

	return exposedObject;
}

The overall process for the method:

  1. If single embodiment, tries to obtain BeanWrapper wrapper bean from the cache, the cache and clears
  2. If there is no corresponding to the Wrapper, then the bean not instantiated, the instance created bean
  3. Post processor performs MergedBeanDefinitionPostProcessor
  4. Check that the exposure in advance, to avoid circular dependencies
  5. Filling property, filling property to all instances of the bean.
  6. Performs a series of initialization methods (hook callback interfaces)
  7. Check again if there is a circular dependency
  8. Registration DisposableBean

There are several ways this process needs to focus on, namely the creation createBeaninstance Bean instance method, injection method populateBean Bean properties and methods to perform initializeBean Bean initialization method.

2.2.1 Creating Bean instance

From the beginning createBeaninstance:

protected BeanWrapper createBeanInstance(String beanName, RootBeanDefinition mbd, @Nullable Object[] args) {
	Class<?> beanClass = resolveBeanClass(mbd, beanName);
	if (beanClass != null && !Modifier.isPublic(beanClass.getModifiers()) && !mbd.isNonPublicAccessAllowed()) {
		throw new BeanCreationException(mbd.getResourceDescription(), beanName,
				"Bean class isn't public, and non-public access not allowed: " + beanClass.getName());
	}

    // 如果有用于创建 bean 实例的回调方法
	Supplier<?> instanceSupplier = mbd.getInstanceSupplier();
	if (instanceSupplier != null) {
		return obtainFromSupplier(instanceSupplier, beanName);
	}
    
    // 如果工厂方法不为空,则使用工厂方法进行实例化
	if (mbd.getFactoryMethodName() != null)  {
		return instantiateUsingFactoryMethod(beanName, mbd, args);
	}

	// 利用构造函数进行实例化,解析并确定目标构造函数
	boolean resolved = false;
	boolean autowireNecessary = false;
	if (args == null) {
		synchronized (mbd.constructorArgumentLock) {
		    // 一个类可能有多个构造函数,需要根据参数来确定具体的构造函数
			if (mbd.resolvedConstructorOrFactoryMethod != null) {
				resolved = true;
				autowireNecessary = mbd.constructorArgumentsResolved;
			}
		}
	}
	
	// 如果已经解析过,则使用已经确定的构造方法
	if (resolved) {
		if (autowireNecessary) {
		    // 依据构造函数注入
			return autowireConstructor(beanName, mbd, null, null);
		}
		else {
		    // 使用默认构造函数构造
			return instantiateBean(beanName, mbd);
		}
	}

	// 根据参数确定构造函数
	Constructor<?>[] ctors = determineConstructorsFromBeanPostProcessors(beanClass, beanName);
	if (ctors != null ||
			mbd.getResolvedAutowireMode() == RootBeanDefinition.AUTOWIRE_CONSTRUCTOR ||
			mbd.hasConstructorArgumentValues() || !ObjectUtils.isEmpty(args))  {
		return autowireConstructor(beanName, mbd, ctors, args);
	}

	// 使用默认的构造函数
	return instantiateBean(beanName, mbd);
}

Above is divided into:

  1. Examples of a method for using the plant
  2. By constructing instantiates

Whether through the factory method is configured to instantiate objects, to obtained here is only the first instance of a bean, not our final desired bean, since the latter need to be initialized on the bean instance, injection corresponding attribute value Wait.

Initialization 2.2.2 Bean instance - Property injection

Properties achieved by the injection method populateBean:

protected void populateBean(String beanName, RootBeanDefinition mbd, @Nullable BeanWrapper bw) {
    // 判断实例化的 bean 是否为空
	if (bw == null) {
	    // 返回是否有为此 bean 定义的属性值,如果有,则抛异常,提示 “无法将属性值应用于空实例”
		if (mbd.hasPropertyValues()) {
			throw new BeanCreationException(
					mbd.getResourceDescription(), beanName, "Cannot apply property values to null instance");
		}
		// 没有,则跳过属性填充阶段以获取空实例
		else {
			// Skip property population phase for null instance.
			return;
		}
	}

	// 在设置属性之前,先执行 InstantiationAwareBeanPostProcessors 后置处理器,这些后置处理器可以用其它方式注入属性,如字段注入。
	boolean continueWithPropertyPopulation = true;
	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;
				}
			}
		}
	}
    // 当使用了 InstantiationAwareBeanPostProcessors 后置处理器注入属性,则结束属性注入流程,直接返回
	if (!continueWithPropertyPopulation) {
		return;
	}

    // 获取 bean 实例的属性值集合
	PropertyValues pvs = (mbd.hasPropertyValues() ? mbd.getPropertyValues() : null);

	if (mbd.getResolvedAutowireMode() == RootBeanDefinition.AUTOWIRE_BY_NAME ||
			mbd.getResolvedAutowireMode() == RootBeanDefinition.AUTOWIRE_BY_TYPE) {
		MutablePropertyValues newPvs = new MutablePropertyValues(pvs);

		// 根据名称自动注入
		if (mbd.getResolvedAutowireMode() == RootBeanDefinition.AUTOWIRE_BY_NAME) {
			autowireByName(beanName, mbd, bw, newPvs);
		}

		// 根据类型自动注入
		if (mbd.getResolvedAutowireMode() == RootBeanDefinition.AUTOWIRE_BY_TYPE) {
			autowireByType(beanName, mbd, bw, newPvs);
		}

		pvs = newPvs;
	}

    // 返回此工厂是否拥有 InstantiationAwareBeanPostProcessor
	boolean hasInstAwareBpps = hasInstantiationAwareBeanPostProcessors();
	// 是否进行依赖检查
	boolean needsDepCheck = (mbd.getDependencyCheck() != RootBeanDefinition.DEPENDENCY_CHECK_NONE);

	if (hasInstAwareBpps || needsDepCheck) {
		if (pvs == null) {
			pvs = mbd.getPropertyValues();
		}
		PropertyDescriptor[] filteredPds = filterPropertyDescriptorsForDependencyCheck(bw, mbd.allowCaching);
		// 在属性注入前执行 InstantiationAwareBeanPostProcessor 后置处理器
		if (hasInstAwareBpps) {
			for (BeanPostProcessor bp : getBeanPostProcessors()) {
				if (bp instanceof InstantiationAwareBeanPostProcessor) {
					InstantiationAwareBeanPostProcessor ibp = (InstantiationAwareBeanPostProcessor) bp;
					pvs = ibp.postProcessPropertyValues(pvs, filteredPds, bw.getWrappedInstance(), beanName);
					if (pvs == null) {
						return;
					}
				}
			}
		}
		// 进行依赖检查
		if (needsDepCheck) {
			checkDependencies(beanName, mbd, filteredPds, pvs);
		}
	}

	if (pvs != null) {
	    // 执行属性注入
		applyPropertyValues(beanName, mbd, bw, pvs);
	}
}

2.2.3 Initialization Bean instance - to perform initialization method (callback hooks Interface)

Then enter initializeBean method, in which the callback will be executed in a number of ways Bean initialization phase.

protected Object initializeBean(final String beanName, final Object bean, @Nullable RootBeanDefinition mbd) {

    // 回调 Aware 系列接口
	if (System.getSecurityManager() != null) {
		AccessController.doPrivileged((PrivilegedAction<Object>) () -> {
			invokeAwareMethods(beanName, bean);
			return null;
		}, getAccessControlContext());
	}
	else {
		invokeAwareMethods(beanName, bean);
	}

    // 回调 BeanPostProcessor 后置处理器的 postProcessBeforeInitialization 方法
	Object wrappedBean = bean;
	if (mbd == null || !mbd.isSynthetic()) {
		wrappedBean = applyBeanPostProcessorsBeforeInitialization(wrappedBean, beanName);
	}

	try {
	    // 回调 InitializingBean 的 afterPropertiesSet 方法
		invokeInitMethods(beanName, wrappedBean, mbd);
	}
	catch (Throwable ex) {
		throw new BeanCreationException(
				(mbd != null ? mbd.getResourceDescription() : null),
				beanName, "Invocation of init method failed", ex);
	}
	if (mbd == null || !mbd.isSynthetic()) {
	    // 回调 BeanPostProcessor 后置处理器的 postProcessAfterInitialization 方法
		wrappedBean = applyBeanPostProcessorsAfterInitialization(wrappedBean, beanName);
	}

	return wrappedBean;
}

It can be seen in the initialization phase of Bean, respectively callback Aware serial interface, BeanPostProcessor postprocessor, InitializingBean. The three interfaces are Spring hook interface is open Spring out of the expansion interfaces, it can affect Bean life cycle. Interface on a hook on the - has been discussed in detail in "Spring (seven) core container hook Interface" article, interested students can read on their own.

1, the callback series Aware Interface

private void invokeAwareMethods(final String beanName, final Object bean) {   
    
	if (bean instanceof Aware) {
	    // 如果当前 Bean 继承了 BeanNameAware 接口,则回调接口中的 setBeanName 方法,并传递 beanName 参数
		if (bean instanceof BeanNameAware) {
			((BeanNameAware) bean).setBeanName(beanName);
		}
		
		// 这个 BeanClassLoaderAware 接口传递的是 ClassLoader
		if (bean instanceof BeanClassLoaderAware) {
			ClassLoader bcl = getBeanClassLoader();
			if (bcl != null) {
				((BeanClassLoaderAware) bean).setBeanClassLoader(bcl);
			}
		}
		
		// 这个 BeanFactoryAware 接口传递的是 AbstractAutowireCapableBeanFactory
		if (bean instanceof BeanFactoryAware) {
			((BeanFactoryAware) bean).setBeanFactory(AbstractAutowireCapableBeanFactory.this);
		}
	}
}

Aware callback here has three interfaces, namely BeanNameAware, BeanClassLoaderAware, BeanFactoryAware.

2, the callback method postProcessBeforeInitialization BeanPostProcessor postprocessor

public Object applyBeanPostProcessorsBeforeInitialization(Object existingBean, String beanName)
			throws BeansException {

	Object result = existingBean;
	
	// getBeanPostProcessors 方法获取所有的 BeanPostProcessor 后置处理器的实现,并循环执行 postProcessBeforeInitialization 方法,参数是当前 Bean 以及 beanName 。
	for (BeanPostProcessor beanProcessor : getBeanPostProcessors()) {
		Object current = beanProcessor.postProcessBeforeInitialization(result, beanName);
		if (current == null) {
			return result;
		}
		result = current;
	}
	return result;
}

Wherein ApplicationContextAwareProcessor for realizing a post-processor, which is used for the remaining Aware interface callbacks:

@Override
public Object postProcessBeforeInitialization(final Object bean, String beanName) throws BeansException {
		AccessControlContext acc = null;

	//...
	
		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);
		}
	}
}

3, the callback method InitializingBean of afterPropertiesSet

protected void invokeInitMethods(String beanName, final Object bean, @Nullable RootBeanDefinition mbd)
			throws Throwable {

	//..
			((InitializingBean) bean).afterPropertiesSet();
	
	//..
}

4, the callback method postProcessAfterInitialization BeanPostProcessor postprocessor

@Override
public Object applyBeanPostProcessorsAfterInitialization(Object existingBean, String beanName)
		throws BeansException {

	Object result = existingBean;
	
	// getBeanPostProcessors 方法获取所有的 BeanPostProcessor 后置处理器的实现,并循环执行 postProcessAfterInitialization 方法,参数是当前 Bean 以及 beanName 。
	for (BeanPostProcessor beanProcessor : getBeanPostProcessors()) {
		Object current = beanProcessor.postProcessAfterInitialization(result, beanName);
		if (current == null) {
			return result;
		}
		result = current;
	}
	return result;
}

Here, create core processes Bean on the end of the discussion can be summarized as follows:

  1. 解析 beanName
  2. Bean container from the cache singleton instance, attempts to obtain the target Bean
  3. Cache does not exist start an instance of Bean, distinguish between single embodiment or a prototype
  4. Examples of Bean, initialization Bean, perform the initialization method callback hooks and interfaces
  5. Examples of determining whether the type of the Bean is FactoryBean







Reference "the Spring source depth analysis (2nd Edition)"

Guess you like

Origin www.cnblogs.com/loongk/p/12563304.html