您现在的位置是:首页 >技术杂谈 >【细读Spring Boot源码】Spring如何获取一个Bean?BeanFactory的getBean方法网站首页技术杂谈

【细读Spring Boot源码】Spring如何获取一个Bean?BeanFactory的getBean方法

园长的牧歌 2024-07-09 10:33:21
简介【细读Spring Boot源码】Spring如何获取一个Bean?BeanFactory的getBean方法

前言

在读refresh(context);时,finishBeanFactoryInitialization(beanFactory);beanFactory.preInstantiateSingletons();用于实例化剩余所有的non-lazy-init的单例。这里包含了前置处理器和后置处理器
这里面就用到了本文主题getBean(beanName);
在这里插入图片描述
下面具体看看这个方法的源码

类图

在这里插入图片描述
AbstractBeanFactory实现了BeanFactory中getBean(String name)

调用主流程

public Object getBean(String name) throws BeansException {
	// 找到做事的方法doGetBean
	return doGetBean(name, null, null, false);
}

解析做事的主方法doGetBean

protected <T> T doGetBean(
		String name, @Nullable Class<T> requiredType, @Nullable Object[] args, boolean typeCheckOnly)
		throws BeansException {
	
	// 转换bean为实际的名称
	String beanName = transformedBeanName(name);
	// 保存Bean实例的对象
	Object beanInstance;

	// Eagerly check singleton cache for manually registered singletons.
	// 检查单例缓存是否有手动注册的单例,这里涉及到三级缓存
	// 看详解1
	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.
		// 如果我们已经在创建这个bean实例,则失败: 我们假设在循环引用中
		if (isPrototypeCurrentlyInCreation(beanName)) {
			throw new BeanCurrentlyInCreationException(beanName);
		}

		// Check if bean definition exists in this factory.
		// 检查此工厂中是否存在bean定义
		// 获取父Bean工厂
		BeanFactory parentBeanFactory = getParentBeanFactory();
		// 从beanDefinitionMap里containsKey一下这个beanName ,不在则进入此条件
		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) {
			// 将指定的bean标记为已创建 (或即将创建)
			markBeanAsCreated(beanName);
		}

		StartupStep beanCreation = this.applicationStartup.start("spring.beans.instantiate")
				.tag("beanName", name);
		try {
			if (requiredType != null) {
				beanCreation.tag("beanType", requiredType::toString);
			}
			// 获取合并后的BeanDefinition,详见详解2
			RootBeanDefinition mbd = getMergedLocalBeanDefinition(beanName);
			// 检查是否是抽象的,是抽象的报错
			checkMergedBeanDefinition(mbd, beanName, args);

			// Guarantee initialization of beans that the current bean depends on.
			// 获取DependsOn
			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 + "'");
					}
					// 注册依赖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()) {
				// 这里是个回调函数,先进入看getSingleton方法,看详解3
				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();
		}
	}

	// 适配Bean实例。详看6
	return adaptBeanInstance(name, beanInstance, requiredType);
}

详解1——Object sharedInstance = getSingleton(beanName);

public Object getSingleton(String beanName) {
	// allowEarlyReference(应该创建早期引用)属性为true
	return getSingleton(beanName, true);
}

检查已经实例化的单例,并且还允许对当前创建的单例进行早期引用 (解析循环引用)。
三级缓存:

  • singletonObjects:单例对象的缓存
  • earlySingletonObjects:早期单例对象的缓存
  • singletonFactories:单例工厂的缓存
protected Object getSingleton(String beanName, boolean allowEarlyReference) {
	// Quick check for existing instance without full singleton lock
	// 取单例,取到直接返回
	Object 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 lock
				// 在加锁的情况下再次取单例,确定在这个过程中是否已经创建完成。取到直接返回
				singletonObject = this.singletonObjects.get(beanName);
				// 未取到
				if (singletonObject == null) {
					// 在加锁的情况下再次取早期单例,确定在这个过程中是否已创建早期单例对象。取到直接返回
					singletonObject = this.earlySingletonObjects.get(beanName);
					// 未取到
					if (singletonObject == null) {
						// 去单例工厂。未取到返回null
						ObjectFactory<?> singletonFactory = this.singletonFactories.get(beanName);
						// 取到单例工厂
						if (singletonFactory != null) {
							// 创建一个单例对象
							singletonObject = singletonFactory.getObject();
							// 放入早期单例对象
							this.earlySingletonObjects.put(beanName, singletonObject);
							// 从工厂中移除
							this.singletonFactories.remove(beanName);
						}
					}
				}
			}
		}
	}
	// 有4中情况的返回 单例对象、早期单例对象、单例工厂、null
	return singletonObject;
}

