use of org.springframework.beans.factory.support.DefaultListableBeanFactory in project spring-framework by spring-projects.
the class MailSession method testFactoryMethodsSingletonOnTargetClass.
@Test
public void testFactoryMethodsSingletonOnTargetClass() {
DefaultListableBeanFactory xbf = new DefaultListableBeanFactory();
XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(xbf);
reader.loadBeanDefinitions(new ClassPathResource("factory-methods.xml", getClass()));
TestBean tb = (TestBean) xbf.getBean("defaultTestBean");
assertEquals("defaultInstance", tb.getName());
assertEquals(1, tb.getAge());
FactoryMethods fm = (FactoryMethods) xbf.getBean("default");
assertEquals(0, fm.getNum());
assertEquals("default", fm.getName());
assertEquals("defaultInstance", fm.getTestBean().getName());
assertEquals("setterString", fm.getStringValue());
fm = (FactoryMethods) xbf.getBean("testBeanOnly");
assertEquals(0, fm.getNum());
assertEquals("default", fm.getName());
// This comes from the test bean
assertEquals("Juergen", fm.getTestBean().getName());
fm = (FactoryMethods) xbf.getBean("full");
assertEquals(27, fm.getNum());
assertEquals("gotcha", fm.getName());
assertEquals("Juergen", fm.getTestBean().getName());
FactoryMethods fm2 = (FactoryMethods) xbf.getBean("full");
assertSame(fm, fm2);
xbf.destroySingletons();
assertTrue(tb.wasDestroyed());
}
use of org.springframework.beans.factory.support.DefaultListableBeanFactory in project spring-framework by spring-projects.
the class MailSession method testFactoryMethodsWithNullInstance.
@Test
public void testFactoryMethodsWithNullInstance() {
DefaultListableBeanFactory xbf = new DefaultListableBeanFactory();
XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(xbf);
reader.loadBeanDefinitions(new ClassPathResource("factory-methods.xml", getClass()));
FactoryMethods fm = (FactoryMethods) xbf.getBean("null");
assertNull(fm);
try {
xbf.getBean("nullWithProperty");
fail("Should have thrown BeanCreationException");
} catch (BeanCreationException ex) {
// expected
}
}
use of org.springframework.beans.factory.support.DefaultListableBeanFactory in project spring-framework by spring-projects.
the class MailSession method testFactoryMethodArgumentsForNonExistingMethod.
@Test
public void testFactoryMethodArgumentsForNonExistingMethod() {
DefaultListableBeanFactory xbf = new DefaultListableBeanFactory();
XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(xbf);
reader.loadBeanDefinitions(new ClassPathResource("factory-methods.xml", getClass()));
try {
xbf.getBean("invalidPrototype", new TestBean());
fail("Should have thrown BeanCreationException");
} catch (BeanCreationException ex) {
assertTrue(ex.getMessage().contains("nonExisting(TestBean)"));
}
}
use of org.springframework.beans.factory.support.DefaultListableBeanFactory in project spring-framework by spring-projects.
the class MailSession method testCannotSpecifyFactoryMethodArgumentsOnSingletonAfterCreation.
@Test
public void testCannotSpecifyFactoryMethodArgumentsOnSingletonAfterCreation() {
DefaultListableBeanFactory xbf = new DefaultListableBeanFactory();
XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(xbf);
reader.loadBeanDefinitions(new ClassPathResource("factory-methods.xml", getClass()));
// First getBean call triggers actual creation of the singleton bean
FactoryMethods fm1 = (FactoryMethods) xbf.getBean("testBeanOnly");
TestBean tb = fm1.getTestBean();
FactoryMethods fm2 = (FactoryMethods) xbf.getBean("testBeanOnly", new TestBean());
assertSame(fm1, fm2);
assertSame(tb, fm2.getTestBean());
}
use of org.springframework.beans.factory.support.DefaultListableBeanFactory in project spring-framework by spring-projects.
the class MailSession method testCanSpecifyFactoryMethodArgumentsOnSingleton.
@Test
public void testCanSpecifyFactoryMethodArgumentsOnSingleton() {
DefaultListableBeanFactory xbf = new DefaultListableBeanFactory();
XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(xbf);
reader.loadBeanDefinitions(new ClassPathResource("factory-methods.xml", getClass()));
// First getBean call triggers actual creation of the singleton bean
TestBean tb = new TestBean();
FactoryMethods fm1 = (FactoryMethods) xbf.getBean("testBeanOnly", tb);
assertSame(tb, fm1.getTestBean());
FactoryMethods fm2 = (FactoryMethods) xbf.getBean("testBeanOnly", new TestBean());
assertSame(fm1, fm2);
assertSame(tb, fm2.getTestBean());
}
Aggregations