Spring源码剖析-单利Bean的实例化(六)

前言

前面系列章节我们分析了Spring的IOC的启动流程,包括:容器创建,配置加载,配置解析,Bean注册等几个阶段,所以Bean注册其实就是把Bean的相关属性,依赖关系等封装成BeanDeafinition对象,然后注册到一个ConcurrentHashMap中。要注意的是这个BeanDeafinition只是对Bean的一个定义封装而已,并不是真正的Bean的实例,那Bean的实例是在什么时候创建的?有三种情况

  • 如果是单利Bean,且lazy-init=false 急切初始(即时,立即,迫切,饥饿都是一个意思)的情况,在IOC容器启动之后就会根据BeanDeafinition对Bean进行实例化。
  • 如果某个Bean还没被实例化,通过BeanFacotry.getBean 获取Bean的时候,就会触发Bean的实例化。
  • 通过自动注入的时候,会触发Bean的实例化

虽然有多种情况会触Bean的发实例化,但是实例化的过程都是一样的,这篇文章就讲一下IOC启动之后,单利Bean的实例化过程吧。

refresh() 单利Bean实例化

代码还得回到 AbstractApplicationContext#refresh 方法中,在《IOC启动流程》一文中我们对容器刷新refresh方法已经有过研究

@Override
   public void refresh() throws BeansException, IllegalStateException {
   	synchronized (this.startupShutdownMonitor) {
   		// Prepare this context for refreshing.
   		//准备刷新工作 ,记录开始时间,初始化属性,校验配置文件,准备事件的存储Set
   		prepareRefresh();

   		// Tell the subclass to refresh the internal bean factory.
   		//告诉子类,刷新Bean工厂,销毁旧beanFactory,创建新beanFactory,默认DefaultListableBeanFactory
   		//从子容器的refreshBeanFactory方法中载入Bean的资源文件
   		ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();
		
        ...省略...
            
        // Instantiate all remaining (non-lazy-init) singletons.
        //实例化所有剩余的(非延迟初始化)单例的Bean
        finishBeanFactoryInitialization(beanFactory);

   		...省略...
   }

在 obtainFreshBeanFactory方法中完成了Bean的资源文件加载,Bean的解析,BeanDefinition的注册等一些列过程。最终把BeanDefinition保存到了DefaultListableBeanFactory管理的一个ConcurrentHashMap中。

我们接下来要探讨的是finishBeanFactoryInitialization; 方法,它里面实现了急切初始化的单例的Bean的创建流程。

/**
	 * Finish the initialization of this context's bean factory,
	 * initializing all remaining singleton beans.
	 * 完成此上下文的 bean 工厂的初始化,初始化所有剩余的单例 bean。
	 */
	protected void finishBeanFactoryInitialization(ConfigurableListableBeanFactory beanFactory) {
		// Initialize conversion service for this context.
		//用于类型转换的服务接口
		if (beanFactory.containsBean(CONVERSION_SERVICE_BEAN_NAME) &&
				beanFactory.isTypeMatch(CONVERSION_SERVICE_BEAN_NAME, ConversionService.class)) {
			beanFactory.setConversionService(
					beanFactory.getBean(CONVERSION_SERVICE_BEAN_NAME, ConversionService.class));
		}

		// Register a default embedded value resolver if no bean post-processor
		// (such as a PropertyPlaceholderConfigurer bean) registered any before:
		// at this point, primarily for resolution in annotation attribute values.
		if (!beanFactory.hasEmbeddedValueResolver()) {
			beanFactory.addEmbeddedValueResolver(strVal -> getEnvironment().resolvePlaceholders(strVal));
		}

		// Initialize LoadTimeWeaverAware beans early to allow for registering their transformers early.
		String[] weaverAwareNames = beanFactory.getBeanNamesForType(LoadTimeWeaverAware.class, false, false);
		for (String weaverAwareName : weaverAwareNames) {
			getBean(weaverAwareName);
		}

		// Stop using the temporary ClassLoader for type matching.
		//停止使用临时类加载器进行类型匹配
		beanFactory.setTempClassLoader(null);

		// Allow for caching all bean definition metadata, not expecting further changes.
		//允许缓存所有 beandefinition 元数据,防止更改
		beanFactory.freezeConfiguration();

		// Instantiate all remaining (non-lazy-init) singletons.
		//[重要]入口在这 ,实例化 lazy-init= false的单利Bean
		beanFactory.preInstantiateSingletons();
	}

这里初始化Bean Factory后,调用 beanFactory.preInstantiateSingletons 方法实例化 lazy-init= false的单利Bean, DefaultListableBeanFactory#preInstantiateSingletons源码如下:

@Override
	public void preInstantiateSingletons() throws BeansException {
		if (logger.isTraceEnabled()) {
			logger.trace("Pre-instantiating singletons in " + this);
		}

		// Iterate over a copy to allow for init methods which in turn register new bean definitions.
		// While this may not be part of the regular factory bootstrap, it does otherwise work fine.
		//获取到所有Bean的名字
		List<String> beanNames = new ArrayList<>(this.beanDefinitionNames);

		// Trigger initialization of all non-lazy singleton beans...
		for (String beanName : beanNames) {
			//获取Bean的BeanDefinition 定义对象
			RootBeanDefinition bd = getMergedLocalBeanDefinition(beanName);
			//如果不是抽象类,如果是单利,如果lazyInit为false,触发实例化逻辑
			if (!bd.isAbstract() && bd.isSingleton() && !bd.isLazyInit()) {
				//判断是否是FactoryBean
				if (isFactoryBean(beanName)) {
					//调用 getBean 方法得到FactoryBean工厂类实例。
					//【重要】getBean方法是实例化Bean的核心,后面我们要重点分析这个方法
					Object bean = getBean(FACTORY_BEAN_PREFIX + beanName);
					//如果是FactoryBean
					if (bean instanceof FactoryBean) {
						FactoryBean<?> factory = (FactoryBean<?>) bean;
						//是否要马上初始化
						boolean isEagerInit;
						if (System.getSecurityManager() != null && factory instanceof SmartFactoryBean) {
							isEagerInit = AccessController.doPrivileged(
									(PrivilegedAction<Boolean>) ((SmartFactoryBean<?>) factory)::isEagerInit,
									getAccessControlContext());
						}
						else {
							isEagerInit = (factory instanceof SmartFactoryBean &&
									((SmartFactoryBean<?>) factory).isEagerInit());
						}
						if (isEagerInit) {
							//【重要】 如果需要马上实例化,就调用getBean方法进行Bean的实例化
							getBean(beanName);
						}
					}
				}
				else {
					//【重要】 如果是普通的单利Bean,就调用getBean方法进行Bean的实例化
					getBean(beanName);
				}
			}
		}

		// Trigger post-initialization callback for all applicable beans...
		// 触发所有的Bean的生命周期方法的 init初始化方法.
		for (String beanName : beanNames) {
			//获取到单利Bean的实例
			Object singletonInstance = getSingleton(beanName);
			//判断Bean有没有实现 SmartInitializingSingleton接口,SmartInitializingSingleton是针对单利Bean的初始化接口
			if (singletonInstance instanceof SmartInitializingSingleton) {
				SmartInitializingSingleton smartSingleton = (SmartInitializingSingleton) singletonInstance;
				if (System.getSecurityManager() != null) {
					AccessController.doPrivileged((PrivilegedAction<Object>) () -> {
						smartSingleton.afterSingletonsInstantiated();
						return null;
					}, getAccessControlContext());
				}
				else {
					//触发afterSingletonsInstantiated方法的调用
					smartSingleton.afterSingletonsInstantiated();
				}
			}
		}
	}

该方法中先是获取到IOC容器注册的所有的Bean的名字,然后循环拿到每个Bean的BeanDefinition定义对象,然后做了三个判断:不能是抽象,必须是单利,必须是急切初始化, 如果满足条件再看是普通的Bean还是FactoryBean,最终都会调用 getBean(beanName); 方法,该方法调用 AbstractBeanFactory#doGetBean 该方法创建Bean。

创建好Bean之后,会判断Bean是否实现了 SmartInitializingSingleton 初始化接口,这个是针对单利Bean的初始化接口,然后调用afterSingletonsInstantiated进行Bean的初始化。

Bean实例化流程图

先上一个流程图,待会儿你可以根据这个图来看代码。
Spring源码剖析-单利Bean的实例化(六)_第1张图片

AbstractBeanFactory#doGetBean

protected <T> T doGetBean(
			String name, @Nullable Class<T> requiredType, @Nullable Object[] args, boolean typeCheckOnly)
			throws BeansException {
		//得到Bean的名字,如果指定的是别名,将别名转换为规范的 Bean 名称
		String beanName = transformedBeanName(name);
		Object bean;

		// Eagerly check singleton cache for manually registered singletons.
		//从缓存中获取单利的Bean,对于单利只会创建一次,创建好之后就会缓存到map中
		//默认情况下Bean都是迫切加载的,在容器启动的过程中就实例化好缓存到Map中
		Object sharedInstance = getSingleton(beanName);
		if (sharedInstance != null && args == null) {
			if (logger.isTraceEnabled()) {
				//判断bean是否在  singletonsCurrentlyInCreation set集合中,该集合中存储的是正在创建的单利Bean
				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 + "'");
				}
			}
			//获取 Bean 实例的对象,主要是完成 FactoryBean 的相关处理
			//即:naem是是以  $ 开头 ,就返回FactoryBean工厂
			bean = getObjectForBeanInstance(sharedInstance, name, beanName, null);
		}

		else {
			//代码走到这里,说明单利缓存的map中是没有正在创建的bean。

			// Fail if we're already creating this bean instance:
			// We're assumably within a circular reference.
			//如果缓存中没有单利Bean,但是缓存中有一个多利的Bean,目前正在创建,由于循环依赖问题,抛出异常
			if (isPrototypeCurrentlyInCreation(beanName)) {
				throw new BeanCurrentlyInCreationException(beanName);
			}

			// Check if bean definition exists in this factory.
			//得到容器IOC工厂
			BeanFactory parentBeanFactory = getParentBeanFactory();
			//通过beanName检查,如果容器中不包含这个Bean
			//如果容器中没有Bean,沿着继承体系交给父级去找
			if (parentBeanFactory != null && !containsBeanDefinition(beanName)) {
				// Not found -> check parent.
				//得到Bean的原始名字,将别名解析为规范名称
				String nameToLookup = originalBeanName(name);
				if (parentBeanFactory instanceof AbstractBeanFactory) {
					//根据名字,类型,参数找Bean
					return ((AbstractBeanFactory) parentBeanFactory).doGetBean(
							nameToLookup, requiredType, args, typeCheckOnly);
				}
				else if (args != null) {
					//委托父工厂去找Bean , 根据名字和参数
					// Delegation to parent with explicit args.
					return (T) parentBeanFactory.getBean(nameToLookup, args);
				}
				else if (requiredType != null) {
					// 无参数 -> 委托给标准的 getBean 方法,根据名字和类型
					// No args -> delegate to standard getBean method.
					return parentBeanFactory.getBean(nameToLookup, requiredType);
				}
				else {
					//根据名字找Bean
					return (T) parentBeanFactory.getBean(nameToLookup);
				}
			}
			//创建Bean是否要类型检查
			if (!typeCheckOnly) {
				//标记Bean被创建
				markBeanAsCreated(beanName);
			}

			try {
				//合并Bean,主要解决Bean继承时子类和父类的公共属性
				RootBeanDefinition mbd = getMergedLocalBeanDefinition(beanName);
				checkMergedBeanDefinition(mbd, beanName, args);

				// Guarantee initialization of beans that the current bean depends on.
				//保证当前 bean 所依赖的 bean 的初始化。
				String[] dependsOn = mbd.getDependsOn();
				//如果当前Bean依赖了其他Bean
				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 + "'");
						}
						//为给定的 bean 注册一个依赖 bean
						registerDependentBean(dep, beanName);
						try {
							//递归,从容器中获取依赖的Bean
							getBean(dep);
						}
						catch (NoSuchBeanDefinitionException ex) {
							throw new BeanCreationException(mbd.getResourceDescription(), beanName,
									"'" + beanName + "' depends on missing bean '" + dep + "'", ex);
						}
					}
				}

				// Create bean instance.
				//创建单利Bean
				if (mbd.isSingleton()) {
					//根据Bean名字获取单利Bean,这里是真正创建Bean的地方,通过ObjectFactory创建
                      //这里是匿名内部类
					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.
							//销毁实例,从缓存的map中清除单利Bean
							destroySingleton(beanName);
							throw ex;
						}
					});
					//返回bean实例,主要处理FactoryBean相关
					bean = getObjectForBeanInstance(sharedInstance, name, beanName, mbd);
				}
				//如果是多利
				else if (mbd.isPrototype()) {
					// It's a prototype -> create a new instance.
					//如果是prototype模式,每次都会创建新的实例
					Object prototypeInstance = null;
					try {
						beforePrototypeCreation(beanName);
						//创建指定 Bean 对象实例 ,如果是prototype模式,每次都会创建新的实例
						prototypeInstance = createBean(beanName, mbd, args);
					}
					finally {
						//默认实现将原型标记为不再创建
						afterPrototypeCreation(beanName);
					}
					//返回bean实例,主要处理FactoryBean相关
					bean = getObjectForBeanInstance(prototypeInstance, name, beanName, mbd);
				}

				else {
					String scopeName = mbd.getScope();
					//如果不是单利,不是多利,Bean没有 scope属性,则不合法
					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);
							}
						});
						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 实例的类型匹配。
		// Check if required type matches the type of the actual bean instance.
		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.isTraceEnabled()) {
					logger.trace("Failed to convert bean '" + name + "' to required type '" +
							ClassUtils.getQualifiedName(requiredType) + "'", ex);
				}
				throw new BeanNotOfRequiredTypeException(name, requiredType, bean.getClass());
			}
		}
		return (T) bean;
	}

