Spring中的三级缓存是什么?
在Spring框架中,三级缓存用于存储和管理Bean的实例,他们的存在主要是为了解决循环依赖问题(即,Spring容器在创建Bean过程中,可能存在Bean A依赖Bean B,同时,B有依赖A的情况(即,循环依赖),为了确保在初始化过程中能提前暴露可用的Bean实例,采用了这样的三级缓存机制,这确保了Bean在初始化过程中的一致性和可用性)。其中包括:
- 单例对象缓存(singletonObjects): 这是Spring容器中用于存储完全初始化且准备好的单例Bean实例的缓存。当Spring容器初始化时,会创建并缓存这些单例Bean的实例,以便后续的请求可以直接返回已经创建好的实例。——存放成品
- 早期对象工厂缓存(earlySingletonObjects): 存储在Bean初始化过程中,已经完成了实例化、但尚未完成初始化(例如,尚未执行Bean的初始化方法和注入属性)的Bean实例。这一级缓存主要用于解决循环依赖问题,因为在循环依赖的情况下,可能需要提前暴露一个部分初始化的Bean实例。——存放半成品
- 早期对象缓存(earlySingletonObjects): 与早期对象工厂缓存类似,这一级缓存也是用于存储已经部分初始化的Bean实例。在处理循环依赖时,Spring会从这个缓存中获取Bean实例,以确保在完成整个初始化过程之前,可以提前暴露一个可用的Bean实例。——存放lambda表达式。
查找方式:在Idea中Ctrl + N输入 “DefaultSingletonBeanRegistry”:
三级缓存中的ObjectFactory介绍:
ObjectFactory 是函数式接口,可以将lambda表达式作为参数放到方法的实参中,在方法执行的时候,并不会实际的调用当前lambda表达式,只有在调用getObject方法的时候才会去调用lambda表达式。
三级缓存流程:
添加循环依赖举例代码:
Husband:
public class Husband {
private String name;
private Wife wife;
public void setName(String name) {
this.name = name;
}
public void setWife(Wife wife) {
this.wife = wife;
}
public String getName() {
return name;
}
@Override
public String toString() {
return "Husband{" +
"name='" + name + '\'' +
", wife=" + wife.getName() +
'}';
}
}
Wife
public class Wife {
private String name;
private Husband husband;
public void setHusband(Husband husband) {
this.husband = husband;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
@Override
public String toString() {
return "Wife{" +
"name='" + name + '\'' +
", husband=" + husband.getName() +
'}';
}
}
配置文件spring.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="husbandBean" class="com.xxx.spring6.bean.Husband" scope="singleton">
<property name="name" value="张三"/>
<property name="wife" ref="wifeBean"/>
</bean>
<bean id="wifeBean" class="com.xxx.spring6.bean.Wife" scope="singleton">
<property name="name" value="小花"/>
<property name="husband" ref="husbandBean"/>
</bean>
</beans>
源码探索:
通过查询进入到AbstractAutowireCapableBeanFactory类中,然后找到方法doCreateBean(在该方法中完成Bean的实例化及赋值)。
在该方法中的以下位置添加断点,然后进入Debug状态:
- 断点1:Object bean = instanceWrapper.getWrappedInstance();
- 断点2:addSingletonFactory(beanName, () -> getEarlyBeanReference(beanName, mbd, bean));
- 断点3:populateBean(beanName, mbd, instanceWrapper);
Debug经过:
- 在经过断点1后,查看bean的状态——属性值都为空,但已完成对象的空间分配操作(@3471):
- 直接运行到断点2,该步骤会将单例模式下的Bean添加到缓存中,通过f7进入该流程:
在DefaultSingletonBeanRegistry.java类中,我们可以看到有三个缓存:
一级缓存Map<String, Object> singletonObjects:存储的是完整的单例Bean对象,即,该缓存中的Bean对象已经完成属性赋值了,是一个完整的Bean对象。
二级缓存Map<String, Object> earlySingletonObjects:存储的是早期的单例Bean对象,此时的Bean对象的属性没有赋值,只是一个早期的实例对象。
三级缓存Map<String, ObjectFactory<?>> singletonFactories:存储的是单例工厂对象,存储的是大量的“工厂对象”,每一个单例Bean对象都会对应一个单例工厂对象,即,创建该单例对象时对应的单例工厂对象。
在addSingletonFactory方法中,会首先将该Bean对象的工厂对象放入三级缓存singletonFactories中,实现进行曝光:
- 继续执行,在填充Bean属性时(populateBean()),会调用DefaultSingletonBeanRegistry中的getSingleton方法(在该方法中加上断点):
可以发现赋值流程为:一级缓存–>二级缓存–>三级缓存
现在来回答“Spring解决循环依赖的原理”:
Spring通过set + singleton模式解决循环依赖的原因在于:这种方式可以做到将“实例化Bean”和“给Bean属性赋值”这两个动作分开去完成。实例化时,通过调用无参数构造方法来完成,此时可以先不给属性赋值,可以提前将该Bean对象“曝光”给外界;属性赋值时,调用setter方法来完成。两个步骤是可以分离完成的,即,Bean都是单例的,我们可以先把所有的单例Bean实例化出来,放到一个集合当中(即,缓存),所有的单例Bean全部实例化完成之后,以后我们再慢慢的调用setter方法给属性赋值。这样就解决了循环依赖的问题。
更详细的流程:
从AbstractApplicationContext.java中的refresh()方法进入(该方法主要用于刷新或重载应用程序的上下文):
synchronized (this.startupShutdownMonitor) {
StartupStep contextRefresh = this.applicationStartup.start("spring.context.refresh");
// Prepare this context for refreshing.
prepareRefresh();
// Tell the subclass to refresh the internal bean factory.
ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();
// Prepare the bean factory for use in this context.
prepareBeanFactory(beanFactory);
try {
// Allows post-processing of the bean factory in context subclasses.
postProcessBeanFactory(beanFactory);
StartupStep beanPostProcess = this.applicationStartup.start("spring.context.beans.post-process");
// Invoke factory processors registered as beans in the context.
invokeBeanFactoryPostProcessors(beanFactory);
// Register bean processors that intercept bean creation.
registerBeanPostProcessors(beanFactory);
beanPostProcess.end();
// Initialize message source for this context.
initMessageSource();
// Initialize event multicaster for this context.
initApplicationEventMulticaster();
// Initialize other special beans in specific context subclasses.
onRefresh();
// Check for listener beans and register them.
registerListeners();
// Instantiate all remaining (non-lazy-init) singletons.
finishBeanFactoryInitialization(beanFactory);
// Last step: publish corresponding event.
finishRefresh();
}
catch (BeansException ex) {
if (logger.isWarnEnabled()) {
logger.warn("Exception encountered during context initialization - " +
"cancelling refresh attempt: " + ex);
}
// Destroy already created singletons to avoid dangling resources.
destroyBeans();
// Reset 'active' flag.
cancelRefresh(ex);
// Propagate exception to caller.
throw ex;
}
finally {
contextRefresh.end();
}
}
}
finishBeanFactoryInitialization()方法之前,都是完成准备工作和初始化工作,finishBeanFactoryInitialization(beanFactory)实例化单例Bean对象,通过f7,查看具体流程:
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 BeanFactoryPostProcessor
// (such as a PropertySourcesPlaceholderConfigurer 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.
beanFactory.freezeConfiguration();
// Instantiate all remaining (non-lazy-init) singletons.
beanFactory.preInstantiateSingletons();
}
前面都是一些判断工作,实例化工作通过beanFactory.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.
List<String> beanNames = new ArrayList<>(this.beanDefinitionNames);
// Trigger initialization of all non-lazy singleton beans...
for (String beanName : beanNames) {
RootBeanDefinition bd = getMergedLocalBeanDefinition(beanName);
if (!bd.isAbstract() && bd.isSingleton() && !bd.isLazyInit()) {
if (isFactoryBean(beanName)) {
Object bean = getBean(FACTORY_BEAN_PREFIX + beanName);
if (bean instanceof SmartFactoryBean<?> smartFactoryBean && smartFactoryBean.isEagerInit()) {
getBean(beanName);
}
}
else {
getBean(beanName);
}
}
}
// Trigger post-initialization callback for all applicable beans...
for (String beanName : beanNames) {
Object singletonInstance = getSingleton(beanName);
if (singletonInstance instanceof SmartInitializingSingleton smartSingleton) {
StartupStep smartInitialize = getApplicationStartup().start("spring.beans.smart-initialize")
.tag("beanName", beanName);
smartSingleton.afterSingletonsInstantiated();
smartInitialize.end();
}
}
}
在beanDefinitionNames罗列了初始化Bean的名称:
RootBeanDefinition对应于一个具体的Java类或Java类型的Bean定义,其中包括以下主要信息:
- Bean的Class类型: 指定了该Bean对应的Java类或接口。
- Bean的作用域(Scope): 描述了Bean的作用域,例如单例、原型等。
- 是否抽象(abstract): 表示该Bean是否是一个抽象类,如果为true,表示该Bean不能被实例化。
- 是否懒加载(lazy-init): 指定是否在容器启动时就创建该Bean的实例,或者等到被请求时再创建。
- 构造函数参数信息: 描述了Bean的构造函数以及构造函数参数的信息。
- Bean的依赖信息(Dependencies): 包括依赖的其他Bean以及依赖检查等。
- 初始化方法和销毁方法: 指定了Bean的初始化方法和销毁方法。
针对非抽象,单例模式下且立即加载的Bean对象进行实例化(代码:!bd.isAbstract() && bd.isSingleton() && !bd.isLazyInit()),使用getBean()获取Bean实例。
在getBean()方法中,调用doGetBean()——该方法是实际获取Bean的方法,也是触发依赖注入的方法:
protected <T> T doGetBean(
String name, @Nullable Class<T> requiredType, @Nullable Object[] args, boolean typeCheckOnly)
throws BeansException {
String beanName = transformedBeanName(name);
Object beanInstance;
// Eagerly check singleton cache for manually registered singletons.
// 提前检查单例缓存中是否有手动注入的单例对象,跟循环依赖有关联
Object sharedInstance = getSingleton(beanName);
// 如果bean单例对象找到了,并且没有不创建bean实例要使用的参数
if (sharedInstance != null && args == null) {
if (logger.isTraceEnabled()) {
if (isSingletonCurrentlyInCreation(beanName)) {
logger.trace("Returning eagerly cached instance of singleton bean '" + beanName +
"' that is not fully initialized yet - a consequence of a circular reference");
}
else {
logger.trace("Returning cached instance of singleton bean '" + beanName + "'");
}
}
// 返回对象的实力
beanInstance = getObjectForBeanInstance(sharedInstance, name, beanName, null);
}
else {
// Fail if we're already creating this bean instance:
// We're assumably within a circular reference.
// 当对象时单例的时候会尝试解决循环依赖的问题,但是原型模式下如果存在循环依赖,则直接抛出异常
if (isPrototypeCurrentlyInCreation(beanName)) {
throw new BeanCurrentlyInCreationException(beanName);
}
// Check if bean definition exists in this factory.
// 如果bean定义不存在,就检查父工厂是否存在
BeanFactory parentBeanFactory = getParentBeanFactory();
// 如果beanDefinition中不包含beanName(即,在所有已经加载的类中不包含beanName),则尝试从父容器中获取
if (parentBeanFactory != null && !containsBeanDefinition(beanName)) {
// Not found -> check parent.
// 获取name对应的规范名称【全类名】,若name前有&,则返回 '&'+ 全类名
String nameToLookup = originalBeanName(name);
// 若父工厂是AbstractBeanFactory的实例
if (parentBeanFactory instanceof AbstractBeanFactory abf) {
// 调用付工厂的doGetBean方法。
return abf.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 {
// 使用父工厂通过全类名获取Bean
return (T) parentBeanFactory.getBean(nameToLookup);
}
}
// 如果不也是类型检查,那么表示要创建bean,此处在集合中添加一个记录
if (!typeCheckOnly) {
// 为此beanName表级为已创建(或将要创建)
markBeanAsCreated(beanName);
}
StartupStep beanCreation = this.applicationStartup.start("spring.beans.instantiate")
.tag("beanName", name);
try {
if (requiredType != null) {
beanCreation.tag("beanType", requiredType::toString);
}
// 做BeanDefinition对象的转换,如果是子类bean的话,会合并父类的相关属性
RootBeanDefinition mbd = getMergedLocalBeanDefinition(beanName);
// 检查mbd的合法性,不合格会引发异常
checkMergedBeanDefinition(mbd, beanName, args);
// Guarantee initialization of beans that the current bean depends on.
// 若存在依赖bean的话,则优先实例化依赖的bean
String[] dependsOn = mbd.getDependsOn();
if (dependsOn != null) {
// 存在依赖,需要地柜实例化依赖的bean
for (String dep : dependsOn) {
// 如果beanName已注册依赖于dependentBeanName的情况
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()) {
// 返回beanName的单例对象,若未注册,则使用singleonFactory创建并注册一个对象
sharedInstance = getSingleton(beanName, () -> {
try {
// 为给定的合并后BeanDefinition(和参数)创建一个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.
// 从单例缓存中显式删除实例:它可能是由创建过程急切地放在那里,用于循环引用解析,也要删除。
// 接收该Bean临时所引用的任何Bean。
// 销毁给定的bean。若找到相应的一次性bean实例,则位于给destoryBean
destroySingleton(beanName);
throw ex;
}
});
// 从beanInstance中获取已经公开的bean对象,
// Factory会直接返回beanInstance实例
beanInstance = getObjectForBeanInstance(sharedInstance, name, beanName, mbd);
}
// 原型模式下的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);
}
beanInstance = getObjectForBeanInstance(prototypeInstance, name, beanName, mbd);
}
else {
String scopeName = mbd.getScope();
if (!StringUtils.hasLength(scopeName)) {
throw new IllegalStateException("No scope name defined for bean '" + beanName + "'");
}
Scope scope = this.scopes.get(scopeName);
if (scope == null) {
throw new IllegalStateException("No Scope registered for scope name '" + scopeName + "'");
}
try {
Object scopedInstance = scope.get(beanName, () -> {
beforePrototypeCreation(beanName);
try {
return createBean(beanName, mbd, args);
}
finally {
afterPrototypeCreation(beanName);
}
});
beanInstance = getObjectForBeanInstance(scopedInstance, name, beanName, mbd);
}
catch (IllegalStateException ex) {
throw new ScopeNotActiveException(beanName, scopeName, ex);
}
}
}
catch (BeansException ex) {
beanCreation.tag("exception", ex.getClass().toString());
beanCreation.tag("message", String.valueOf(ex.getMessage()));
cleanupAfterBeanCreationFailure(beanName);
throw ex;
}
finally {
beanCreation.end();
}
}
return adaptBeanInstance(name, beanInstance, requiredType);
}
通过getSingleton获取单例对象:
sharedInstance = getSingleton(beanName, () -> {
try {
return createBean(beanName, mbd, args);
}
catch (BeansException ex) {
destroySingleton(beanName);
throw ex;
}
});
getSingleton方法:
public Object getSingleton(String beanName, ObjectFactory<?> singletonFactory) {
Assert.notNull(beanName, "Bean name must not be null");
// 使用单例对象的高速缓存Map作为锁,保证线程同步
synchronized (this.singletonObjects) {
// 从单例对象的高速缓存Map中获取beanName对应的单例对象
Object singletonObject = this.singletonObjects.get(beanName);
// 若单例对象获取不到
if (singletonObject == null) {
// 若当前在destorySingletons中,抛出异常
if (this.singletonsCurrentlyInDestruction) {
throw new BeanCreationNotAllowedException(beanName,
"Singleton bean creation not allowed while singletons of this factory are in destruction " +
"(Do not request a bean from a BeanFactory in a destroy method implementation!)");
}
if (logger.isDebugEnabled()) {
logger.debug("Creating shared instance of singleton bean '" + beanName + "'");
}
// 创建单例之前的回调,默认实现将单例注册为当前正在创建中
beforeSingletonCreation(beanName);
// 表示生成了新的单例对象的标记,默认为false,表示没有生成新的单例对象
boolean newSingleton = false;
// 有抑制异常记录的标记,没有时为true,否则为false
boolean recordSuppressedExceptions = (this.suppressedExceptions == null);
// 若没有抑制异常记录
if (recordSuppressedExceptions) {
// 对抑制的异常列表进行实例化(LinkedHashSet)
this.suppressedExceptions = new LinkedHashSet<>();
}
try {
// 从单例工厂中获取对象
// singletonFactory是传过来的lambda表达式,在调用getObject方法的时候,会执行lambda表达式,即,执行createBean(beanName, mbd, args)
singletonObject = singletonFactory.getObject();
// 生成新的单例对象的标记为true,表示生成了新的单例对象
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.
// 同时,单例对象是否隐式出现,如果是,则继续操作,因为异常表明状态
// 尝试从单例对象的高速缓存Map中获取beanName的单例对象
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;
}
}
在getSingleton中,经过一系列操作后(见代码注释),使用createBean()创建bean:
createBean方法:
@Override
protected Object createBean(String beanName, RootBeanDefinition mbd, @Nullable Object[] args)
throws BeanCreationException {
if (logger.isTraceEnabled()) {
logger.trace("Creating instance of bean '" + beanName + "'");
}
RootBeanDefinition mbdToUse = mbd;
// Make sure bean class is actually resolved at this point, and
// clone the bean definition in case of a dynamically resolved Class
// which cannot be stored in the shared merged bean definition.
// 锁定class,根据设置的class属性或者根据className来接卸class
Class<?> resolvedClass = resolveBeanClass(mbd, beanName);
// 条件筛选,重新赋值RootBeanDefinition,并设置BeanClass属性
if (resolvedClass != null && !mbd.hasBeanClass() && mbd.getBeanClassName() != null) {
// 重新创建一个RootBeanDefinition对象
mbdToUse = new RootBeanDefinition(mbd);
// 设置BeanClass属性
mbdToUse.setBeanClass(resolvedClass);
}
// Prepare method overrides.
// 验证及准备覆盖的方法
try {
mbdToUse.prepareMethodOverrides();
}
catch (BeanDefinitionValidationException ex) {
throw new BeanDefinitionStoreException(mbdToUse.getResourceDescription(),
beanName, "Validation of method overrides failed", ex);
}
try {
// Give BeanPostProcessors a chance to return a proxy instead of the target bean instance.
Object bean = resolveBeforeInstantiation(beanName, mbdToUse);
if (bean != null) {
return bean;
}
}
catch (Throwable ex) {
throw new BeanCreationException(mbdToUse.getResourceDescription(), beanName,
"BeanPostProcessor before instantiation of bean failed", ex);
}
try {
// 实际创建bean的调用
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);
}
}
在createBean中,通过doCreateBean()实际创建Bean对象:
doCreateBean方法:
protected Object doCreateBean(String beanName, RootBeanDefinition mbd, @Nullable Object[] args)
throws BeanCreationException {
// Instantiate the bean.
// 这个beanWrapper用来持有创建出来的bean对象
BeanWrapper instanceWrapper = null;
// 获取factoryBean实例缓存
if (mbd.isSingleton()) {
// 如果是单例对象,从factoryBean实例缓存中移除当前bean定义信息
instanceWrapper = this.factoryBeanInstanceCache.remove(beanName);
}
// 没有则创建实例
if (instanceWrapper == null) {
// 根据执行bean使用对应的策略创建新的实例,如:构造函数主动注入、工厂方法、简单初始化
instanceWrapper = createBeanInstance(beanName, mbd, args);
}
// 从包装类中获取原始bean
Object bean = instanceWrapper.getWrappedInstance();
// 获取具体的bean对象的Class属性
Class<?> beanType = instanceWrapper.getWrappedClass();
// 如不等于NullBean类型,则修改目标类型
if (beanType != NullBean.class) {
mbd.resolvedTargetType = beanType;
}
// Allow post-processors to modify the merged bean definition.
// 允许postProcessor去修改并合并的beanDefinition
synchronized (mbd.postProcessingLock) {
if (!mbd.postProcessed) {
try {
// MergedBeanDefinitionPostProcessors后置处理器修改合并bean的定义
applyMergedBeanDefinitionPostProcessors(mbd, beanType, beanName);
}
catch (Throwable ex) {
throw new BeanCreationException(mbd.getResourceDescription(), beanName,
"Post-processing of merged bean definition failed", ex);
}
mbd.markAsPostProcessed();
}
}
// 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));
}
// Initialize the bean instance.
// 初始化bean实例
Object exposedObject = bean;
try {
// 对bean的属性进行填充,将各个属性值注入,其中,可能存在依赖于其他的Bean的属性,则会递归初始化依赖的bean
populateBean(beanName, mbd, instanceWrapper);
// 执行初始化逻辑
exposedObject = initializeBean(beanName, exposedObject, mbd);
}
catch (Throwable ex) {
if (ex instanceof BeanCreationException bce && beanName.equals(bce.getBeanName())) {
throw bce;
}
else {
throw new BeanCreationException(mbd.getResourceDescription(), beanName, ex.getMessage(), 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;
}
使用createBeanInstance创建bean对象:
protected BeanWrapper createBeanInstance(String beanName, RootBeanDefinition mbd, @Nullable Object[] args) {
// Make sure bean class is actually resolved at this point.
// 确认需要创建的bean实力的类可以实例化
Class<?> beanClass = resolveBeanClass(mbd, beanName);
// 确保class不为空,并且访问权限是public
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());
}
// 判断当前beanDefinition中是否包含实例供应器,此处相当于一个回调方法,利用回调方法来创建Bean
Supplier<?> instanceSupplier = mbd.getInstanceSupplier();
if (instanceSupplier != null) {
return obtainFromSupplier(instanceSupplier, beanName, mbd);
}
// 若工厂方法不为空,则使用工厂方法初始化策略
if (mbd.getFactoryMethodName() != null) {
return instantiateUsingFactoryMethod(beanName, mbd, args);
}
// 在使用构造器创建实例后,Spring会将解析后的确定的构造方法或工厂方法保存在缓存中,避免再次创建相同bean时在此触发。
// Shortcut when re-creating the same bean...
// 进行标记,防止重复创建同一个bean
boolean resolved = false;
// 是否需要自动转配
boolean autowireNecessary = false;
// 若没有参数
if (args == null) {
synchronized (mbd.constructorArgumentLock) {
// 一个类可以有多个构造函数,所以要根据配置文件中的配置的参数或传入的参数来确定最终调用的构造函数。
// 因为判断过程会比较,所以spring会将解析、确定好的构造函数缓存到BeanDefinition中的resolvedConstructorOrFactoryMethod
// 在下次创建相同的bean时,会直接从RootBeanDefinition中的属性resolvedConstructorOrFactoryMethod缓存的值获取构造函数
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?
// 从bean后置处理器中为自动装配寻找构造方法,有且仅有一个有参构造或者有且仅有@Autowired注解构造
Constructor<?>[] ctors = determineConstructorsFromBeanPostProcessors(beanClass, beanName);
// 以下情况符合其一即可进入:
// 1. 存在可选构造方法
// 2. 自动装配模型为构造函数自动装配
// 3. 给BeanDefinition中设置了构造参数值
// 4. 有参与构造函数参数列表的参数
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);
}
instantiateBean方法:
protected BeanWrapper instantiateBean(String beanName, RootBeanDefinition mbd) {
try {
// 获取实例化策略并且进行实例化操作
Object beanInstance = getInstantiationStrategy().instantiate(mbd, beanName, this);
// 包装成 BeanWrapper
BeanWrapper bw = new BeanWrapperImpl(beanInstance);
initBeanWrapper(bw);
return bw;
}
catch (Throwable ex) {
throw new BeanCreationException(mbd.getResourceDescription(), beanName, ex.getMessage(), ex);
}
}
instantiate():
@Override
public Object instantiate(RootBeanDefinition bd, @Nullable String beanName, BeanFactory owner) {
// Don't override the class with CGLIB if no overrides.
// bd对象定义中,如果有MethodOverrides列表,spring中会有两个标签参数产生MethodOverrides,分别是lookup-method和replace-method
// 如果没有MethodOverrides对象,则可以直接实例化
if (!bd.hasMethodOverrides()) {
// 实例对象的构造方法
Constructor<?> constructorToUse;
// 锁定对象,获得实例化构造方法线程安全
synchronized (bd.constructorArgumentLock) {
// 查看bd对象里是否含有构造方法
constructorToUse = (Constructor<?>) bd.resolvedConstructorOrFactoryMethod;
// 没有构造方法
if (constructorToUse == null) {
// 从bd中获取beanClass
final Class<?> clazz = bd.getBeanClass();
// 如果要实例化的bdeanDefinition是一个接口,则直接抛出异常
if (clazz.isInterface()) {
throw new BeanInstantiationException(clazz, "Specified class is an interface");
}
try {
// 获取默认的无参构造器
constructorToUse = clazz.getDeclaredConstructor();
// 获取到构造器之后将构造器赋值给bd中的属性
bd.resolvedConstructorOrFactoryMethod = constructorToUse;
}
catch (Throwable ex) {
throw new BeanInstantiationException(clazz, "No default constructor found", ex);
}
}
}
// 通过反射生成具体的实例化对象
return BeanUtils.instantiateClass(constructorToUse);
}
else {
// Must generate CGLIB subclass.
// 必须生成CGLIB子类
return instantiateWithMethodInjection(bd, beanName, owner);
}
}
instantiateClass方法:
public static <T> T instantiateClass(Constructor<T> ctor, Object... args) throws BeanInstantiationException {
Assert.notNull(ctor, "Constructor must not be null");
try {
ReflectionUtils.makeAccessible(ctor);
if (KotlinDetector.isKotlinReflectPresent() && KotlinDetector.isKotlinType(ctor.getDeclaringClass())) {
return KotlinDelegate.instantiateClass(ctor, args);
}
else {
int parameterCount = ctor.getParameterCount();
Assert.isTrue(args.length <= parameterCount, "Can't specify more arguments than constructor parameters");
if (parameterCount == 0) {
return ctor.newInstance();
}
Class<?>[] parameterTypes = ctor.getParameterTypes();
Object[] argsWithDefaultValues = new Object[args.length];
// 获得参数
for (int i = 0 ; i < args.length; i++) {
if (args[i] == null) {
Class<?> parameterType = parameterTypes[i];
argsWithDefaultValues[i] = (parameterType.isPrimitive() ? DEFAULT_TYPE_VALUES.get(parameterType) : null);
}
else {
argsWithDefaultValues[i] = args[i];
}
}
// 获取对象
return ctor.newInstance(argsWithDefaultValues);
}
}
catch (InstantiationException ex) {
throw new BeanInstantiationException(ctor, "Is it an abstract class?", ex);
}
catch (IllegalAccessException ex) {
throw new BeanInstantiationException(ctor, "Is the constructor accessible?", ex);
}
catch (IllegalArgumentException ex) {
throw new BeanInstantiationException(ctor, "Illegal arguments for constructor", ex);
}
catch (InvocationTargetException ex) {
throw new BeanInstantiationException(ctor, "Constructor threw exception", ex.getTargetException());
}
}
把bean初始化完成前将创建实例的ObjectFactory放入工厂,addSingletonFactory方法:经过该方法中的this.singletonFactories.put(beanName, singletonFactory);,此时此操作后,三级缓存singletonFactories存储的的是[bean名称-lambda表达式],其lambda表达式对应于AbstractAutowireCapableBeanFactory.java中的:
() -> getEarlyBeanReference(beanName, mbd, bean)
protected void addSingletonFactory(String beanName, ObjectFactory<?> singletonFactory) {
Assert.notNull(singletonFactory, "Singleton factory must not be null");
// 使用singletonObjects进行加锁,保证线程安全
synchronized (this.singletonObjects) {
// 若单例对象的高速缓存[bean名称-bean实例]没有beanName对象
if (!this.singletonObjects.containsKey(beanName)) {
// 将beanName,singletonFactory放到单例工厂的缓存[bean名称-ObjectFactory]
this.singletonFactories.put(beanName, singletonFactory);
// 从早期单例对象的高速缓存[bean名称-bean实例]移除beanName的相关缓存对象
this.earlySingletonObjects.remove(beanName);
// 将beanName添加到已注册的单例集中
this.registeredSingletons.add(beanName);
}
}
}
属性赋值操作——populateBean()方法:
protected void populateBean(String beanName, RootBeanDefinition mbd, @Nullable BeanWrapper bw) {
// 如果beanWrapper为空
if (bw == null) {
// 如果mbd有需要设置的属性
if (mbd.hasPropertyValues()) {
// 抛出bean创建异常
throw new BeanCreationException(
mbd.getResourceDescription(), beanName, "Cannot apply property values to null instance");
}
else {
// 若没有可填充的属性,则跳过
// Skip property population phase for null instance.
return;
}
}
if (bw.getWrappedClass().isRecord()) {
if (mbd.hasPropertyValues()) {
throw new BeanCreationException(
mbd.getResourceDescription(), beanName, "Cannot apply property values to a record");
}
else {
// Skip property population phase for records since they are immutable.
return;
}
}
// Give any InstantiationAwareBeanPostProcessors the opportunity to modify the
// state of the bean before properties are set. This can be used, for example,
// to support styles of field injection.
// 在设置属性之前,为任何 InstantiationAwareBeanPostProcessor 提供修改 bean 状态的机会。
// 若是 "synthetic",一般是指只有AOP相关的pointCut配置或Advice配置才会将sythetic设置为true
// 若mdb不是“synthetic”且工厂拥有InstantiationAwareBeanPostProcessor
if (!mbd.isSynthetic() && hasInstantiationAwareBeanPostProcessors()) {
// 遍历工厂中的BeanPostProcessor对象
for (InstantiationAwareBeanPostProcessor bp : getBeanPostProcessorCache().instantiationAware) {
// postProcessAfterInstantiation一般用于设置属性
if (!bp.postProcessAfterInstantiation(bw.getWrappedInstance(), beanName)) {
return;
}
}
}
// PropertyValues包含以一个或多个PropertyValue对象的容器,通常包括针对特定目标Bean的一次更新
// 若mbd有PropertyValues就获取其PropertyValues
PropertyValues pvs = (mbd.hasPropertyValues() ? mbd.getPropertyValues() : null);
// 获取mbd的自动装配模式
int resolvedAutowireMode = mbd.getResolvedAutowireMode();
// 若自动装配模式为 按名称自动装配bean属性 或者 按类型自动装配bean属性
if (resolvedAutowireMode == AUTOWIRE_BY_NAME || resolvedAutowireMode == AUTOWIRE_BY_TYPE) {
// MutablePropertyValues: PropertyValues的默认接口实现。允许对属性进行简单操作。
MutablePropertyValues newPvs = new MutablePropertyValues(pvs);
// Add property values based on autowire by name if applicable.
// 根据autowire的名称(如果适用)添加属性
if (resolvedAutowireMode == AUTOWIRE_BY_NAME) {
autowireByName(beanName, mbd, bw, newPvs);
}
// Add property values based on autowire by type if applicable.
// 根据自动装配的类型(如果适用)添加属性值
if (resolvedAutowireMode == AUTOWIRE_BY_TYPE) {
// 通过bw的PropertyDescriptor属性类型,查找出对应的Bean对象,将其添加到newPvs中
autowireByType(beanName, mbd, bw, newPvs);
}
// 让pvs重新引用newPvs,newPvs此时已经包含了pvs的属性值以及通过AUTOWIRE_BY_NAME、AUTOWIRE_BY_TYPE自动装配
pvs = newPvs;
}
// 工厂是否拥有InstantiationAwareBeanPostProcessor
if (hasInstantiationAwareBeanPostProcessors()) {
if (pvs == null) {
// 尝试获取mbd的PropertyValues
pvs = mbd.getPropertyValues();
}
// 遍历工厂内的所有后置处理器
for (InstantiationAwareBeanPostProcessor bp : getBeanPostProcessorCache().instantiationAware) {
// postProcessProperties在工厂将给定的属性值应用到给定bean之前,对他们进行后置处理,不需要任何属性
// 让bp对pvs增加对bw的Bean对象的propertyValue,或编辑pvs的propertyValue
PropertyValues pvsToUse = bp.postProcessProperties(pvs, bw.getWrappedInstance(), beanName);
if (pvsToUse == null) {
return;
}
// 让pvs引用pvsToUse
pvs = pvsToUse;
}
}
// mbd.getDependencyCheck()默认返回 DEPENDENCY_CHECK_NONE,表示不检查
// 是否需要依赖检查
boolean needsDepCheck = (mbd.getDependencyCheck() != AbstractBeanDefinition.DEPENDENCY_CHECK_NONE);
if (needsDepCheck) {
// mbd.allowCaching:是否允许缓存,默认允许。
// 从bw提取一组筛选的PropertyDescriptor,排除忽略的依赖项或忽略项上的定义的属性
PropertyDescriptor[] filteredPds = filterPropertyDescriptorsForDependencyCheck(bw, mbd.allowCaching);
// 检查依赖项:主要检查pd的setter方法需要赋值时,pvs中有没有满足其pd的需求的属性值可供其赋值
checkDependencies(beanName, mbd, filteredPds, pvs);
}
if (pvs != null) {
// 应用给定的属性值,解决任何在这个bean工厂运行时其他bean的引用。必须进行深拷贝。
applyPropertyValues(beanName, mbd, bw, pvs);
}
}
属性赋值方法——applyPropertyValues():
protected void applyPropertyValues(String beanName, BeanDefinition mbd, BeanWrapper bw, PropertyValues pvs) {
// 若pvs没有PropertyValue
if (pvs.isEmpty()) {
return;
}
// MutablePropertyValues:PropertyValues接口的默认实现。
MutablePropertyValues mpvs = null;
// 原始属性列表
List<PropertyValue> original;
if (pvs instanceof MutablePropertyValues _mpvs) {
mpvs = _mpvs;
// 若mpvs值包含转换后的值
if (mpvs.isConverted()) {
// Shortcut: use the pre-converted values as-is.
try {
// 完成属性值赋值,直接晚会
bw.setPropertyValues(mpvs);
return;
}
catch (BeansException ex) {
throw new BeanCreationException(
mbd.getResourceDescription(), beanName, "Error setting property values", ex);
}
}
// 获取mpvs的PropertyValue列表
original = mpvs.getPropertyValueList();
}
else {
// 获取pvs的PropertyValue对象数组,并将其转换成列表
original = Arrays.asList(pvs.getPropertyValues());
}
// 获取用户自定义类型转换器
TypeConverter converter = getCustomTypeConverter();
// 如果转换器为空,则直接把包装类赋值给converter
if (converter == null) {
converter = bw;
}
// BeanDefinitionValueResolver在bean工厂实现中使用Helper类,它将beanDefinition对象中包含的值解析为应用于目标bean实例的实际值
BeanDefinitionValueResolver valueResolver = new BeanDefinitionValueResolver(this, beanName, mbd, converter);
// Create a deep copy, resolving any references for values.
// 创建一个深拷贝,解析认知引用
List<PropertyValue> deepCopy = new ArrayList<>(original.size());
// 是否还需要标记
boolean resolveNecessary = false;
// 遍历属性,将属性转换为对应类的对应属性的类型
for (PropertyValue pv : original) {
// 若该属性已经解析过
if (pv.isConverted()) {
// 将pv添加到deepCopy中
deepCopy.add(pv);
}
// 属性没有被解析过
else {
// 获取属性的名字
String propertyName = pv.getName();
// 获取未经类型转换的值
Object originalValue = pv.getValue();
// AutowiredPropertyMarker.INSTANCE:自动生成标记的规范实例
if (originalValue == AutowiredPropertyMarker.INSTANCE) {
// 获取propertyName在bw值的setter方法
Method writeMethod = bw.getPropertyDescriptor(propertyName).getWriteMethod();
// setterr方法为null
if (writeMethod == null) {
throw new IllegalArgumentException("Autowire marker for property without write method: " + pv);
}
// 将writerMethod封装到DependencyDescriptor对象
originalValue = new DependencyDescriptor(new MethodParameter(writeMethod, 0), true);
}
// 交由valueResolver根据pv解析出originalValue所封装的对象
Object resolvedValue = valueResolver.resolveValueIfNecessary(pv, originalValue);
// 默认转换后的值是刚解析出来的值
Object convertedValue = resolvedValue;
// 可转换标记:propertyName是否是bw中的科协属性 && propertyName不是表示索引属性或嵌套属性
boolean convertible = bw.isWritableProperty(propertyName) &&
!PropertyAccessorUtils.isNestedOrIndexedProperty(propertyName);
// 如果可以转换
if (convertible) {
// 将convertedValue转换为执行的目标对象
convertedValue = convertForProperty(resolvedValue, propertyName, bw, converter);
}
// Possibly store converted value in merged bean definition,
// in order to avoid re-conversion for every created bean instance.
// 可以将转换后的值存储合并后BeanDefinition中,以避免对每个创建Bean实例进行重新转化
// 如果resolvedValue与originalValue时同一个对象
if (resolvedValue == originalValue) {
if (convertible) {
// 将cinvertedValue设置到pv中
pv.setConvertedValue(convertedValue);
}
// 将pv添加到deepCopy中
deepCopy.add(pv);
}
// TypedStringValue: 类型字符串的Holder, 他将只存储字符串值和目标类型
// 如果可转化 && originalValue是TypedStringValue的实力 && originalValue不是标记为动态
// originalValue不是Collection对象 或 数组
else if (convertible && originalValue instanceof TypedStringValue typedStringValue &&
!typedStringValue.isDynamic() &&
!(convertedValue instanceof Collection || ObjectUtils.isArray(convertedValue))) {
pv.setConvertedValue(convertedValue);
deepCopy.add(pv);
}
else {
// 标记还需要解析
resolveNecessary = true;
// 根据pv,convertedValue构建PropertyValue对象,并添加到deepCopy中
deepCopy.add(new PropertyValue(pv, convertedValue));
}
}
}
// mpvs不为空,且已经不需要解析
if (mpvs != null && !resolveNecessary) {
// 将此holder标记为只包含转换后的值
mpvs.setConverted();
}
// Set our (possibly massaged) deep copy.
try {
// 按照原样使用deepCopy构造一个新的MutablePropertyValues对象,然后设置到bw中以对bw的属性值更新
bw.setPropertyValues(new MutablePropertyValues(deepCopy));
}
catch (BeansException ex) {
throw new BeanCreationException(mbd.getResourceDescription(), beanName, ex.getMessage(), ex);
}
}
给定一个 PropertyValue,返回一个值,如有必要,解析对工厂中其他 bean 的任何引用——resolveValueIfNecessary()方法:
public Object resolveValueIfNecessary(Object argName, @Nullable Object value) {
// We must check each value to see whether it requires a runtime reference
// to another bean to be resolved.
// 必须检查每个值,以查看它是否需要对另一个bean的运行时引用才能解决
// RuntimeBeanReference:当属性值对象是工厂中另一个bean的引用是,使用不可变的占位符类,在运行时进行解析
// 如果values是RuntimeBeanReference实例
if (value instanceof RuntimeBeanReference ref) {
// 解析处对应ref所封装的Bean元信息(即Bean名,Bean类型)的Bean对象
return resolveReference(argName, ref);
}
// RuntimeBeanReference对应于<idref bean="bea"/>
// idref注入的是目标bean的id,而不是目标bean的实例,同时使用idref容器在部署的时候还会验证这个名称的bean
// 是否真实存在。其实idref就跟value一样,只是将某个字符串注入到属性或者构造函数中,只不过注入的是某个Bean定义的id属性值。
// 即: <idref bean="bea"/>等同于<value>bea</value>
// 若value是RuntimeBeanNameReference的实力
else if (value instanceof RuntimeBeanNameReference ref) {
// 从value中获取引用的bean名
String refName = ref.getBeanName();
refName = String.valueOf(doEvaluate(refName));
if (!this.beanFactory.containsBean(refName)) {
throw new BeanDefinitionStoreException(
"Invalid bean name '" + refName + "' in bean reference for " + argName);
}
return refName;
}
else if (value instanceof BeanDefinitionHolder bdHolder) {
// Resolve BeanDefinitionHolder: contains BeanDefinition with name and aliases.
return resolveInnerBean(bdHolder.getBeanName(), bdHolder.getBeanDefinition(),
(name, mbd) -> resolveInnerBeanValue(argName, name, mbd));
}
else if (value instanceof BeanDefinition bd) {
return resolveInnerBean(null, bd,
(name, mbd) -> resolveInnerBeanValue(argName, name, mbd));
}
else if (value instanceof DependencyDescriptor dependencyDescriptor) {
Set<String> autowiredBeanNames = new LinkedHashSet<>(4);
Object result = this.beanFactory.resolveDependency(
dependencyDescriptor, this.beanName, autowiredBeanNames, this.typeConverter);
for (String autowiredBeanName : autowiredBeanNames) {
if (this.beanFactory.containsBean(autowiredBeanName)) {
this.beanFactory.registerDependentBean(autowiredBeanName, this.beanName);
}
}
return result;
}
else if (value instanceof ManagedArray managedArray) {
// May need to resolve contained runtime references.
Class<?> elementType = managedArray.resolvedElementType;
if (elementType == null) {
String elementTypeName = managedArray.getElementTypeName();
if (StringUtils.hasText(elementTypeName)) {
try {
elementType = ClassUtils.forName(elementTypeName, this.beanFactory.getBeanClassLoader());
managedArray.resolvedElementType = elementType;
}
catch (Throwable ex) {
// Improve the message by showing the context.
throw new BeanCreationException(
this.beanDefinition.getResourceDescription(), this.beanName,
"Error resolving array type for " + argName, ex);
}
}
else {
elementType = Object.class;
}
}
return resolveManagedArray(argName, (List<?>) value, elementType);
}
else if (value instanceof ManagedList<?> managedList) {
// May need to resolve contained runtime references.
return resolveManagedList(argName, managedList);
}
else if (value instanceof ManagedSet<?> managedSet) {
// May need to resolve contained runtime references.
return resolveManagedSet(argName, managedSet);
}
else if (value instanceof ManagedMap<?, ?> managedMap) {
// May need to resolve contained runtime references.
return resolveManagedMap(argName, managedMap);
}
else if (value instanceof ManagedProperties original) {
// Properties original = managedProperties;
Properties copy = new Properties();
original.forEach((propKey, propValue) -> {
if (propKey instanceof TypedStringValue typedStringValue) {
propKey = evaluate(typedStringValue);
}
if (propValue instanceof TypedStringValue typedStringValue) {
propValue = evaluate(typedStringValue);
}
if (propKey == null || propValue == null) {
throw new BeanCreationException(
this.beanDefinition.getResourceDescription(), this.beanName,
"Error converting Properties key/value pair for " + argName + ": resolved to null");
}
copy.put(propKey, propValue);
});
return copy;
}
else if (value instanceof TypedStringValue typedStringValue) {
// Convert value to target type here.
Object valueObject = evaluate(typedStringValue);
try {
Class<?> resolvedTargetType = resolveTargetType(typedStringValue);
if (resolvedTargetType != null) {
return this.typeConverter.convertIfNecessary(valueObject, resolvedTargetType);
}
else {
return valueObject;
}
}
catch (Throwable ex) {
// Improve the message by showing the context.
throw new BeanCreationException(
this.beanDefinition.getResourceDescription(), this.beanName,
"Error converting typed String value for " + argName, ex);
}
}
else if (value instanceof NullBean) {
return null;
}
else {
return evaluate(value);
}
}
解析对工厂中另一个 bean 的引用——resolveReference()方法:
private Object resolveReference(Object argName, RuntimeBeanReference ref) {
try {
// 定义用于一个存储bean对象的变量
Object bean;
// 获取另一个Bean引用的Bean类型
Class<?> beanType = ref.getBeanType();
// 若引用来自父工厂
if (ref.isToParent()) {
// 获取父工厂
BeanFactory parent = this.beanFactory.getParentBeanFactory();
// 若没有父工厂
if (parent == null) {
throw new BeanCreationException(
this.beanDefinition.getResourceDescription(), this.beanName,
"Cannot resolve reference to bean " + ref +
" in parent factory: no parent factory available");
}
// 若引用的Bean类型不为null
if (beanType != null) {
bean = parent.getBean(beanType);
}
else {
// 否则,使用引用的Bean名,从父工厂中获取对应的Bean对象
bean = parent.getBean(String.valueOf(doEvaluate(ref.getBeanName())));
}
}
else {
// 定义一个用于存储解析出来的Bean名的变量
String resolvedName;
// 若beanType不为null
if (beanType != null) {
// 解析与beanType唯一匹配的实例,包括其bean名
NamedBeanHolder<?> namedBean = this.beanFactory.resolveNamedBean(beanType);
// 让bean引用nameBean所封装的Bean对象
bean = namedBean.getBeanInstance();
// 让resolvedName引用nameBean所封装的Bean名
resolvedName = namedBean.getBeanName();
}
else {
// 让resolveName引用ref所包装的Bean名
resolvedName = String.valueOf(doEvaluate(ref.getBeanName()));
// 获取resolveName的Bean对象
bean = this.beanFactory.getBean(resolvedName);
}
// 注册beanName与dependentBeanNamed的依赖关系到Bean工厂
this.beanFactory.registerDependentBean(resolvedName, this.beanName);
}
// 若Bean对象时NullBean对象
if (bean instanceof NullBean) {
// 将bean设置为null
bean = null;
}
// 返回解析出来对应ref所封装的Bean元信息(即Bean名,Bean类型)的Bean对象
return bean;
}
catch (BeansException ex) {
throw new BeanCreationException(
this.beanDefinition.getResourceDescription(), this.beanName,
"Cannot resolve reference to bean '" + ref.getBeanName() + "' while setting " + argName, ex);
}
}
在上段代码属性赋值过程中,调用getBean() 开始套娃创建Bean对象——getBean()方法:
@Override
public Object getBean(String name) throws BeansException {
// 此方法是实际获取bean的方法,也是触发依赖注入的方法
return doGetBean(name, null, null, false);
}
获取对指定 bean 的早期访问的引用——getEarlyBeanReference():
protected Object getEarlyBeanReference(String beanName, RootBeanDefinition mbd, Object bean) {
// 默认最终公开的对象时bean,通过createBeanInstance创建出来的普通对象
Object exposedObject = bean;
if (!mbd.isSynthetic() && hasInstantiationAwareBeanPostProcessors()) {
// 遍历工厂的所有后处理器
for (SmartInstantiationAwareBeanPostProcessor bp : getBeanPostProcessorCache().smartInstantiationAware) {
// 让exposedObject经过每个SmartInstantiationAware的包装
exposedObject = bp.getEarlyBeanReference(exposedObject, beanName);
}
}
// 返回最终经过层次包装后的对象
return exposedObject;
}
放到集合中,然后判断要不要包装,其实就是循环依赖注入属性的时候如果有AOP代理的话,也会进行代理,然后返回——getEarlyBeanReference():
public Object getEarlyBeanReference(Object bean, String beanName) {
Object cacheKey = this.getCacheKey(bean.getClass(), beanName);
this.earlyProxyReferences.put(cacheKey, bean);
return this.wrapIfNecessary(bean, beanName, cacheKey);
}
protected Object wrapIfNecessary(Object bean, String beanName, Object cacheKey) {
// 如果已经处理过,直接返回
if (StringUtils.hasLength(beanName) && this.targetSourcedBeans.contains(beanName)) {
return bean;
}
// 这里的advisedBeans缓存了已经进行了代理的bean,如果缓存中存在,则可以直接返回
else if (Boolean.FALSE.equals(this.advisedBeans.get(cacheKey))) {
return bean;
}
// 这里的isInfrastructureClass()用于判断当前bean是否是为Spring系统自带的bean,自带的bean是不用进行代理的
// shouldSkip()用于判断当前bean是否应该被略过
else if (!this.isInfrastructureClass(bean.getClass()) && !this.shouldSkip(bean.getClass(), beanName)) {
// 获取当前bean的Advices和Advisors
Object[] specificInterceptors = this.getAdvicesAndAdvisorsForBean(bean.getClass(), beanName, (TargetSource)null);
if (specificInterceptors != DO_NOT_PROXY) {
// 对当前bean进行缓存
this.advisedBeans.put(cacheKey, Boolean.TRUE);
// 根据获取到的Advices和Advisors为当前bean生成代理对象
Object proxy = this.createProxy(bean.getClass(), beanName, specificInterceptors, new SingletonTargetSource(bean));
// 缓存生成的代理bean的类型,并且返回生成的代理bean
this.proxyTypes.put(cacheKey, proxy.getClass());
return proxy;
} else {
this.advisedBeans.put(cacheKey, Boolean.FALSE);
return bean;
}
} else {
this.advisedBeans.put(cacheKey, Boolean.FALSE);
return bean;
}
}
综上,Bean的创建流程可简单归纳为: