Search in sources :

Example 16 with DefaultListableBeanFactory

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

the class DefaultListableBeanFactoryTests method testRegistrationOfManySingletonsIsFastEnough.

@Test(timeout = 1000)
public void testRegistrationOfManySingletonsIsFastEnough() {
    Assume.group(TestGroup.PERFORMANCE);
    DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
    bf.registerBeanDefinition("b", new RootBeanDefinition(B.class));
    for (int i = 0; i < 100000; i++) {
        bf.registerSingleton("a" + i, new A());
    }
}
Also used : DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) Test(org.junit.Test)

Example 17 with DefaultListableBeanFactory

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

the class DefaultListableBeanFactoryTests method testAutowireBeanByName.

@Test
public void testAutowireBeanByName() {
    DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
    RootBeanDefinition bd = new RootBeanDefinition(TestBean.class);
    lbf.registerBeanDefinition("spouse", bd);
    DependenciesBean bean = (DependenciesBean) lbf.autowire(DependenciesBean.class, AutowireCapableBeanFactory.AUTOWIRE_BY_NAME, true);
    TestBean spouse = (TestBean) lbf.getBean("spouse");
    assertEquals(spouse, bean.getSpouse());
    assertTrue(BeanFactoryUtils.beanOfType(lbf, TestBean.class) == spouse);
}
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) 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 18 with DefaultListableBeanFactory

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

the class DefaultListableBeanFactoryTests method testUnreferencedSingletonWasInstantiated.

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

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

the class DefaultListableBeanFactoryTests method testScopeInheritanceForChildBeanDefinitions.

@Test
public void testScopeInheritanceForChildBeanDefinitions() throws Exception {
    RootBeanDefinition parent = new RootBeanDefinition();
    parent.setScope("bonanza!");
    AbstractBeanDefinition child = new ChildBeanDefinition("parent");
    child.setBeanClass(TestBean.class);
    DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
    factory.registerBeanDefinition("parent", parent);
    factory.registerBeanDefinition("child", child);
    BeanDefinition def = factory.getMergedBeanDefinition("child");
    assertEquals("Child 'scope' not inherited", "bonanza!", def.getScope());
}
Also used : AbstractBeanDefinition(org.springframework.beans.factory.support.AbstractBeanDefinition) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) ChildBeanDefinition(org.springframework.beans.factory.support.ChildBeanDefinition) AbstractBeanDefinition(org.springframework.beans.factory.support.AbstractBeanDefinition) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) ChildBeanDefinition(org.springframework.beans.factory.support.ChildBeanDefinition) BeanDefinition(org.springframework.beans.factory.config.BeanDefinition) Test(org.junit.Test)

Example 20 with DefaultListableBeanFactory

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

the class DefaultListableBeanFactoryTests method testEmpty.

@Test
public void testEmpty() {
    ListableBeanFactory lbf = new DefaultListableBeanFactory();
    assertTrue("No beans defined --> array != null", lbf.getBeanDefinitionNames() != null);
    assertTrue("No beans defined after no arg constructor", lbf.getBeanDefinitionNames().length == 0);
    assertTrue("No beans defined after no arg constructor", lbf.getBeanDefinitionCount() == 0);
}
Also used : DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) Test(org.junit.Test)

Aggregations

DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)801 Test (org.junit.jupiter.api.Test)354 Test (org.junit.Test)298 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)229 TestBean (org.springframework.beans.testfixture.beans.TestBean)124 XmlBeanDefinitionReader (org.springframework.beans.factory.xml.XmlBeanDefinitionReader)100 ITestBean (org.springframework.tests.sample.beans.ITestBean)94 TestBean (org.springframework.tests.sample.beans.TestBean)94 DerivedTestBean (org.springframework.tests.sample.beans.DerivedTestBean)91 ClassPathResource (org.springframework.core.io.ClassPathResource)86 ITestBean (org.springframework.beans.testfixture.beans.ITestBean)73 NestedTestBean (org.springframework.tests.sample.beans.NestedTestBean)69 ResourceTestBean (org.springframework.tests.sample.beans.ResourceTestBean)50 BeanCreationException (org.springframework.beans.factory.BeanCreationException)41 DerivedTestBean (org.springframework.beans.testfixture.beans.DerivedTestBean)38 Properties (java.util.Properties)35 HashMap (java.util.HashMap)30 IndexedTestBean (org.springframework.tests.sample.beans.IndexedTestBean)29 BeanDefinitionBuilder (org.springframework.beans.factory.support.BeanDefinitionBuilder)28 BeforeEach (org.junit.jupiter.api.BeforeEach)27