上面这个方法中代码量有点多,这里做一下总结

  • transformedBeanName(name):转换Bean的名字,如果传入的name是别名需要找到真实的Bean的名字,如果传入的name是FactoryBean,比如“&bean” ,要去掉 “&”。

  • getSingleton(name):从缓存中查找Bean(ConcurrentHashMap中),单利Bean只会在容器中创建一次,后续都从缓存中加载,如果缓存中没有就就会进行创建。默认情况下Bean都是迫切创建的,即在容器启动的过程中就会根据BeaDefinition创建好Bean的实例存储到缓存中。
    在创建单利Bean的时候会存在依赖注入的情况,为了解决循环依赖(A依赖B,B依赖A),在创建Bean的时候会提前将ObjectFactory 加入缓存,如果下个Bean创建时需要依赖上一个Bean就会直接使用ObejctFactory(后面讲循环依赖)

  • createBean :Bean的实例化,从BeanDefinition的缓存中获取当前Bean的BeanDefinition定义对象,根据BeanDefinition进行Bean的创建。如果是通过工厂Bea 方式实例化,则需要得到工厂Bean中的factory-method方法返回的的Bean的实例,通过getObjectForBeanInstance 处理的。

  • isPrototypeCurrentlyInCreation:Prototype依赖检查,单利情况下才会解决循环依赖,如果A中依赖了B,B中依赖了A,在创建依赖的时候就会产生循环依赖。

  • 如果是多利Bean直接走createBean创建一个新的Bean

