Skip to content

Commit 82651a0

Browse files
committed
BeanFactory accepts getBean arguments for non-prototype beans as well
Issue: SPR-12488
1 parent c85686a commit 82651a0

File tree

1 file changed

+16
-18
lines changed

1 file changed

+16
-18
lines changed

spring-beans/src/test/java/org/springframework/beans/factory/xml/FactoryMethodTests.java

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import org.junit.Test;
2525

2626
import org.springframework.beans.factory.BeanCreationException;
27-
import org.springframework.beans.factory.BeanDefinitionStoreException;
2827
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
2928
import org.springframework.core.io.ClassPathResource;
3029
import org.springframework.tests.sample.beans.TestBean;
@@ -325,32 +324,32 @@ public void testCanSpecifyFactoryMethodArgumentsOnFactoryMethodPrototype() {
325324
}
326325

327326
@Test
328-
public void testCannotSpecifyFactoryMethodArgumentsOnSingleton() {
327+
public void testCanSpecifyFactoryMethodArgumentsOnSingleton() {
329328
DefaultListableBeanFactory xbf = new DefaultListableBeanFactory();
330329
XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(xbf);
331330
reader.loadBeanDefinitions(new ClassPathResource("factory-methods.xml", getClass()));
332-
try {
333-
xbf.getBean("testBeanOnly", new TestBean());
334-
fail("Shouldn't allow args to be passed to a singleton");
335-
}
336-
catch (BeanDefinitionStoreException ex) {
337-
// OK
338-
}
331+
332+
// First getBean call triggers actual creation of the singleton bean
333+
TestBean tb = new TestBean();
334+
FactoryMethods fm1 = (FactoryMethods) xbf.getBean("testBeanOnly", tb);
335+
assertSame(tb, fm1.getTestBean());
336+
FactoryMethods fm2 = (FactoryMethods) xbf.getBean("testBeanOnly", new TestBean());
337+
assertSame(fm1, fm2);
338+
assertSame(tb, fm2.getTestBean());
339339
}
340340

341341
@Test
342342
public void testCannotSpecifyFactoryMethodArgumentsOnSingletonAfterCreation() {
343343
DefaultListableBeanFactory xbf = new DefaultListableBeanFactory();
344344
XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(xbf);
345345
reader.loadBeanDefinitions(new ClassPathResource("factory-methods.xml", getClass()));
346-
xbf.getBean("testBeanOnly");
347-
try {
348-
xbf.getBean("testBeanOnly", new TestBean());
349-
fail("Shouldn't allow args to be passed to a singleton");
350-
}
351-
catch (BeanDefinitionStoreException ex) {
352-
// OK
353-
}
346+
347+
// First getBean call triggers actual creation of the singleton bean
348+
FactoryMethods fm1 = (FactoryMethods) xbf.getBean("testBeanOnly");
349+
TestBean tb = fm1.getTestBean();
350+
FactoryMethods fm2 = (FactoryMethods) xbf.getBean("testBeanOnly", new TestBean());
351+
assertSame(fm1, fm2);
352+
assertSame(tb, fm2.getTestBean());
354353
}
355354

356355
@Test
@@ -386,7 +385,6 @@ public void testFactoryMethodForJavaMailSession() {
386385
assertEquals("someuser", session.getProperty("mail.smtp.user"));
387386
assertEquals("somepw", session.getProperty("mail.smtp.password"));
388387
}
389-
390388
}
391389

392390

0 commit comments

Comments
 (0)