Search in sources :

Example 16 with PropertiesBeanDefinitionReader

use of org.springframework.beans.factory.support.PropertiesBeanDefinitionReader in project spring-framework by spring-projects.

the class DefaultListableBeanFactoryTests method testEmptyPropertiesPopulation.

@Test
public void testEmptyPropertiesPopulation() {
    DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
    Properties p = new Properties();
    (new PropertiesBeanDefinitionReader(lbf)).registerBeanDefinitions(p);
    assertTrue("No beans defined after ignorable invalid", lbf.getBeanDefinitionCount() == 0);
}
Also used : PropertiesBeanDefinitionReader(org.springframework.beans.factory.support.PropertiesBeanDefinitionReader) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) Properties(java.util.Properties) Test(org.junit.Test)

Example 17 with PropertiesBeanDefinitionReader

use of org.springframework.beans.factory.support.PropertiesBeanDefinitionReader in project spring-framework by spring-projects.

the class DefaultListableBeanFactoryTests method testPropertiesPopulationWithNullPrefix.

@Test
public void testPropertiesPopulationWithNullPrefix() {
    DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
    Properties p = new Properties();
    p.setProperty("test.(class)", TestBean.class.getName());
    p.setProperty("test.name", "Tony");
    p.setProperty("test.age", "48");
    int count = (new PropertiesBeanDefinitionReader(lbf)).registerBeanDefinitions(p);
    assertTrue("1 beans registered, not " + count, count == 1);
    testSingleTestBean(lbf);
}
Also used : ITestBean(org.springframework.tests.sample.beans.ITestBean) DerivedTestBean(org.springframework.tests.sample.beans.DerivedTestBean) TestBean(org.springframework.tests.sample.beans.TestBean) NestedTestBean(org.springframework.tests.sample.beans.NestedTestBean) PropertiesBeanDefinitionReader(org.springframework.beans.factory.support.PropertiesBeanDefinitionReader) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) Properties(java.util.Properties) Test(org.junit.Test)

Example 18 with PropertiesBeanDefinitionReader

use of org.springframework.beans.factory.support.PropertiesBeanDefinitionReader in project spring-framework by spring-projects.

the class DefaultListableBeanFactoryTests method testPrototypeExtendsPrototype.

@Test
public void testPrototypeExtendsPrototype() {
    DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
    Properties p = new Properties();
    p.setProperty("wife.(class)", TestBean.class.getName());
    p.setProperty("wife.name", "kerry");
    p.setProperty("kerry.(parent)", "wife");
    p.setProperty("kerry.age", "35");
    (new PropertiesBeanDefinitionReader(lbf)).registerBeanDefinitions(p);
    TestBean kerry1 = (TestBean) lbf.getBean("kerry");
    TestBean kerry2 = (TestBean) lbf.getBean("kerry");
    assertEquals("kerry", kerry1.getName());
    assertNotNull("Non null", kerry1);
    assertTrue("Singletons equal", kerry1 == kerry2);
    lbf = new DefaultListableBeanFactory();
    p = new Properties();
    p.setProperty("wife.(class)", TestBean.class.getName());
    p.setProperty("wife.name", "kerry");
    p.setProperty("wife.(singleton)", "false");
    p.setProperty("kerry.(parent)", "wife");
    p.setProperty("kerry.(singleton)", "false");
    p.setProperty("kerry.age", "35");
    (new PropertiesBeanDefinitionReader(lbf)).registerBeanDefinitions(p);
    assertFalse(lbf.isSingleton("kerry"));
    kerry1 = (TestBean) lbf.getBean("kerry");
    kerry2 = (TestBean) lbf.getBean("kerry");
    assertTrue("Non null", kerry1 != null);
    assertTrue("Prototypes NOT equal", kerry1 != kerry2);
    lbf = new DefaultListableBeanFactory();
    p = new Properties();
    p.setProperty("kerry.(class)", TestBean.class.getName());
    p.setProperty("kerry.(singleton)", "true");
    p.setProperty("kerry.age", "35");
    (new PropertiesBeanDefinitionReader(lbf)).registerBeanDefinitions(p);
    kerry1 = (TestBean) lbf.getBean("kerry");
    kerry2 = (TestBean) lbf.getBean("kerry");
    assertTrue("Non null", kerry1 != null);
    assertTrue("Specified singletons equal", kerry1 == kerry2);
}
Also used : ITestBean(org.springframework.tests.sample.beans.ITestBean) DerivedTestBean(org.springframework.tests.sample.beans.DerivedTestBean) TestBean(org.springframework.tests.sample.beans.TestBean) NestedTestBean(org.springframework.tests.sample.beans.NestedTestBean) PropertiesBeanDefinitionReader(org.springframework.beans.factory.support.PropertiesBeanDefinitionReader) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) Properties(java.util.Properties) Test(org.junit.Test)

Example 19 with PropertiesBeanDefinitionReader

use of org.springframework.beans.factory.support.PropertiesBeanDefinitionReader in project spring-framework by spring-projects.