DefaultSingletonBeanRegistry#getSingleton(String, ObjectFactory)

对于 getSingleton从缓存中获取单利Bean的方法,这里有两个重载方法第一个方法是在 Object sharedInstance = getSingleton(beanName);
时候调用,源码如下:

//allowEarlyReference :是否创建早期应用,主要用来解决循环依赖
	@Nullable
	protected Object getSingleton(String beanName, boolean allowEarlyReference) {
		// Quick check for existing instance without full singleton lock
		//从Map中 singletonObjects = new ConcurrentHashMap<>(256); 获取单利Bean
		
		//singletonObject缓存中是否有Bean
		Object singletonObject = this.singletonObjects.get(beanName);
		//如果singletonObjects中没有Bean,但是 Set singletonsCurrentlyInCreation中有Bean,代表Bean正在创建
		if (singletonObject == null && isSingletonCurrentlyInCreation(beanName)) {
			//从早期单例对象的缓存 earlySingletonObjects 中获取
			singletonObject = this.earlySingletonObjects.get(beanName);
			if (singletonObject == null && allowEarlyReference) {
				synchronized (this.singletonObjects) {
					// Consistent creation of early reference within full singleton lock
					singletonObject = this.singletonObjects.get(beanName);
					if (singletonObject == null) {
						singletonObject = this.earlySingletonObjects.get(beanName);
						if (singletonObject == null) {
							//获取ObjectFactory , 对象创建工厂
							ObjectFactory<?> singletonFactory = this.singletonFactories.get(beanName);
							if (singletonFactory != null) {
								//获取对象实例
								singletonObject = singletonFactory.getObject();
								//把对象存储到缓存中
								this.earlySingletonObjects.put(beanName, singletonObject);
								this.singletonFactories.remove(beanName);
							}
						}
					}
				}
			}
		}
		return singletonObject;
	}

这个代码看着就晕,先理一下这几个缓存

  • singletonObjects:保存beanName和Bean的关系
  • singletonFactories:保存beanName和Bean的工厂之间的关系,ObjectFactory
  • earlySingletonObjects:存放beanName和Bean组件的关系,这个Map中的Bean代表正在创建中,用来检测循环依赖用的
  • registeredSingletons:保存所有已经注册成功的Bean

这个方法首先从 singletonObjects 获取Bean,如果没有就从earlySingletonObjects中获取正在创建的Bean,如果也没有就从singletonFactories中得到ObjectFactory,通过ObjectFactory.getObject 来创建Bean,并且放到earlySingletonObjects中。且把ObjectFactory从 singletonFactories移除。

如果上面 getSingleton 返回实例为空,就会走到 if (mbd.isSingleton()) 然后调用 另外一个 getSingleton方法 ,这个方法需要传入ObjectFactory,源码如下:

public Object getSingleton(String beanName, ObjectFactory<?> singletonFactory) {
		Assert.notNull(beanName, "Bean name must not be null");
		synchronized (this.singletonObjects) {
			//从singletonObjects获取Bean 
			Object singletonObject = this.singletonObjects.get(beanName);
			if (singletonObject == null) {
				...省略...
				//把 beanName 添加到 singletonsCurrentlyInCreation 中,用来做循环依赖检查
				beforeSingletonCreation(beanName);

				try {
					//通过工厂获取Bean
					singletonObject = singletonFactory.getObject();
					newSingleton = true;
				}
				...省略...
				if (newSingleton) {
					//把Bean添加到缓存
					addSingleton(beanName, singletonObject);
				}
			}
			return singletonObject;
		}
	}


	//注册到registeredSingletons
	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);
		}
	}

