Skip to content

Commit 13a68c6

Browse files
committed
AbstractBeanFactory removes alreadyCreated entry after bean creation failure
Issue: SPR-10896 (cherry picked from commit e213561)
1 parent 6110919 commit 13a68c6

File tree

1 file changed

+72
-58
lines changed

1 file changed

+72
-58
lines changed

spring-beans/src/main/java/org/springframework/beans/factory/support/AbstractBeanFactory.java

Lines changed: 72 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -275,77 +275,83 @@ protected <T> T doGetBean(
275275
markBeanAsCreated(beanName);
276276
}
277277

278-
final RootBeanDefinition mbd = getMergedLocalBeanDefinition(beanName);
279-
checkMergedBeanDefinition(mbd, beanName, args);
280-
281-
// Guarantee initialization of beans that the current bean depends on.
282-
String[] dependsOn = mbd.getDependsOn();
283-
if (dependsOn != null) {
284-
for (String dependsOnBean : dependsOn) {
285-
getBean(dependsOnBean);
286-
registerDependentBean(dependsOnBean, beanName);
287-
}
288-
}
289-
290-
// Create bean instance.
291-
if (mbd.isSingleton()) {
292-
sharedInstance = getSingleton(beanName, new ObjectFactory<Object>() {
293-
public Object getObject() throws BeansException {
294-
try {
295-
return createBean(beanName, mbd, args);
296-
}
297-
catch (BeansException ex) {
298-
// Explicitly remove instance from singleton cache: It might have been put there
299-
// eagerly by the creation process, to allow for circular reference resolution.
300-
// Also remove any beans that received a temporary reference to the bean.
301-
destroySingleton(beanName);
302-
throw ex;
303-
}
278+
try {
279+
final RootBeanDefinition mbd = getMergedLocalBeanDefinition(beanName);
280+
checkMergedBeanDefinition(mbd, beanName, args);
281+
282+
// Guarantee initialization of beans that the current bean depends on.
283+
String[] dependsOn = mbd.getDependsOn();
284+
if (dependsOn != null) {
285+
for (String dependsOnBean : dependsOn) {
286+
getBean(dependsOnBean);
287+
registerDependentBean(dependsOnBean, beanName);
304288
}
305-
});
306-
bean = getObjectForBeanInstance(sharedInstance, name, beanName, mbd);
307-
}
308-
309-
else if (mbd.isPrototype()) {
310-
// It's a prototype -> create a new instance.
311-
Object prototypeInstance = null;
312-
try {
313-
beforePrototypeCreation(beanName);
314-
prototypeInstance = createBean(beanName, mbd, args);
315-
}
316-
finally {
317-
afterPrototypeCreation(beanName);
318289
}
319-
bean = getObjectForBeanInstance(prototypeInstance, name, beanName, mbd);
320-
}
321290

322-
else {
323-
String scopeName = mbd.getScope();
324-
final Scope scope = this.scopes.get(scopeName);
325-
if (scope == null) {
326-
throw new IllegalStateException("No Scope registered for scope '" + scopeName + "'");
327-
}
328-
try {
329-
Object scopedInstance = scope.get(beanName, new ObjectFactory<Object>() {
291+
// Create bean instance.
292+
if (mbd.isSingleton()) {
293+
sharedInstance = getSingleton(beanName, new ObjectFactory<Object>() {
330294
public Object getObject() throws BeansException {
331-
beforePrototypeCreation(beanName);
332295
try {
333296
return createBean(beanName, mbd, args);
334297
}
335-
finally {
336-
afterPrototypeCreation(beanName);
298+
catch (BeansException ex) {
299+
// Explicitly remove instance from singleton cache: It might have been put there
300+
// eagerly by the creation process, to allow for circular reference resolution.
301+
// Also remove any beans that received a temporary reference to the bean.
302+
destroySingleton(beanName);
303+
throw ex;
337304
}
338305
}
339306
});
340-
bean = getObjectForBeanInstance(scopedInstance, name, beanName, mbd);
307+
bean = getObjectForBeanInstance(sharedInstance, name, beanName, mbd);
308+
}
309+
310+
else if (mbd.isPrototype()) {
311+
// It's a prototype -> create a new instance.
312+
Object prototypeInstance = null;
313+
try {
314+
beforePrototypeCreation(beanName);
315+
prototypeInstance = createBean(beanName, mbd, args);
316+
}
317+
finally {
318+
afterPrototypeCreation(beanName);
319+
}
320+
bean = getObjectForBeanInstance(prototypeInstance, name, beanName, mbd);
341321
}
342-
catch (IllegalStateException ex) {
343-
throw new BeanCreationException(beanName,
344-
"Scope '" + scopeName + "' is not active for the current thread; " +
345-
"consider defining a scoped proxy for this bean if you intend to refer to it from a singleton",
346-
ex);
322+
323+
else {
324+
String scopeName = mbd.getScope();
325+
final Scope scope = this.scopes.get(scopeName);
326+
if (scope == null) {
327+
throw new IllegalStateException("No Scope registered for scope '" + scopeName + "'");
328+
}
329+
try {
330+
Object scopedInstance = scope.get(beanName, new ObjectFactory<Object>() {
331+
public Object getObject() throws BeansException {
332+
beforePrototypeCreation(beanName);
333+
try {
334+
return createBean(beanName, mbd, args);
335+
}
336+
finally {
337+
afterPrototypeCreation(beanName);
338+
}
339+
}
340+
});
341+
bean = getObjectForBeanInstance(scopedInstance, name, beanName, mbd);
342+
}
343+
catch (IllegalStateException ex) {
344+
throw new BeanCreationException(beanName,
345+
"Scope '" + scopeName + "' is not active for the current thread; " +
346+
"consider defining a scoped proxy for this bean if you intend to refer to it from a singleton",
347+
ex);
348+
}
347349
}
348350
}
351+
catch (BeansException ex) {
352+
cleanupAfterBeanCreationFailure(beanName);
353+
throw ex;
354+
}
349355
}
350356

351357
// Check if required type matches the type of the actual bean instance.
@@ -1388,6 +1394,14 @@ protected void markBeanAsCreated(String beanName) {
13881394
this.alreadyCreated.put(beanName, Boolean.TRUE);
13891395
}
13901396

1397+
/**
1398+
* Perform appropriate cleanup of cached metadata after bean creation failed.
1399+
* @param beanName the name of the bean
1400+
*/
1401+
protected void cleanupAfterBeanCreationFailure(String beanName) {
1402+
this.alreadyCreated.remove(beanName);
1403+
}
1404+
13911405
/**
13921406
* Determine whether the specified bean is eligible for having
13931407
* its bean definition metadata cached.

0 commit comments

Comments
 (0)