A对象的创建,需要B对象;B对象的创建需要A对象,此时便出现循环依赖问题,A和B都无法创建成功。
@Service
public class ServiceA {
@Autowired
private ServiceB serviceB;
}
@Service
public class ServiceB {
@Autowired
private ServiceA serviceA;
}
singletonObjects
:单例池,一级缓存。earlySingletonObjects
:未完成初始化的单例池,也称之为二级缓存 。singletonFactories
:三级缓存,存放的是创建对象的lambda表达式。如果是普通对象直接返回,如果需要AOP则创建代理对象。当你看到 Spring 中提供了 get/set 或者注解,这样之所以能解决,首先是进行了一定的解耦。让类的创建和属性的填充分离,先创建出半成品Bean,再处理属性的填充,完成成品Bean的提供。
AbstractBeanFactory#doGetBean
。从单例池中获取对象,DefaultSingletonBeanRegistry#getSingleton(String beanName, boolean allowEarlyReference)
。 @Nullable
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) {
ObjectFactory<?> singletonFactory = this.singletonFactories.get(beanName);
if (singletonFactory != null) {
singletonObject = singletonFactory.getObject();
this.earlySingletonObjects.put(beanName, singletonObject);
this.singletonFactories.remove(beanName);
}
}
}
}
}
}
return singletonObject;
}
DefaultSingletonBeanRegistry#getSingleton(String beanName, ObjectFactory> singletonFactory)
,用传入的lambda表达式来创建对象。 if (mbd.isSingleton()) {
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;
}
});
bean = getObjectForBeanInstance(sharedInstance, name, beanName, mbd);
}
创建bean对象,AbstractAutowireCapableBeanFactory#createBean()
。在走到真正的创建对象之前会将三级缓存设置。
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));
}
AbstractAutowireCapableBeanFactory#populateBean
earlySingletonObject
。earlySingletonObject
不存在,调用三级缓存的对象工厂生成。AbstractAutowireCapableBeanFactory#getEarlyBeanReference
。 protected Object getEarlyBeanReference(String beanName, RootBeanDefinition mbd, Object bean) {
Object exposedObject = bean;
if (!mbd.isSynthetic() && hasInstantiationAwareBeanPostProcessors()) {
for (BeanPostProcessor bp : getBeanPostProcessors()) {
if (bp instanceof SmartInstantiationAwareBeanPostProcessor) {
SmartInstantiationAwareBeanPostProcessor ibp = (SmartInstantiationAwareBeanPostProcessor) bp;
exposedObject = ibp.getEarlyBeanReference(exposedObject, beanName);
}
}
}
return exposedObject;
}
earlySingletonObject
@Component
public class ABean {
private BBean bBean;
public ABean(BBean bBean) {
this.bBean = bBean;
System.out.println("init");
}
}
@Component
public class BBean {
@Autowired
private ABean aBean;
public BBean(ABean aBean) {
this.aBean = aBean;
}
}
AbstractAutowireCapableBeanFactory#doCreateBean->AbstractAutowireCapableBeanFactory#createBeanInstance
。 // 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);
}
找到构造器需要的B对象,进行生成。当B对象生成需要设置属性A的时候,再次获取A对象的时候,由于三级缓存中没有A对象的bean创建工厂,无法创建earlySingletonObject
。
@Service
public class A {
@Autowired
B b;
@Async
public void method() {
b.xx();
}
}
@Service
public class B {
@Autowired
A a;
}
AbstractAutowireCapableBeanFactory#initializeBean()
,会根据@Async
生成代理对象。AsyncAnnotationBeanPostProcessor
是BeanPostProcessor
,不会在生成早期对象的时候进行代理。exposedObject
是经过AsyncAnnotationBeanPostProcessor
后置处理器处理过的。所以exposedObject
和bean不相等。这时候B对象设置的是A的earlySingletonReference
,是原始对象。而准备生成的实例却是代理对象。@Transactional
,不是@Async
,不会报错。原因是exposedObject == bean
,都是原始对象。带有Transactional
的对象在生成早期对象的时候,会执行AbstractAdvisorAutoProxyCreator#findCandidateAdvisors
,会获取到BeanFactoryTransactionAttributeSourceAdvisor
。代理对象应该是暴露出去的对象,所以进行赋值。@Transactional
或者@Aspect
拦截,该早期对象是代理对象)。 //AbstractAutowireCapableBeanFactory#doCreateBean
if (earlySingletonExposure) {
//获取earlySingletonReference,如果没有,不创建
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.");
}
}
}
}