Search in sources :

Example 1 with DummyFactory

use of org.springframework.tests.sample.beans.factory.DummyFactory in project spring-framework by spring-projects.

the class XmlListableBeanFactoryTests method setUp.

@Before
public void setUp() {
    parent = new DefaultListableBeanFactory();
    Map m = new HashMap();
    m.put("name", "Albert");
    RootBeanDefinition bd1 = new RootBeanDefinition(TestBean.class);
    bd1.setPropertyValues(new MutablePropertyValues(m));
    parent.registerBeanDefinition("father", bd1);
    m = new HashMap();
    m.put("name", "Roderick");
    RootBeanDefinition bd2 = new RootBeanDefinition(TestBean.class);
    bd2.setPropertyValues(new MutablePropertyValues(m));
    parent.registerBeanDefinition("rod", bd2);
    this.factory = new DefaultListableBeanFactory(parent);
    new XmlBeanDefinitionReader(this.factory).loadBeanDefinitions(new ClassPathResource("test.xml", getClass()));
    this.factory.addBeanPostProcessor(new BeanPostProcessor() {

        @Override
        public Object postProcessBeforeInitialization(Object bean, String name) throws BeansException {
            if (bean instanceof TestBean) {
                ((TestBean) bean).setPostProcessed(true);
            }
            if (bean instanceof DummyFactory) {
                ((DummyFactory) bean).setPostProcessed(true);
            }
            return bean;
        }

        @Override
        public Object postProcessAfterInitialization(Object bean, String name) throws BeansException {
            return bean;
        }
    });
    this.factory.addBeanPostProcessor(new LifecycleBean.PostProcessor());
    this.factory.addBeanPostProcessor(new ProtectedLifecycleBean.PostProcessor());
//this.factory.preInstantiateSingletons();
}
Also used : HashMap(java.util.HashMap) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) ClassPathResource(org.springframework.core.io.ClassPathResource) DummyFactory(org.springframework.tests.sample.beans.factory.DummyFactory) ITestBean(org.springframework.tests.sample.beans.ITestBean) TestBean(org.springframework.tests.sample.beans.TestBean) MutablePropertyValues(org.springframework.beans.MutablePropertyValues) BeanPostProcessor(org.springframework.beans.factory.config.BeanPostProcessor) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) LifecycleBean(org.springframework.tests.sample.beans.LifecycleBean) HashMap(java.util.HashMap) Map(java.util.Map) BeansException(org.springframework.beans.BeansException) Before(org.junit.Before)

Example 2 with DummyFactory

use of org.springframework.tests.sample.beans.factory.DummyFactory in project spring-framework by spring-projects.

the class AbstractBeanFactoryTests method factorySingleton.

@Test
public void factorySingleton() throws Exception {
    assertTrue(getBeanFactory().isSingleton("&singletonFactory"));
    assertTrue(getBeanFactory().isSingleton("singletonFactory"));
    TestBean tb = (TestBean) getBeanFactory().getBean("singletonFactory");
    assertTrue("Singleton from factory has correct name, not " + tb.getName(), tb.getName().equals(DummyFactory.SINGLETON_NAME));
    DummyFactory factory = (DummyFactory) getBeanFactory().getBean("&singletonFactory");
    TestBean tb2 = (TestBean) getBeanFactory().getBean("singletonFactory");
    assertTrue("Singleton references ==", tb == tb2);
    assertTrue("FactoryBean is BeanFactoryAware", factory.getBeanFactory() != null);
}
Also used : DummyFactory(org.springframework.tests.sample.beans.factory.DummyFactory) TestBean(org.springframework.tests.sample.beans.TestBean) Test(org.junit.Test)

Example 3 with DummyFactory

use of org.springframework.tests.sample.beans.factory.DummyFactory in project spring-framework by spring-projects.

the class AbstractBeanFactoryTests method factoryIsInitialized.

/**
	 * Check that afterPropertiesSet gets called on factory
	 * @throws Exception
	 */
@Test
public void factoryIsInitialized() throws Exception {
    TestBean tb = (TestBean) getBeanFactory().getBean("singletonFactory");
    assertNotNull(tb);
    DummyFactory factory = (DummyFactory) getBeanFactory().getBean("&singletonFactory");
    assertTrue("Factory was initialized because it implemented InitializingBean", factory.wasInitialized());
}
Also used : DummyFactory(org.springframework.tests.sample.beans.factory.DummyFactory) TestBean(org.springframework.tests.sample.beans.TestBean) Test(org.junit.Test)

Example 4 with DummyFactory

use of org.springframework.tests.sample.beans.factory.DummyFactory in project spring-framework by spring-projects.

the class XmlBeanFactoryTests method testFactoryReferenceCircle.

@Test
public void testFactoryReferenceCircle() {
    DefaultListableBeanFactory xbf = new DefaultListableBeanFactory();
    new XmlBeanDefinitionReader(xbf).loadBeanDefinitions(FACTORY_CIRCLE_CONTEXT);
    TestBean tb = (TestBean) xbf.getBean("singletonFactory");
    DummyFactory db = (DummyFactory) xbf.getBean("&singletonFactory");
    assertTrue(tb == db.getOtherTestBean());
}
Also used : DummyFactory(org.springframework.tests.sample.beans.factory.DummyFactory) IndexedTestBean(org.springframework.tests.sample.beans.IndexedTestBean) ResourceTestBean(org.springframework.tests.sample.beans.ResourceTestBean) ITestBean(org.springframework.tests.sample.beans.ITestBean) DerivedTestBean(org.springframework.tests.sample.beans.DerivedTestBean) TestBean(org.springframework.tests.sample.beans.TestBean) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) Test(org.junit.Test)

Aggregations

TestBean (org.springframework.tests.sample.beans.TestBean)4 DummyFactory (org.springframework.tests.sample.beans.factory.DummyFactory)4 Test (org.junit.Test)3 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)2 ITestBean (org.springframework.tests.sample.beans.ITestBean)2 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Before (org.junit.Before)1 BeansException (org.springframework.beans.BeansException)1 MutablePropertyValues (org.springframework.beans.MutablePropertyValues)1 BeanPostProcessor (org.springframework.beans.factory.config.BeanPostProcessor)1 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)1 ClassPathResource (org.springframework.core.io.ClassPathResource)1 DerivedTestBean (org.springframework.tests.sample.beans.DerivedTestBean)1 IndexedTestBean (org.springframework.tests.sample.beans.IndexedTestBean)1 LifecycleBean (org.springframework.tests.sample.beans.LifecycleBean)1 ResourceTestBean (org.springframework.tests.sample.beans.ResourceTestBean)1