创建对象核心入口
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory#doCreateBean
执行链
//调用反射创建对象,根据BeanDefinition的定义进行填充初始值,各种依赖注入
//默认使用无参构造创建,如果指定了有参构造函数则从IOC容器获取参数,获取不到参数则创建参数对象
BeanWrapper instanceWrapper = createBeanInstance(beanName, mbd, args);
=>填充入口 initBeanWrapper(bw);
======
//开始走初始化流程
org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory#initializeBean()
//先执行BeanNameAware,BeanClassLoaderAware,BeanFactoryAware接口实现判断然后分别调用
invokeAwareMethods(beanName, bean);
//for循环执行BeanPostProcessor前置方法
applyBeanPostProcessorsBeforeInitialization(wrappedBean, beanName);
//执行InitializingBean接口判断并调用
//从BeanDefinition获取initMethodName名称集合,如果不为空则反射调用
invokeInitMethods(beanName, wrappedBean, mbd);
=======
//最后for循环执行BeanPostProcessor后置方法
applyBeanPostProcessorsAfterInitialization(wrappedBean, beanName);
三级缓存解决入口
org.springframework.beans.factory.support.DefaultSingletonBeanRegistry#getSingleton()
三级缓存核心集合
//第一级缓存 创建完全的单例对象都放在这个集合
private final Map<String, Object> singletonObjects = new ConcurrentHashMap<>(256);
//第二级缓存 创建了但未初始化完全,中途被其他对象引入的单例对象这个集合
private final Map<String, Object> earlySingletonObjects = new ConcurrentHashMap<>(16);
//第三级缓存 刚创建但未初始化完全的单例对象放到这个集合
private final Map<String, ObjectFactory<?>> singletonFactories = new ConcurrentHashMap<>(16);
核心代码解析
protected Object getSingleton(String beanName, boolean allowEarlyReference) {
//从一级缓存拿正常单例对象
Object singletonObject = this.singletonObjects.get(beanName);
//为空,判断目标对象是否正在创建
if (singletonObject == null && isSingletonCurrentlyInCreation(beanName)) {
//从二级缓存拿创建中的对象
singletonObject = this.earlySingletonObjects.get(beanName);
//为空且允许获取创建中的对象
if (singletonObject == null && allowEarlyReference) {
//ReentrantLock尝试上锁,避免正在创建对象的线程感知不到对象被转移到其他集合
if (!this.singletonLock.tryLock()) {
//没拿到锁返回空
return null;
}
try {
// 二重校验,从一级缓存找正常对象(可能已经创建好了)
singletonObject = this.singletonObjects.get(beanName);
//为空,继续
if (singletonObject == null) {
//从二级缓存找对象(可能已经从三级缓存转移到二级缓存)
singletonObject = this.earlySingletonObjects.get(beanName);
//为空,继续
if (singletonObject == null) {
//从三级缓存找对象
ObjectFactory<?> singletonFactory = this.singletonFactories.get(beanName);
//有值,继续
if (singletonFactory != null) {
singletonObject = singletonFactory.getObject();
//将创建中的对象从三级移到二级缓存
if (this.singletonFactories.remove(beanName) != null) {
this.earlySingletonObjects.put(beanName, singletonObject);
}
else {
//没移动成功则说明,寻找的对象已经被创建完全并移动到了一级缓存
//(hashmap移除对象会返回旧值)
singletonObject = this.singletonObjects.get(beanName);
}
}
}
}
}
finally {
this.singletonLock.unlock();
}
}
}
return singletonObject;
}
A开始创建 → A放进三级缓存 → 发现A依赖B → B开始创建 → B放到三级缓存
→ 发现依赖A → 从三级缓存获取A并把A移动到二级缓存
→ B创建完成并放到一级缓存 → A拿到B
spring 6.2之后支持异步创建对象
// not to be exposed, just to lead to ClassCastException in case of mismatch
//不要暴露,只会在不匹配的情况下导致ClassCastException
CompletableFuture<?> future = CompletableFuture.runAsync(
() -> instantiateSingletonInBackgroundThread(beanName), executor);
addSingletonFactory(beanName, () -> {
try {
future.join();
}
catch (CompletionException ex) {
ReflectionUtils.rethrowRuntimeException(ex.getCause());
}
return future; // not to be exposed, just to lead to ClassCastException in case of mismatch
});