这里先从singletonObjects获取Bean,如果没有,就调用ObjectFactory.getObject方法创建Bean的实例,然后调用addSingletion 把Bean添加到 singletonObjects ,和registeredSingletons中。且从 singletonFactories和 earlySingletonObjects移除该Bean。

上面讲的都是走缓存获取Bean,当缓存没有Bean就会走 ObjectFactory.getObject 创建Bean。

AbstractAutowireCapableBeanFactory#createBean

创建Bean的工作是交给 ObjectFactory#createBean方法完成,代码中采用了匿名内部类的方式实现。而又把具体的实例化过程委派给 AbstractAutowireCapableBeanFactory#createBean 方法来完成

下面是 AbstractAutowireCapableBeanFactory#createBean 创建Bean方法源码

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

		...省略...

		try {
			//创建Bean【重要】
			Object beanInstance = doCreateBean(beanName, mbdToUse, args);
			if (logger.isTraceEnabled()) {
				logger.trace("Finished creating instance of bean '" + beanName + "'");
			}
			return beanInstance;
		}
		...省略...
	}

下面是 AbstractAutowireCapableBeanFactory#doCreateBean 方法源码

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

		// Instantiate the bean.
		//对Bean的装饰
		BeanWrapper instanceWrapper = null;
		if (mbd.isSingleton()) {
			//如果是单利,清除缓存
			instanceWrapper = this.factoryBeanInstanceCache.remove(beanName);
		}
		if (instanceWrapper == null) {
			//创建Bean方法【重要】
			instanceWrapper = createBeanInstance(beanName, mbd, args);
		}
		Object bean = instanceWrapper.getWrappedInstance();
		Class<?> beanType = instanceWrapper.getWrappedClass();
		if (beanType != NullBean.class) {
			mbd.resolvedTargetType = beanType;
		}
		//允许后处理器修改合并的 bean 定义。
		// 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;
			}
		}
		//急切地缓存单例,以便能够解析循环引用
		//是否允许提前曝光:单利&&允许循环依赖&&Bean在创建中
		// 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");
			}
			//匿名内部类,防止循环引用,尽早持有对象的引用
			//为了解决循环依赖,在Bean初始化完成前把ObjectFactory放入工厂
			addSingletonFactory(beanName, () -> getEarlyBeanReference(beanName, mbd, bean));
		}

		//初始化Bean,处理依赖注入
		// Initialize the bean instance.
		Object exposedObject = bean;
		try {
			//填充Bean , 主要是赋值属性,使用 bean 定义中的属性值填充给定 BeanWrapper 中的 bean 实例。
			//如果依赖其他Bean,会进行递归初始化依赖的Bean
			populateBean(beanName, mbd, instanceWrapper);
			//初始化Bean,如果Bean实现了InitializingBean接口,就会在这里调用afterPropertiesSet方法
			//同时会调用init-method方法
			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) {
			//获取已经注册的单利的Bean
			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 {
			//注册一个 DisposableBean 实现销毁回调
             //注册其 DisposableBean 接口和/或在工厂关闭时调用的给定销毁方法
			registerDisposableBeanIfNecessary(beanName, bean, mbd);
		}
		catch (BeanDefinitionValidationException ex) {
			throw new BeanCreationException(
					mbd.getResourceDescription(), beanName, "Invalid destruction signature", ex);
		}

		return exposedObject;
	}

