LazyInitTargetSource works for @Bean targets as well

Issue: SPR-10508
Issue: SPR-8080
This commit is contained in:
Juergen Hoeller
2014-09-02 22:13:35 +02:00
parent b64f680f19
commit 8c9274e017
4 changed files with 243 additions and 3 deletions

View File

@@ -372,6 +372,10 @@ class ConstructorResolver {
throw new BeanCreationException(mbd.getResourceDescription(), beanName,
"factory-bean '" + factoryBeanName + "' (or a BeanPostProcessor involved) returned null");
}
if (mbd.isSingleton() && this.beanFactory.containsSingleton(beanName)) {
throw new IllegalStateException("About-to-be-created singleton instance implicitly appeared " +
"through the creation of the factory bean that its bean definition points to");
}
factoryClass = factoryBean.getClass();
isStatic = false;
}
@@ -904,4 +908,5 @@ class ConstructorResolver {
}
}
}
}

View File

@@ -220,12 +220,22 @@ public class DefaultSingletonBeanRegistry extends SimpleAliasRegistry implements
logger.debug("Creating shared instance of singleton bean '" + beanName + "'");
}
beforeSingletonCreation(beanName);
boolean newSingleton = false;
boolean recordSuppressedExceptions = (this.suppressedExceptions == null);
if (recordSuppressedExceptions) {
this.suppressedExceptions = new LinkedHashSet<Exception>();
}
try {
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) {
@@ -241,7 +251,9 @@ public class DefaultSingletonBeanRegistry extends SimpleAliasRegistry implements
}
afterSingletonCreation(beanName);
}
addSingleton(beanName, singletonObject);
if (newSingleton) {
addSingleton(beanName, singletonObject);
}
}
return (singletonObject != NULL_OBJECT ? singletonObject : null);
}