Search in sources :

Example 11 with PropertiesBeanDefinitionReader

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

the class DefaultListableBeanFactoryTests method testBeanReferenceWithNewSyntax.

@Test
public void testBeanReferenceWithNewSyntax() {
    DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
    Properties p = new Properties();
    p.setProperty("r.(class)", TestBean.class.getName());
    p.setProperty("r.name", "rod");
    p.setProperty("k.(class)", TestBean.class.getName());
    p.setProperty("k.name", "kerry");
    p.setProperty("k.spouse", "*r");
    (new PropertiesBeanDefinitionReader(lbf)).registerBeanDefinitions(p);
    TestBean k = (TestBean) lbf.getBean("k");
    TestBean r = (TestBean) lbf.getBean("r");
    assertTrue(k.getSpouse() == r);
}
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 12 with PropertiesBeanDefinitionReader

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

the class DefaultListableBeanFactoryTests method testPropertiesPopulationWithPrefix.

@Test
public void testPropertiesPopulationWithPrefix() {
    String PREFIX = "beans.";
    DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
    Properties p = new Properties();
    p.setProperty(PREFIX + "test.(class)", TestBean.class.getName());
    p.setProperty(PREFIX + "test.name", "Tony");
    p.setProperty(PREFIX + "test.age", "0x30");
    int count = (new PropertiesBeanDefinitionReader(lbf)).registerBeanDefinitions(p, PREFIX);
    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 13 with PropertiesBeanDefinitionReader

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

the class DefaultListableBeanFactoryTests method testPrototype.

@Test
public void testPrototype() {
    DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
    Properties p = new Properties();
    p.setProperty("kerry.(class)", TestBean.class.getName());
    p.setProperty("kerry.age", "35");
    (new PropertiesBeanDefinitionReader(lbf)).registerBeanDefinitions(p);
    TestBean kerry1 = (TestBean) lbf.getBean("kerry");
    TestBean kerry2 = (TestBean) lbf.getBean("kerry");
    assertTrue("Non null", kerry1 != null);
    assertTrue("Singletons equal", kerry1 == kerry2);
    lbf = new DefaultListableBeanFactory();
    p = new Properties();
    p.setProperty("kerry.(class)", TestBean.class.getName());
    p.setProperty("kerry.(scope)", "prototype");
    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("Prototypes NOT equal", kerry1 != kerry2);
    lbf = new DefaultListableBeanFactory();
    p = new Properties();
    p.setProperty("kerry.(class)", TestBean.class.getName());
    p.setProperty("kerry.(scope)", "singleton");
    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 14 with PropertiesBeanDefinitionReader

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

the class DefaultListableBeanFactoryTests method testFactoryBeanDidNotCreatePrototype.

@Test
public void testFactoryBeanDidNotCreatePrototype() {
    DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
    Properties p = new Properties();
    p.setProperty("x1.(class)", DummyFactory.class.getName());
    // Reset static state
    DummyFactory.reset();
    p.setProperty("x1.singleton", "false");
    assertTrue("prototype not instantiated", !DummyFactory.wasPrototypeCreated());
    (new PropertiesBeanDefinitionReader(lbf)).registerBeanDefinitions(p);
    assertTrue("prototype not instantiated", !DummyFactory.wasPrototypeCreated());
    assertEquals(TestBean.class, lbf.getType("x1"));
    lbf.preInstantiateSingletons();
    assertTrue("prototype not instantiated", !DummyFactory.wasPrototypeCreated());
    lbf.getBean("x1");
    assertEquals(TestBean.class, lbf.getType("x1"));
    assertTrue(lbf.containsBean("x1"));
    assertTrue(lbf.containsBean("&x1"));
    assertTrue("prototype was instantiated", DummyFactory.wasPrototypeCreated());
}
Also used : DummyFactory(org.springframework.tests.sample.beans.factory.DummyFactory) PropertiesBeanDefinitionReader(org.springframework.beans.factory.support.PropertiesBeanDefinitionReader) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) Properties(java.util.Properties) Test(org.junit.Test)

Example 15 with PropertiesBeanDefinitionReader

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

the class DefaultListableBeanFactoryTests method testHarmlessIgnorableRubbish.

@Test
public void testHarmlessIgnorableRubbish() {
    DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
    Properties p = new Properties();
    p.setProperty("foo", "bar");
    p.setProperty("qwert", "er");
    (new PropertiesBeanDefinitionReader(lbf)).registerBeanDefinitions(p, "test");
    assertTrue("No beans defined after harmless ignorable rubbish", 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)

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