总结一下方法的步骤

  • 如果是单利,清除缓存
  • createBeanInstance: 调用了createBeanInstance方法创建实例,转换成BeanWrapper
  • createBeanInstance ,创建BeanDean的实例
  • addSingletonFactory:添加ObjectFactory到缓存中
    依赖处理 :当A中有属性B,B中有属性A的时候,实例化A时发现依赖了B,此时会实例化B,而实例化B时发现依赖了A,此时不会直接实例化A,而是通过缓存中的ObjectFactory来创建,目的是解决循环依赖。
  • populateBean,对Bean的属性进行依赖注入.
  • initializeBean,对Bean进行初始化,如果Bean实现了InitializingBean接口,就会在这里调用afterPropertiesSet方法
  • registerDisposableBeanIfNecessary :销毁方法支持,如果Bean实现了DisposableBean接口,通过 DefaultSingletonBeanRegistry 把Bean包装成一个DisposableBeanAdapter,注册到一个disposableBeans = new LinkedHashMap 中。当容器关闭如:applicaiton.close() ,就会触发 AbstractApplicationContext#destroyBeans方法遍历该集合中的所有Bean,执行Bean的destroy方法。

下面是 AbstractAutowireCapableBeanFactory#createBeanInstance 创建实例的代码 ,该方法中使用了策略模式,

protected BeanWrapper createBeanInstance(String beanName, RootBeanDefinition mbd, @Nullable Object[] args) {
		// Make sure bean class is actually resolved at this point.
		//获取到Bean的class
		Class<?> beanClass = resolveBeanClass(mbd, beanName);

    	Supplier<?> instanceSupplier = mbd.getInstanceSupplier();
		if (instanceSupplier != null) {
			return obtainFromSupplier(instanceSupplier, beanName);
		}
		//如果BeanDefinition中有 工厂方法,通过工厂方式实例化
		if (mbd.getFactoryMethodName() != null)  {
             //通过工厂方法实例化Bean
			return instantiateUsingFactoryMethod(beanName, mbd, args);
		}
    
    	//使用自动装配功能实例化Bean
    	// Shortcut when re-creating the same bean...
		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);
			}
		}
    
		...省略...
		//确定构造器,用来创建实例
		//需要根据参数解析构造函数
		// Candidate constructors for autowiring?
		Constructor<?>[] ctors = determineConstructorsFromBeanPostProcessors(beanClass, beanName);
		if (ctors != null || mbd.getResolvedAutowireMode() == AUTOWIRE_CONSTRUCTOR ||
				mbd.hasConstructorArgumentValues() || !ObjectUtils.isEmpty(args)) {
			return autowireConstructor(beanName, mbd, ctors, args);
		}

		...省略...
		//实例化Bean[重要]
         //使用默认的无参构造方法实例化
		// No special handling: simply use no-arg constructor.
		return instantiateBean(beanName, mbd);
	}

