org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory#doCreateBean
if (earlySingletonExposure) {
Object earlySingletonReference = getSingleton(beanName, false);
if (earlySingletonReference != null) {
if (exposedObject == bean) {
exposedObject = earlySingletonReference;
}
else if (!this.allowRawInjectionDespiteWrapping && hasDependentBean(beanName)) {
// omit code...
}
}
}
If the earlySingletonReference != null
condition is true, it means that a circular dependency has occurred and the object has been exposed in advance. Since it is a circular dependency, the exposedObject == bean
condition must be true. If there is no circular dependency, the exposedObject can only be modified during the initialization phase. Whether to consider removing the if (exposedObject == bean)
condition to simplify the code logic.
if (earlySingletonExposure) {
Object earlySingletonReference = getSingleton(beanName, false);
if (earlySingletonReference != null) {
exposedObject = earlySingletonReference;
if (!this.allowRawInjectionDespiteWrapping && hasDependentBean(beanName)) {
// omit code...
}
}
}
Comment From: snicoll
Since it is a circular dependency, the exposedObject == bean condition must be true.
I don't think it's that obvious. You may have a post processor triggering and changing the bean instance. If you're looking for optimizing the code, I am afraid that's the wrong place to look at. In the absence of evidence of a concrete problem I am going to close this but we can reopen if you attach a test case that describes concretely why the check must be removed.