详看2——getObjectForBeanInstance(sharedInstance, name, beanName, null);

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

	// 当前创建的Bean
	String currentlyCreatedBean = this.currentlyCreatedBean.get();
	// 不为空
	if (currentlyCreatedBean != null) {
		// 为给定的bean注册一个依赖的bean,在给定的bean被销毁之前要销毁
		registerDependentBean(beanName, currentlyCreatedBean);
	}

	// 调用父类的方法
	return super.getObjectForBeanInstance(beanInstance, name, beanName, mbd);
}

父类AbstractBeanFactorygetObjectForBeanInstance方法

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.
	// 如果bean不是工厂,请不要让调用代码尝试取消引用工厂。
	if (BeanFactoryUtils.isFactoryDereference(name)) {
		if (beanInstance instanceof NullBean) {
			return beanInstance;
		}
		if (!(beanInstance instanceof FactoryBean)) {
			throw new BeanIsNotAFactoryException(beanName, beanInstance.getClass());
		}
		// 把isFactoryBean属性置为true
		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.
	// 现在我们有一个Bean的实例,可能是普通Bean也可能是工厂Bean
	// 如果他是一个FactoryBean,我们用它创建一个Bean实例,除非调用者想要一个工厂的引用
	if (!(beanInstance instanceof FactoryBean)) {
		// 不是工厂Bean直接返回实例对象 
		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());
		// 通过工厂Bean创建一个Bean
		object = getObjectFromFactoryBean(factory, beanName, !synthetic);
	}
	return object;
}

详解3——RootBeanDefinition mbd = getMergedLocalBeanDefinition(beanName);

protected RootBeanDefinition getMergedLocalBeanDefinition(String beanName) throws BeansException {
	// Quick check on the concurrent map first, with minimal locking.
	// 在 concurrent map上快速检查,锁定最少
	RootBeanDefinition mbd = this.mergedBeanDefinitions.get(beanName);
	if (mbd != null && !mbd.stale) {
		return mbd;
	}
	// 没有再去调用获取。getBeanDefinition是在beanDefinitionMap里取。
	return getMergedBeanDefinition(beanName, getBeanDefinition(beanName));
}
protected RootBeanDefinition getMergedBeanDefinition(String beanName, BeanDefinition bd)
		throws BeanDefinitionStoreException {
	// 再去调用获取
	return getMergedBeanDefinition(beanName, bd, null);
}