上面代码体现出了几种实例化方式,通过工厂方法实例化,或者通过自动装配特性实例化Bean,这需要调用对应的构造器完成,但是我们通常使用的是默认无参构造,这就要用到了反射机制来实例化了,见: instantiateBean 方法。下面是 AbstractAutowireCapableBeanFactory#instantiateBean 方法源码如下:

protected BeanWrapper instantiateBean(String beanName, RootBeanDefinition mbd) {
		try {
			Object beanInstance;
			if (System.getSecurityManager() != null) {
				beanInstance = AccessController.doPrivileged(
						(PrivilegedAction<Object>) () -> getInstantiationStrategy().instantiate(mbd, beanName, this),
						getAccessControlContext());
			}
			else {
				//重要的代码在这里,通过 InstantiationStrategy 实例化Bean
				beanInstance = getInstantiationStrategy().instantiate(mbd, beanName, this);
			}
			BeanWrapper bw = new BeanWrapperImpl(beanInstance);
			initBeanWrapper(bw);
			return bw;
		}
		catch (Throwable ex) {
			throw new BeanCreationException(
					mbd.getResourceDescription(), beanName, "Instantiation of bean failed", ex);
		}
	}

方法中通过 InstantiationStrategy 实例化策略来实例化Bean,单利的Bean使用的是 SimpleInstantiationStrategy策略来实例化。得到实例对象后包装到BeanWrapper对象中。

SimpleInstantiationStrategy#instantiate

下面是 SimpleInstantiationStrategy#instantiate() 方法源码

