|
24 | 24 | import org.junit.Test;
|
25 | 25 |
|
26 | 26 | import org.springframework.beans.factory.BeanCreationException;
|
27 |
| -import org.springframework.beans.factory.BeanDefinitionStoreException; |
28 | 27 | import org.springframework.beans.factory.support.DefaultListableBeanFactory;
|
29 | 28 | import org.springframework.core.io.ClassPathResource;
|
30 | 29 | import org.springframework.tests.sample.beans.TestBean;
|
@@ -325,32 +324,32 @@ public void testCanSpecifyFactoryMethodArgumentsOnFactoryMethodPrototype() {
|
325 | 324 | }
|
326 | 325 |
|
327 | 326 | @Test
|
328 |
| - public void testCannotSpecifyFactoryMethodArgumentsOnSingleton() { |
| 327 | + public void testCanSpecifyFactoryMethodArgumentsOnSingleton() { |
329 | 328 | DefaultListableBeanFactory xbf = new DefaultListableBeanFactory();
|
330 | 329 | XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(xbf);
|
331 | 330 | 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()); |
339 | 339 | }
|
340 | 340 |
|
341 | 341 | @Test
|
342 | 342 | public void testCannotSpecifyFactoryMethodArgumentsOnSingletonAfterCreation() {
|
343 | 343 | DefaultListableBeanFactory xbf = new DefaultListableBeanFactory();
|
344 | 344 | XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(xbf);
|
345 | 345 | 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()); |
354 | 353 | }
|
355 | 354 |
|
356 | 355 | @Test
|
@@ -386,7 +385,6 @@ public void testFactoryMethodForJavaMailSession() {
|
386 | 385 | assertEquals("someuser", session.getProperty("mail.smtp.user"));
|
387 | 386 | assertEquals("somepw", session.getProperty("mail.smtp.password"));
|
388 | 387 | }
|
389 |
| - |
390 | 388 | }
|
391 | 389 |
|
392 | 390 |
|
|
0 commit comments