获取详情,作用:如果给定bean的定义是子bean定义,则通过与父级合并来返回给定bean的RootBeanDefinition。
参数:

  • beanName -bean定义的名称
  • bd -原始bean定义 (Root/ChildBeanDefinition)
  • containingBd -inner bean定义的包含bean定义,顶级bean定义的null
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) {
			// 保留mdb对象
			previous = mbd;
			// 取父定义的名称 为空
			if (bd.getParentName() == null) {
				// Use copy of given root bean definition.
				// 使用给定root bean 定义的拷贝
				if (bd instanceof RootBeanDefinition) {
					// 本身就是直接clone
					mbd = ((RootBeanDefinition) bd).cloneBeanDefinition();
				}
				else {
					// bd转为RootBeanDefinition
					mbd = new RootBeanDefinition(bd);
				}
			}
			// 有父定义的名称
			else {
				// Child bean definition: needs to be merged with parent.
				// 子定义需要merged给父定义
				BeanDefinition pbd;
				try {
					// 获取父BeanName
					String parentBeanName = transformedBeanName(bd.getParentName());
					// 与父名称不一样
					if (!beanName.equals(parentBeanName)) {
						// 合并
						pbd = getMergedBeanDefinition(parentBeanName);
					}
					else {
						// 得到父BeanFactory
						BeanFactory parent = getParentBeanFactory();
						if (parent instanceof ConfigurableBeanFactory) {
							// 从父BeanFactory得到BeanDefinition
							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)
			// 暂时缓存合并的bean定义 (以后可能仍会重新合并以获取元数据更改)
			if (containingBd == null && isCacheBeanMetadata()) {
				this.mergedBeanDefinitions.put(beanName, mbd);
			}
		}
		if (previous != null) {
			// 复制相关的合并Bean定义缓存
			copyRelevantMergedBeanDefinitionCaches(previous, mbd);
		}
		return mbd;
	}
}

详解4——getSingleton()

public Object getSingleton(String beanName, ObjectFactory<?> singletonFactory) {
	Assert.notNull(beanName, "Bean name must not be null");
	// 先上锁,锁singletonObjects对象
	synchronized (this.singletonObjects) {
		// 缓存里取一下
		Object singletonObject = this.singletonObjects.get(beanName);
		// 没取到
		if (singletonObject == null) {
			// 判断是否正在销毁,默认属性为false
			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 + "'");
			}
			// 单例创建前回调
			// 判断beanName在不在当前在创建检查中排除的bean名称缓存中
			// 加入当前正在创建的bean的名称缓存
			// 上面两个有一个失败false则抛BeanCurrentlyInCreationException异常
			beforeSingletonCreation(beanName);
			boolean newSingleton = false;
			boolean recordSuppressedExceptions = (this.suppressedExceptions == null);
			if (recordSuppressedExceptions) {
				this.suppressedExceptions = new LinkedHashSet<>();
			}
			try {
				// 这个singletonFactory就是回调函数,getObject()就是调用回调函数
				// 这里 getObject()返回是主流程中的return createBean(beanName, mbd, args);
				// 详见4.1
				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;
	}
}

详看4.1——createBean(beanName, mbd, args)

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.
	// 确保此时bean类已实际解析,并在无法存储在共享合并bean定义中的动态解析类的情况下克隆bean定义。
	// 解析Bean类,详见4.2
	Class<?> resolvedClass = resolveBeanClass(mbd, beanName);
	// 有解析到class 且 不是BeanClass 且 有BeanClassName
	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.
		// 给BeanPostProcessors一个返回代理而不是目标bean实例的机会。详看4.3
		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);
	}
}

详看4.2——resolveBeanClass(mbd, beanName);

解析Bean类

protected Class<?> resolveBeanClass(RootBeanDefinition mbd, String beanName, Class<?>... typesToMatch)
		throws CannotLoadBeanClassException {

	try {
		// 是否存在Bean类
		if (mbd.hasBeanClass()) {
			// 直接返回
			return mbd.getBeanClass();
		}
		// 有安全策略
		if (System.getSecurityManager() != null) {
			return AccessController.doPrivileged((PrivilegedExceptionAction<Class<?>>)
					() -> doResolveBeanClass(mbd, typesToMatch), getAccessControlContext());
		}
		else {
			// 做解析Bean类操作
			return doResolveBeanClass(mbd, typesToMatch);
		}
	}
	catch (PrivilegedActionException pae) {
		ClassNotFoundException ex = (ClassNotFoundException) pae.getException();
		throw new CannotLoadBeanClassException(mbd.getResourceDescription(), beanName, mbd.getBeanClassName(), ex);
	}
	catch (ClassNotFoundException ex) {
		throw new CannotLoadBeanClassException(mbd.getResourceDescription(), beanName, mbd.getBeanClassName(), ex);
	}
	catch (LinkageError err) {
		throw new CannotLoadBeanClassException(mbd.getResourceDescription(), beanName, mbd.getBeanClassName(), err);
	}
}

做解析动作

