Search in sources :

Example 16 with RootBeanDefinition

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

the class DefaultListableBeanFactoryTests method testAutowireBeanByNameWithNoDependencyCheck.

@Test
public void testAutowireBeanByNameWithNoDependencyCheck() {
    DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
    RootBeanDefinition bd = new RootBeanDefinition(TestBean.class);
    lbf.registerBeanDefinition("spous", bd);
    DependenciesBean bean = (DependenciesBean) lbf.autowire(DependenciesBean.class, AutowireCapableBeanFactory.AUTOWIRE_BY_NAME, false);
    assertNull(bean.getSpouse());
}
Also used : DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) ConstructorDependenciesBean(org.springframework.beans.factory.xml.ConstructorDependenciesBean) DependenciesBean(org.springframework.tests.sample.beans.DependenciesBean) Test(org.junit.Test)

Example 17 with RootBeanDefinition

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

the class DefaultListableBeanFactoryTests method testBeanPostProcessorWithWrappedObjectAndDisposableBean.

@Test
public void testBeanPostProcessorWithWrappedObjectAndDisposableBean() {
    DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
    RootBeanDefinition bd = new RootBeanDefinition(BeanWithDisposableBean.class);
    lbf.registerBeanDefinition("test", bd);
    lbf.addBeanPostProcessor(new BeanPostProcessor() {

        @Override
        public Object postProcessBeforeInitialization(Object bean, String beanName) {
            return new TestBean();
        }

        @Override
        public Object postProcessAfterInitialization(Object bean, String beanName) {
            return bean;
        }
    });
    BeanWithDisposableBean.closed = false;
    lbf.preInstantiateSingletons();
    lbf.destroySingletons();
    assertTrue("Destroy method invoked", BeanWithDisposableBean.closed);
}
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) BeanPostProcessor(org.springframework.beans.factory.config.BeanPostProcessor) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) Test(org.junit.Test)

Example 18 with RootBeanDefinition

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

the class DefaultListableBeanFactoryTests method testPrototypeCreationWithDependencyCheckIsFastEnough.

@Test
public void testPrototypeCreationWithDependencyCheckIsFastEnough() {
    Assume.group(TestGroup.PERFORMANCE);
    Assume.notLogging(factoryLog);
    DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
    RootBeanDefinition rbd = new RootBeanDefinition(LifecycleBean.class);
    rbd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE);
    rbd.setDependencyCheck(RootBeanDefinition.DEPENDENCY_CHECK_OBJECTS);
    lbf.registerBeanDefinition("test", rbd);
    lbf.addBeanPostProcessor(new LifecycleBean.PostProcessor());
    StopWatch sw = new StopWatch();
    sw.start("prototype");
    for (int i = 0; i < 100000; i++) {
        lbf.getBean("test");
    }
    sw.stop();
    // System.out.println(sw.getTotalTimeMillis());
    assertTrue("Prototype creation took too long: " + sw.getTotalTimeMillis(), sw.getTotalTimeMillis() < 3000);
}
Also used : DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) LifecycleBean(org.springframework.tests.sample.beans.LifecycleBean) StopWatch(org.springframework.util.StopWatch) Test(org.junit.Test)

Example 19 with RootBeanDefinition

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

the class DefaultListableBeanFactoryTests method testSmartInitFactory.

@Test
public void testSmartInitFactory() {
    DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
    lbf.registerBeanDefinition("test", new RootBeanDefinition(EagerInitFactory.class));
    lbf.preInstantiateSingletons();
    EagerInitFactory factory = (EagerInitFactory) lbf.getBean("&test");
    assertTrue(factory.initialized);
}
Also used : DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) Test(org.junit.Test)

Example 20 with RootBeanDefinition

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

the class DefaultListableBeanFactoryTests method testBeanDefinitionOverridingNotAllowed.

@Test
public void testBeanDefinitionOverridingNotAllowed() {
    DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
    lbf.setAllowBeanDefinitionOverriding(false);
    lbf.registerBeanDefinition("test", new RootBeanDefinition(TestBean.class));
    try {
        lbf.registerBeanDefinition("test", new RootBeanDefinition(NestedTestBean.class));
        fail("Should have thrown BeanDefinitionStoreException");
    } catch (BeanDefinitionStoreException ex) {
        assertEquals("test", 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) NestedTestBean(org.springframework.tests.sample.beans.NestedTestBean) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) Test(org.junit.Test)

Aggregations

RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)696 Test (org.junit.Test)521 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)308 TestBean (org.springframework.tests.sample.beans.TestBean)159 ITestBean (org.springframework.tests.sample.beans.ITestBean)143 NestedTestBean (org.springframework.tests.sample.beans.NestedTestBean)133 BeanDefinition (org.springframework.beans.factory.config.BeanDefinition)84 GenericApplicationContext (org.springframework.context.support.GenericApplicationContext)79 IndexedTestBean (org.springframework.tests.sample.beans.IndexedTestBean)77 RuntimeBeanReference (org.springframework.beans.factory.config.RuntimeBeanReference)74 Element (org.w3c.dom.Element)47 BeanComponentDefinition (org.springframework.beans.factory.parsing.BeanComponentDefinition)46 DerivedTestBean (org.springframework.tests.sample.beans.DerivedTestBean)46 MutablePropertyValues (org.springframework.beans.MutablePropertyValues)36 GenericWebApplicationContext (org.springframework.web.context.support.GenericWebApplicationContext)31 BeanDefinitionBuilder (org.springframework.beans.factory.support.BeanDefinitionBuilder)30 MockHttpServletRequest (org.springframework.mock.web.test.MockHttpServletRequest)30 MockHttpServletResponse (org.springframework.mock.web.test.MockHttpServletResponse)28 Properties (java.util.Properties)26 ManagedList (org.springframework.beans.factory.support.ManagedList)25