@Override
	public Object instantiate(RootBeanDefinition bd, @Nullable String beanName, BeanFactory owner) {
		// Don't override the class with CGLIB if no overrides.
		//如果Bean定义没有覆盖方法,不要用 CGLIB 覆盖类。
		if (!bd.hasMethodOverrides()) {
			Constructor<?> constructorToUse;
			synchronized (bd.constructorArgumentLock) {
				constructorToUse = (Constructor<?>) bd.resolvedConstructorOrFactoryMethod;
				if (constructorToUse == null) {
					//反射,得到Bean的类型
					final Class<?> clazz = bd.getBeanClass();
					//接口没法实例化
					if (clazz.isInterface()) {
						throw new BeanInstantiationException(clazz, "Specified class is an interface");
					}
					try {
						if (System.getSecurityManager() != null) {
							//匿名内部类,得到构造器
							constructorToUse = AccessController.doPrivileged(
									(PrivilegedExceptionAction<Constructor<?>>) clazz::getDeclaredConstructor);
						}
						else {
							//反射,获取到构造器
							constructorToUse = clazz.getDeclaredConstructor();
						}
						bd.resolvedConstructorOrFactoryMethod = constructorToUse;
					}
					catch (Throwable ex) {
						throw new BeanInstantiationException(clazz, "No default constructor found", ex);
					}
				}
			}
			//使用Bean工具,通过反射创建Bean,通过构造方法.newInstance(arg)创建Bean
			return BeanUtils.instantiateClass(constructorToUse);
		}
		else {
			// Must generate CGLIB subclass.
			//使用CGLIB 实例化Bean
            //调用子类 CglibSubclassingInstantiationStrategy.instantiateWithMethodInjection 来实例化

			return instantiateWithMethodInjection(bd, beanName, owner);
		}
	}


	public static <T> T instantiateClass(Constructor<T> ctor, Object... args) throws BeanInstantiationException {
		Assert.notNull(ctor, "Constructor must not be null");
		try {
			ReflectionUtils.makeAccessible(ctor);
            //通过  ctor.newInstance 构造器实例化Bean
			return (KotlinDetector.isKotlinType(ctor.getDeclaringClass()) ?
					KotlinDelegate.instantiateClass(ctor, args) : ctor.newInstance(args));
        }

方法中做了一个判断如果Bean有方法被覆盖,就使用JDK反射来创建Bean的实例,否则使用CGLIB来实例化。 CGLIB调用的是子类 CglibSubclassingInstantiationStrategy 来完成的 , 如果一个类没有接口,就只能使用CGLIB

CglibSubclassingInstantiationStrategy#instantiateWithMethodInjection

下面是 CglibSubclassingInstantiationStrategy#instantiateWithMethodInjection CGLIB创建对象的代码

@Override
	protected Object instantiateWithMethodInjection(RootBeanDefinition bd, @Nullable String beanName, BeanFactory owner,
			@Nullable Constructor<?> ctor, Object... args) {

		// Must generate CGLIB subclass...
		return new CglibSubclassCreator(bd, owner).instantiate(ctor, args);
	}


public Object instantiate(@Nullable Constructor<?> ctor, Object... args) {
        //【重要】创建代理子类,返回class
        Class<?> subclass = createEnhancedSubclass(this.beanDefinition);
        Object instance;
        if (ctor == null) {
            //通过反射把class变成对象
            instance = BeanUtils.instantiateClass(subclass);
        }
        else {
            try {
                //代理子类构造器
                Constructor<?> enhancedSubclassConstructor = subclass.getConstructor(ctor.getParameterTypes());
                //通过构造器创建实例
                instance = enhancedSubclassConstructor.newInstance(args);
            }
            catch (Exception ex) {
                throw new BeanInstantiationException(this.beanDefinition.getBeanClass(),
                                                     "Failed to invoke constructor for CGLIB enhanced subclass [" + subclass.getName() + "]", ex);
            }
        }
        // SPR-10785: set callbacks directly on the instance instead of in the
        // enhanced class (via the Enhancer) in order to avoid memory leaks.
        Factory factory = (Factory) instance;
        factory.setCallbacks(new Callback[] {NoOp.INSTANCE,
                                             //查找覆盖方法拦截器
                                             //CGLIB MethodInterceptor 覆盖方法,将它们替换为返回在容器中查找的 bean 的实现。
                                             new LookupOverrideMethodInterceptor(this.beanDefinition, this.owner),
                                             //CGLIB MethodInterceptor 来覆盖方法,将它们替换为对通用 MethodReplacer 的调用
                                             new ReplaceOverrideMethodInterceptor(this.beanDefinition, this.owner)});
        return instance;
    }


	private Class<?> createEnhancedSubclass(RootBeanDefinition beanDefinition) {
			Enhancer enhancer = new Enhancer();
        	//增强类(代理类)将会把当前类作为父类
			enhancer.setSuperclass(beanDefinition.getBeanClass());
			enhancer.setNamingPolicy(SpringNamingPolicy.INSTANCE);
			if (this.owner instanceof ConfigurableBeanFactory) {
				ClassLoader cl = ((ConfigurableBeanFactory) this.owner).getBeanClassLoader();
				enhancer.setStrategy(new ClassLoaderAwareGeneratorStrategy(cl));
			}
			enhancer.setCallbackFilter(new MethodOverrideCallbackFilter(beanDefinition));
			enhancer.setCallbackTypes(CALLBACK_TYPES);
			return enhancer.createClass();
		}

	

上面就是CGLIB创建代理的代码,对于JDK动态代理而言被代理类必须有接口,JDK动态代理会为被代理类生成一个代理类和被代理类实现相同的接口,从而实现相同的方法,只不过在代理类的方法中可以持有并调用被代理类,从而实现对被代理类的方法做了增强的目的。但是如果被代理类么有接口,那就必须切换成CGLIB代理方式。

这种方式生成的代理类会把被代理类作为父类,对被代理类的方法进行复写和增强。如果没有接触过CGLIB动态代理这一块的可能看起来就比较晕菜了。

总结

到这里,我已经把IOC容器创建Bean的具体过程分析完毕,下面做个小结

  1. 我们讨论了Bean的实例化时机,IOC启动之后对单利Bean实例化,手动getBean时触发实例化,自动注入时触发实例化
  2. Bean的实例化是通过AbstractBeanFactory#doGetBean方法完成的。如果是单利的Bean,从单利Bean缓存的map中查找(ConcurrentHashMap中),找到直接返回,保证了单利,如果缓存中没有就就会进行创建。如果是多利Bean则每次都会创建一个新的Bean
  3. 创建Bean的过程主要是通过Bean的BeanDeifnition对象,使用JDK反射,或者CGLIB方式创建Bean。
  4. 当Bean创建成功之后会触发Bean的依赖注入,以及Bean的初始化方法。
  5. 最后注册了DisposableBean 来支持Bean的销毁方法调用

好了,本篇文章就到这吧,喜欢的话就给个好评吧,如果好评上500,我就是头发掉光,也出下章!!!

你可能感兴趣的:(《Spring源码深度剖析》,Spring源码,Spring,Bean的实例化)