private Class<?> doResolveBeanClass(RootBeanDefinition mbd, Class<?>... typesToMatch)
		throws ClassNotFoundException {
	
	// 获取类加载器
	ClassLoader beanClassLoader = getBeanClassLoader();
	// 动态类加载器
	ClassLoader dynamicLoader = beanClassLoader;
	// 新的解析标志
	boolean freshResolve = false;

	// 处理类型匹配
	if (!ObjectUtils.isEmpty(typesToMatch)) {
		// When just doing type checks (i.e. not creating an actual instance yet),
		// use the specified temporary class loader (e.g. in a weaving scenario).
		ClassLoader tempClassLoader = getTempClassLoader();
		if (tempClassLoader != null) {
			dynamicLoader = tempClassLoader;
			freshResolve = true;
			if (tempClassLoader instanceof DecoratingClassLoader) {
				DecoratingClassLoader dcl = (DecoratingClassLoader) tempClassLoader;
				for (Class<?> typeToMatch : typesToMatch) {
					dcl.excludeClass(typeToMatch.getName());
				}
			}
		}
	}

	// 获取类全名
	String className = mbd.getBeanClassName();
	if (className != null) {
		// 评估bean定义中包含的给定字符串,有可能将其解析为表达式
		Object evaluated = evaluateBeanDefinitionString(className, mbd);
		// 是否是类全名
		if (!className.equals(evaluated)) {
			// A dynamically resolved expression, supported as of 4.2...
			// 一个动态解析的表达式
			if (evaluated instanceof Class) {
				return (Class<?>) evaluated;
			}
			else if (evaluated instanceof String) {
				className = (String) evaluated;
				freshResolve = true;
			}
			else {
				throw new IllegalStateException("Invalid class name expression result: " + evaluated);
			}
		}
		// 是一个新的解析
		if (freshResolve) {
			// When resolving against a temporary class loader, exit early in order
			// to avoid storing the resolved Class in the bean definition.
			if (dynamicLoader != null) {
				try {
					// 使用动态的类加载器加载类
					return dynamicLoader.loadClass(className);
				}
				catch (ClassNotFoundException ex) {
					if (logger.isTraceEnabled()) {
						logger.trace("Could not load class [" + className + "] from " + dynamicLoader + ": " + ex);
					}
				}
			}
			// 反射出类
			return ClassUtils.forName(className, dynamicLoader);
		}
	}

	// Resolve regularly, caching the result in the BeanDefinition...
	// 解析,将结果缓存在BeanDefinition中,看下面
	return mbd.resolveBeanClass(beanClassLoader);
}

mbd.resolveBeanClass(beanClassLoader);解析

public Class<?> resolveBeanClass(@Nullable ClassLoader classLoader) throws ClassNotFoundException {
	// 得到类名
	String className = getBeanClassName();
	if (className == null) {
		return null;
	}
	// 详看4.3 获取类
	Class<?> resolvedClass = ClassUtils.forName(className, classLoader);
	this.beanClass = resolvedClass;
	return resolvedClass;
}

获取类

public static Class<?> forName(String name, @Nullable ClassLoader classLoader)
		throws ClassNotFoundException, LinkageError {

	Assert.notNull(name, "Name must not be null");

	// 解析原始类名,从primitiveTypeNameMap里取一下
	Class<?> clazz = resolvePrimitiveClassName(name);
	if (clazz == null) {
		// 从通用缓存里取一下
		clazz = commonClassCache.get(name);
	}
	if (clazz != null) {
		return clazz;
	}

	// "java.lang.String[]" style arrays
	// 一种数组风格
	if (name.endsWith(ARRAY_SUFFIX)) {
		String elementClassName = name.substring(0, name.length() - ARRAY_SUFFIX.length());
		Class<?> elementClass = forName(elementClassName, classLoader);
		return Array.newInstance(elementClass, 0).getClass();
	}

	// "[Ljava.lang.String;" style arrays
	// 一种数组风格
	if (name.startsWith(NON_PRIMITIVE_ARRAY_PREFIX) && name.endsWith(";")) {
		String elementName = name.substring(NON_PRIMITIVE_ARRAY_PREFIX.length(), name.length() - 1);
		Class<?> elementClass = forName(elementName, classLoader);
		return Array.newInstance(elementClass, 0).getClass();
	}

	// "[[I" or "[[Ljava.lang.String;" style arrays
	// 一种数组风格
	if (name.startsWith(INTERNAL_ARRAY_PREFIX)) {
		String elementName = name.substring(INTERNAL_ARRAY_PREFIX.length());
		Class<?> elementClass = forName(elementName, classLoader);
		return Array.newInstance(elementClass, 0).getClass();
	}

	ClassLoader clToUse = classLoader;
	// 类加载器为空
	if (clToUse == null) {
		clToUse = getDefaultClassLoader();
	}
	try {
		// 反射出来实例
		return Class.forName(name, false, clToUse);
	}
	catch (ClassNotFoundException ex) {
		int lastDotIndex = name.lastIndexOf(PACKAGE_SEPARATOR);
		if (lastDotIndex != -1) {
			String nestedClassName =
					name.substring(0, lastDotIndex) + NESTED_CLASS_SEPARATOR + name.substring(lastDotIndex + 1);
			try {
				return Class.forName(nestedClassName, false, clToUse);
			}
			catch (ClassNotFoundException ex2) {
				// Swallow - let original exception get through
			}
		}
		throw ex;
	}
}

