@@ -275,77 +275,83 @@ protected <T> T doGetBean(
275
275
markBeanAsCreated (beanName );
276
276
}
277
277
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 );
304
288
}
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 );
318
289
}
319
- bean = getObjectForBeanInstance (prototypeInstance , name , beanName , mbd );
320
- }
321
290
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 >() {
330
294
public Object getObject () throws BeansException {
331
- beforePrototypeCreation (beanName );
332
295
try {
333
296
return createBean (beanName , mbd , args );
334
297
}
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 ;
337
304
}
338
305
}
339
306
});
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 );
341
321
}
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
+ }
347
349
}
348
350
}
351
+ catch (BeansException ex ) {
352
+ cleanupAfterBeanCreationFailure (beanName );
353
+ throw ex ;
354
+ }
349
355
}
350
356
351
357
// Check if required type matches the type of the actual bean instance.
@@ -1388,6 +1394,14 @@ protected void markBeanAsCreated(String beanName) {
1388
1394
this .alreadyCreated .put (beanName , Boolean .TRUE );
1389
1395
}
1390
1396
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
+
1391
1405
/**
1392
1406
* Determine whether the specified bean is eligible for having
1393
1407
* its bean definition metadata cached.
0 commit comments