Search in sources :

Example 21 with PropertiesBeanDefinitionReader

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

the class DefaultListableBeanFactoryTests method testLazyInitialization.

@Test
public void testLazyInitialization() {
    KnowsIfInstantiated.clearInstantiationRecord();
    DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
    Properties p = new Properties();
    p.setProperty("x1.(class)", KnowsIfInstantiated.class.getName());
    p.setProperty("x1.(lazy-init)", "true");
    assertTrue("singleton not instantiated", !KnowsIfInstantiated.wasInstantiated());
    (new PropertiesBeanDefinitionReader(lbf)).registerBeanDefinitions(p);
    assertTrue("singleton not instantiated", !KnowsIfInstantiated.wasInstantiated());
    lbf.preInstantiateSingletons();
    assertTrue("singleton not instantiated", !KnowsIfInstantiated.wasInstantiated());
    lbf.getBean("x1");
    assertTrue("singleton was instantiated", KnowsIfInstantiated.wasInstantiated());
}
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 22 with PropertiesBeanDefinitionReader

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

the class DefaultListableBeanFactoryTests method testInitializedFactoryBeanFoundByNonEagerTypeMatching.

@Test
public void testInitializedFactoryBeanFoundByNonEagerTypeMatching() {
    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);
    lbf.preInstantiateSingletons();
    assertTrue("prototype not instantiated", !DummyFactory.wasPrototypeCreated());
    String[] beanNames = lbf.getBeanNamesForType(TestBean.class, true, false);
    assertEquals(1, beanNames.length);
    assertEquals("x1", beanNames[0]);
    assertTrue(lbf.containsSingleton("x1"));
    assertTrue(lbf.containsBean("x1"));
    assertTrue(lbf.containsBean("&x1"));
    assertTrue(lbf.containsLocalBean("x1"));
    assertTrue(lbf.containsLocalBean("&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", Object.class));
    assertTrue(lbf.isTypeMatch("&x1", Object.class));
    assertEquals(TestBean.class, lbf.getType("x1"));
    assertEquals(DummyFactory.class, lbf.getType("&x1"));
    assertTrue("prototype not instantiated", !DummyFactory.wasPrototypeCreated());
    lbf.registerAlias("x1", "x2");
    assertTrue(lbf.containsBean("x2"));
    assertTrue(lbf.containsBean("&x2"));
    assertTrue(lbf.containsLocalBean("x2"));
    assertTrue(lbf.containsLocalBean("&x2"));
    assertFalse(lbf.isSingleton("x2"));
    assertTrue(lbf.isSingleton("&x2"));
    assertTrue(lbf.isPrototype("x2"));
    assertFalse(lbf.isPrototype("&x2"));
    assertTrue(lbf.isTypeMatch("x2", TestBean.class));
    assertFalse(lbf.isTypeMatch("&x2", TestBean.class));
    assertTrue(lbf.isTypeMatch("&x2", DummyFactory.class));
    assertTrue(lbf.isTypeMatch("x2", Object.class));
    assertTrue(lbf.isTypeMatch("&x2", Object.class));
    assertEquals(TestBean.class, lbf.getType("x2"));
    assertEquals(DummyFactory.class, lbf.getType("&x2"));
    assertEquals(1, lbf.getAliases("x1").length);
    assertEquals("x2", lbf.getAliases("x1")[0]);
    assertEquals(1, lbf.getAliases("&x1").length);
    assertEquals("&x2", lbf.getAliases("&x1")[0]);
    assertEquals(1, lbf.getAliases("x2").length);
    assertEquals("x1", lbf.getAliases("x2")[0]);
    assertEquals(1, lbf.getAliases("&x2").length);
    assertEquals("&x1", lbf.getAliases("&x2")[0]);
}
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)

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