Skip to content

Commit 3c3e07e

Browse files
committed
Defensive handling of manually registered singleton names (based on Spring Integration test failure)
Issue: SPR-12404
1 parent bba38b8 commit 3c3e07e

File tree

2 files changed

+23
-10
lines changed

2 files changed

+23
-10
lines changed

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -469,8 +469,7 @@ private String[] doGetBeanNamesForType(Class<?> type, boolean includeNonSingleto
469469

470470
// Check manually registered singletons too.
471471
for (String beanName : this.manualSingletonNames) {
472-
// Only check if manually registered.
473-
if (!containsBeanDefinition(beanName)) {
472+
try {
474473
// In case of FactoryBean, match object created by FactoryBean.
475474
if (isFactoryBean(beanName)) {
476475
if ((includeNonSingletons || isSingleton(beanName)) && isTypeMatch(beanName, type)) {
@@ -486,6 +485,12 @@ private String[] doGetBeanNamesForType(Class<?> type, boolean includeNonSingleto
486485
result.add(beanName);
487486
}
488487
}
488+
catch (NoSuchBeanDefinitionException ex) {
489+
// Shouldn't happen - probably a result of circular reference resolution...
490+
if (logger.isDebugEnabled()) {
491+
logger.debug("Failed to check manually registered singleton with name '" + beanName + "'", ex);
492+
}
493+
}
489494
}
490495

491496
return StringUtils.toStringArray(result);

spring-context/src/test/java/org/springframework/context/annotation/ClassPathFactoryBeanDefinitionScannerTests.java

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2013 the original author or authors.
2+
* Copyright 2002-2014 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,7 +16,7 @@
1616

1717
package org.springframework.context.annotation;
1818

19-
import junit.framework.TestCase;
19+
import org.junit.Test;
2020

2121
import org.springframework.aop.scope.ScopedObject;
2222
import org.springframework.aop.support.AopUtils;
@@ -25,20 +25,24 @@
2525
import org.springframework.beans.factory.support.RootBeanDefinition;
2626
import org.springframework.context.annotation4.DependencyBean;
2727
import org.springframework.context.annotation4.FactoryMethodComponent;
28+
import org.springframework.context.support.AbstractApplicationContext;
2829
import org.springframework.context.support.GenericApplicationContext;
2930
import org.springframework.tests.context.SimpleMapScope;
3031
import org.springframework.tests.sample.beans.TestBean;
3132
import org.springframework.util.ClassUtils;
3233

34+
import static org.junit.Assert.*;
35+
3336
/**
3437
* @author Mark Pollack
3538
* @author Juergen Hoeller
3639
*/
37-
public class ClassPathFactoryBeanDefinitionScannerTests extends TestCase {
40+
public class ClassPathFactoryBeanDefinitionScannerTests {
3841

3942
private static final String BASE_PACKAGE = FactoryMethodComponent.class.getPackage().getName();
4043

4144

45+
@Test
4246
public void testSingletonScopedFactoryMethod() {
4347
GenericApplicationContext context = new GenericApplicationContext();
4448
ClassPathBeanDefinitionScanner scanner = new ClassPathBeanDefinitionScanner(context);
@@ -52,13 +56,13 @@ public void testSingletonScopedFactoryMethod() {
5256
FactoryMethodComponent fmc = context.getBean("factoryMethodComponent", FactoryMethodComponent.class);
5357
assertFalse(fmc.getClass().getName().contains(ClassUtils.CGLIB_CLASS_SEPARATOR));
5458

55-
TestBean tb = (TestBean)context.getBean("publicInstance"); //2
59+
TestBean tb = (TestBean) context.getBean("publicInstance"); //2
5660
assertEquals("publicInstance", tb.getName());
57-
TestBean tb2 = (TestBean)context.getBean("publicInstance"); //2
61+
TestBean tb2 = (TestBean) context.getBean("publicInstance"); //2
5862
assertEquals("publicInstance", tb2.getName());
5963
assertSame(tb2, tb);
6064

61-
tb = (TestBean)context.getBean("protectedInstance"); //3
65+
tb = (TestBean) context.getBean("protectedInstance"); //3
6266
assertEquals("protectedInstance", tb.getName());
6367
assertSame(tb, context.getBean("protectedInstance"));
6468
assertEquals("0", tb.getCountry());
@@ -78,8 +82,9 @@ public void testSingletonScopedFactoryMethod() {
7882
assertTrue(bean instanceof ScopedObject);
7983

8084
QualifiedClientBean clientBean = context.getBean("clientBean", QualifiedClientBean.class);
81-
assertSame(clientBean.testBean, context.getBean("publicInstance"));
82-
assertSame(clientBean.dependencyBean, context.getBean("dependencyBean"));
85+
assertSame(context.getBean("publicInstance"), clientBean.testBean);
86+
assertSame(context.getBean("dependencyBean"), clientBean.dependencyBean);
87+
assertSame(context, clientBean.applicationContext);
8388
}
8489

8590

@@ -90,6 +95,9 @@ public static class QualifiedClientBean {
9095

9196
@Autowired
9297
public DependencyBean dependencyBean;
98+
99+
@Autowired
100+
AbstractApplicationContext applicationContext;
93101
}
94102

95103
}

0 commit comments

Comments
 (0)