Search in sources :

Example 6 with PropertiesBeanDefinitionReader

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

the class DefaultListableBeanFactoryTests method testCanEscapeBeanReferenceSyntax.

@Test
public void testCanEscapeBeanReferenceSyntax() {
    String name = "*name";
    DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
    Properties p = new Properties();
    p.setProperty("r.(class)", TestBean.class.getName());
    p.setProperty("r.name", "*" + name);
    (new PropertiesBeanDefinitionReader(lbf)).registerBeanDefinitions(p);
    TestBean r = (TestBean) lbf.getBean("r");
    assertTrue(r.getName().equals(name));
}
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 7 with PropertiesBeanDefinitionReader

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

the class DefaultListableBeanFactoryTests method testSimpleReference.

@Test
public void testSimpleReference() {
    String PREFIX = "beans.";
    DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
    Properties p = new Properties();
    p.setProperty(PREFIX + "rod.(class)", TestBean.class.getName());
    p.setProperty(PREFIX + "rod.name", "Rod");
    p.setProperty(PREFIX + "kerry.(class)", TestBean.class.getName());
    p.setProperty(PREFIX + "kerry.name", "Kerry");
    p.setProperty(PREFIX + "kerry.age", "35");
    p.setProperty(PREFIX + "kerry.spouse(ref)", "rod");
    int count = (new PropertiesBeanDefinitionReader(lbf)).registerBeanDefinitions(p, PREFIX);
    assertTrue("2 beans registered, not " + count, count == 2);
    TestBean kerry = lbf.getBean("kerry", TestBean.class);
    assertTrue("Kerry name is Kerry", "Kerry".equals(kerry.getName()));
    ITestBean spouse = kerry.getSpouse();
    assertTrue("Kerry spouse is non null", spouse != null);
    assertTrue("Kerry spouse name is Rod", "Rod".equals(spouse.getName()));
}
Also used : ITestBean(org.springframework.tests.sample.beans.ITestBean) 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 8 with PropertiesBeanDefinitionReader

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

the class DefaultListableBeanFactoryTests method testNonInitializedFactoryBeanIgnoredByNonEagerTypeMatching.

@Test
public void testNonInitializedFactoryBeanIgnoredByNonEagerTypeMatching() {
    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");
    (new PropertiesBeanDefinitionReader(lbf)).registerBeanDefinitions(p);
    assertTrue("prototype not instantiated", !DummyFactory.wasPrototypeCreated());
    String[] beanNames = lbf.getBeanNamesForType(TestBean.class, true, false);
    assertEquals(0, beanNames.length);
    beanNames = lbf.getBeanNamesForAnnotation(SuppressWarnings.class);
    assertEquals(0, beanNames.length);
    assertFalse(lbf.containsSingleton("x1"));
    assertTrue(lbf.containsBean("x1"));
    assertTrue(lbf.containsBean("&x1"));
    assertFalse(lbf.isSingleton("x1"));
    assertTrue(lbf.isSingleton("&x1"));
    assertTrue(lbf.isPrototype("x1"));
    assertFalse(lbf.isPrototype("&x1"));
    assertTrue(lbf.isTypeMatch("x1", TestBean.class));
    assertFalse(lbf.isTypeMatch("&x1", TestBean.class));
    assertTrue(lbf.isTypeMatch("&x1", DummyFactory.class));
    assertTrue(lbf.isTypeMatch("&x1", ResolvableType.forClass(DummyFactory.class)));
    assertTrue(lbf.isTypeMatch("&x1", ResolvableType.forClassWithGenerics(FactoryBean.class, Object.class)));
    assertFalse(lbf.isTypeMatch("&x1", ResolvableType.forClassWithGenerics(FactoryBean.class, String.class)));
    assertEquals(TestBean.class, lbf.getType("x1"));
    assertEquals(DummyFactory.class, lbf.getType("&x1"));
    assertTrue("prototype not instantiated", !DummyFactory.wasPrototypeCreated());
}
Also used : DummyFactory(org.springframework.tests.sample.beans.factory.DummyFactory) 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 9 with PropertiesBeanDefinitionReader

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

the class DefaultListableBeanFactoryTests method testNameAlreadyBound.

@Test
public void testNameAlreadyBound() {
    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);
    try {
        (new PropertiesBeanDefinitionReader(lbf)).registerBeanDefinitions(p);
    } catch (BeanDefinitionStoreException ex) {
        assertEquals("kerry", ex.getBeanName());
    // expected
    }
}
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 10 with PropertiesBeanDefinitionReader

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

the class DefaultListableBeanFactoryTests method testRegisterExistingSingletonWithReference.

@Test
public void testRegisterExistingSingletonWithReference() {
    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);
    Object singletonObject = new TestBean();
    lbf.registerSingleton("singletonObject", singletonObject);
    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);
    assertEquals(2, beansOfType.size());
    Iterator<String> beanNames = lbf.getBeanNamesIterator();
    assertEquals("test", beanNames.next());
    assertEquals("singletonObject", beanNames.next());
    assertFalse(beanNames.hasNext());
    assertTrue(lbf.containsSingleton("test"));
    assertTrue(lbf.containsSingleton("singletonObject"));
    assertTrue(lbf.containsBeanDefinition("test"));
    assertFalse(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) 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