Search in sources :

Example 91 with DefaultListableBeanFactory

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());
}
Also used : TestBean(org.springframework.tests.sample.beans.TestBean) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) ClassPathResource(org.springframework.core.io.ClassPathResource) Test(org.junit.Test)

Example 92 with DefaultListableBeanFactory

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
    }
}
Also used : BeanCreationException(org.springframework.beans.factory.BeanCreationException) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) ClassPathResource(org.springframework.core.io.ClassPathResource) Test(org.junit.Test)

Example 93 with DefaultListableBeanFactory

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)"));
    }
}
Also used : BeanCreationException(org.springframework.beans.factory.BeanCreationException) TestBean(org.springframework.tests.sample.beans.TestBean) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) ClassPathResource(org.springframework.core.io.ClassPathResource) Test(org.junit.Test)

Example 94 with DefaultListableBeanFactory

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());
}
Also used : TestBean(org.springframework.tests.sample.beans.TestBean) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) ClassPathResource(org.springframework.core.io.ClassPathResource) Test(org.junit.Test)

Example 95 with DefaultListableBeanFactory

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());
}
Also used : TestBean(org.springframework.tests.sample.beans.TestBean) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) ClassPathResource(org.springframework.core.io.ClassPathResource) Test(org.junit.Test)

Aggregations

DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)652 Test (org.junit.Test)576 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)317 TestBean (org.springframework.tests.sample.beans.TestBean)249 ITestBean (org.springframework.tests.sample.beans.ITestBean)199 NestedTestBean (org.springframework.tests.sample.beans.NestedTestBean)152 IndexedTestBean (org.springframework.tests.sample.beans.IndexedTestBean)101 DerivedTestBean (org.springframework.tests.sample.beans.DerivedTestBean)96 XmlBeanDefinitionReader (org.springframework.beans.factory.xml.XmlBeanDefinitionReader)82 ClassPathResource (org.springframework.core.io.ClassPathResource)80 Properties (java.util.Properties)32 Before (org.junit.Before)27 BeanCreationException (org.springframework.beans.factory.BeanCreationException)26 ResourceTestBean (org.springframework.tests.sample.beans.ResourceTestBean)26 MutablePropertyValues (org.springframework.beans.MutablePropertyValues)24 HashMap (java.util.HashMap)22 PropertiesBeanDefinitionReader (org.springframework.beans.factory.support.PropertiesBeanDefinitionReader)22 DependenciesBean (org.springframework.tests.sample.beans.DependenciesBean)17 ConstructorDependenciesBean (org.springframework.beans.factory.xml.ConstructorDependenciesBean)16 BeanDefinition (org.springframework.beans.factory.config.BeanDefinition)14