详看4.3——resolveBeforeInstantiation(beanName, mbdToUse)

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.
		// 确保bean类实际上在这一点上得到解决。
		if (!mbd.isSynthetic() && hasInstantiationAwareBeanPostProcessors()) {
			// 确定目标类型
			Class<?> targetType = determineTargetType(beanName, mbd);
			if (targetType != null) {
				// 实例化前应用Bean Post处理器  重要 
				bean = applyBeanPostProcessorsBeforeInstantiation(targetType, beanName);
				if (bean != null) {
					// 初始化后应用Bean Post处理器
					bean = applyBeanPostProcessorsAfterInitialization(bean, beanName);
				}
			}
		}
		mbd.beforeInstantiationResolved = (bean != null);
	}
	return bean;
}

实例化前应用Bean Post处理器,前置处理器

protected Object applyBeanPostProcessorsBeforeInstantiation(Class<?> beanClass, String beanName) {
	for (InstantiationAwareBeanPostProcessor bp : getBeanPostProcessorCache().instantiationAware) {
		Object result = bp.postProcessBeforeInstantiation(beanClass, beanName);
		if (result != null) {
			return result;
		}
	}
	return null;
}

初始化后应用Bean Post处理器

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

	Object result = existingBean;
	for (BeanPostProcessor processor : getBeanPostProcessors()) {
		Object current = processor.postProcessAfterInitialization(result, beanName);
		if (current == null) {
			return result;
		}
		result = current;
	}
	return result;
}

详看5——doCreateBean(beanName, mbdToUse, args);

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) {
		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;
}
protected BeanWrapper createBeanInstance(String beanName, RootBeanDefinition mbd, @Nullable Object[] args) {
	// Make sure bean class is actually resolved at this point.
	// 解析的bean类 (如果没有,则为null)
	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());
	}
	
	// 获取实例供应商
	Supplier<?> instanceSupplier = mbd.getInstanceSupplier();
	if (instanceSupplier != null) {
		return obtainFromSupplier(instanceSupplier, beanName);
	}

	// 获取factoryMethodName,不为空使用工厂方法实例化
	if (mbd.getFactoryMethodName() != null) {
		return instantiateUsingFactoryMethod(beanName, mbd, args);
	}

	// Shortcut when re-creating the same bean...
	// 重新创建相同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);
	}

	// Preferred constructors for default construction?
	ctors = mbd.getPreferredConstructors();
	if (ctors != null) {
		return autowireConstructor(beanName, mbd, ctors, null);
	}

	// No special handling: simply use no-arg constructor.
	return instantiateBean(beanName, mbd);
}

详看6——adaptBeanInstance(name, beanInstance, requiredType);

适配Bean实例

<T> T adaptBeanInstance(String name, Object bean, @Nullable Class<?> requiredType) {
	// Check if required type matches the type of the actual bean instance.
	// 检查所需类型是否与实际bean实例的类型匹配。
	if (requiredType != null && !requiredType.isInstance(bean)) {
		try {
			// 类型转换器转换
			Object convertedBean = getTypeConverter().convertIfNecessary(bean, requiredType);
			if (convertedBean == null) {
				throw new BeanNotOfRequiredTypeException(name, requiredType, bean.getClass());
			}
			return (T) 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;
}
风语者!平时喜欢研究各种技术,目前在从事后端开发工作,热爱生活、热爱工作。