the class DefaultListableBeanFactoryTests method testRegisterExistingSingletonWithNameOverriding.

@Test
public void testRegisterExistingSingletonWithNameOverriding() {
    DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
    Properties p = new Properties();
    p.setProperty("test.(class)", TestBean.class.getName());
    p.setProperty("test.name", "Tony");
    p.setProperty("test.age", "48");
    p.setProperty("test.spouse(ref)", "singletonObject");
    (new PropertiesBeanDefinitionReader(lbf)).registerBeanDefinitions(p);
    lbf.registerBeanDefinition("singletonObject", new RootBeanDefinition(PropertiesFactoryBean.class));
    Object singletonObject = new TestBean();
    lbf.registerSingleton("singletonObject", singletonObject);
    lbf.preInstantiateSingletons();
    assertTrue(lbf.isSingleton("singletonObject"));
    assertEquals(TestBean.class, lbf.getType("singletonObject"));
    TestBean test = (TestBean) lbf.getBean("test");
    assertEquals(singletonObject, lbf.getBean("singletonObject"));
    assertEquals(singletonObject, test.getSpouse());
    Map<?, ?> beansOfType = lbf.getBeansOfType(TestBean.class, false, true);
    assertEquals(2, beansOfType.size());
    assertTrue(beansOfType.containsValue(test));
    assertTrue(beansOfType.containsValue(singletonObject));
    beansOfType = lbf.getBeansOfType(null, false, true);
    Iterator<String> beanNames = lbf.getBeanNamesIterator();
    assertEquals("test", beanNames.next());
    assertEquals("singletonObject", beanNames.next());
    assertFalse(beanNames.hasNext());
    assertEquals(2, beansOfType.size());
    assertTrue(lbf.containsSingleton("test"));
    assertTrue(lbf.containsSingleton("singletonObject"));
    assertTrue(lbf.containsBeanDefinition("test"));
    assertTrue(lbf.containsBeanDefinition("singletonObject"));
}
Also used : ITestBean(org.springframework.tests.sample.beans.ITestBean) DerivedTestBean(org.springframework.tests.sample.beans.DerivedTestBean) TestBean(org.springframework.tests.sample.beans.TestBean) NestedTestBean(org.springframework.tests.sample.beans.NestedTestBean) PropertiesBeanDefinitionReader(org.springframework.beans.factory.support.PropertiesBeanDefinitionReader) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) PropertiesFactoryBean(org.springframework.beans.factory.config.PropertiesFactoryBean) Properties(java.util.Properties) Test(org.junit.Test)

Example 20 with PropertiesBeanDefinitionReader

use of org.springframework.beans.factory.support.PropertiesBeanDefinitionReader in project spring-framework by spring-projects.

the class DefaultListableBeanFactoryTests method testPrototypeCircleLeadsToException.

@Test
public void testPrototypeCircleLeadsToException() {
    DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
    Properties p = new Properties();
    p.setProperty("kerry.(class)", TestBean.class.getName());
    p.setProperty("kerry.(singleton)", "false");
    p.setProperty("kerry.age", "35");
    p.setProperty("kerry.spouse", "*rod");
    p.setProperty("rod.(class)", TestBean.class.getName());
    p.setProperty("rod.(singleton)", "false");
    p.setProperty("rod.age", "34");
    p.setProperty("rod.spouse", "*kerry");
    (new PropertiesBeanDefinitionReader(lbf)).registerBeanDefinitions(p);
    try {
        lbf.getBean("kerry");
        fail("Should have thrown BeanCreationException");
    } catch (BeanCreationException ex) {
        // expected
        assertTrue(ex.contains(BeanCurrentlyInCreationException.class));
    }
}
Also used : ITestBean(org.springframework.tests.sample.beans.ITestBean) DerivedTestBean(org.springframework.tests.sample.beans.DerivedTestBean) TestBean(org.springframework.tests.sample.beans.TestBean) NestedTestBean(org.springframework.tests.sample.beans.NestedTestBean) PropertiesBeanDefinitionReader(org.springframework.beans.factory.support.PropertiesBeanDefinitionReader) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) Properties(java.util.Properties) Test(org.junit.Test)

Aggregations

Properties (java.util.Properties)22 Test (org.junit.Test)22 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)22 PropertiesBeanDefinitionReader (org.springframework.beans.factory.support.PropertiesBeanDefinitionReader)22 DerivedTestBean (org.springframework.tests.sample.beans.DerivedTestBean)17 ITestBean (org.springframework.tests.sample.beans.ITestBean)17 NestedTestBean (org.springframework.tests.sample.beans.NestedTestBean)17 TestBean (org.springframework.tests.sample.beans.TestBean)17 DummyFactory (org.springframework.tests.sample.beans.factory.DummyFactory)5 BeansException (org.springframework.beans.BeansException)1 PropertiesFactoryBean (org.springframework.beans.factory.config.PropertiesFactoryBean)1 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)1