Search in sources :

Example 36 with DefaultListableBeanFactory

use of org.springframework.beans.factory.support.DefaultListableBeanFactory 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 37 with DefaultListableBeanFactory

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

the class DefaultListableBeanFactoryTests method testConstructorDependencyWithClassResolution.

@Test
public void testConstructorDependencyWithClassResolution() {
    DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
    RootBeanDefinition bd = new RootBeanDefinition(ConstructorDependencyWithClassResolution.class);
    bd.getConstructorArgumentValues().addGenericArgumentValue("java.lang.String");
    lbf.registerBeanDefinition("test", bd);
    lbf.preInstantiateSingletons();
}
Also used : DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) Test(org.junit.Test)

Example 38 with DefaultListableBeanFactory

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

the class DefaultListableBeanFactoryTests method testPrototypeCreationIsFastEnough.

@Test
public void testPrototypeCreationIsFastEnough() {
    Assume.group(TestGroup.PERFORMANCE);
    Assume.notLogging(factoryLog);
    DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
    RootBeanDefinition rbd = new RootBeanDefinition(TestBean.class);
    rbd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE);
    lbf.registerBeanDefinition("test", rbd);
    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) StopWatch(org.springframework.util.StopWatch) Test(org.junit.Test)

Example 39 with DefaultListableBeanFactory

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

the class DefaultListableBeanFactoryTests method testScopingBeanToUnregisteredScopeResultsInAnException.

@Test(expected = IllegalStateException.class)
public void testScopingBeanToUnregisteredScopeResultsInAnException() throws Exception {
    BeanDefinitionBuilder builder = BeanDefinitionBuilder.rootBeanDefinition(TestBean.class);
    AbstractBeanDefinition beanDefinition = builder.getBeanDefinition();
    beanDefinition.setScope("he put himself so low could hardly look me in the face");
    DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
    factory.registerBeanDefinition("testBean", beanDefinition);
    factory.getBean("testBean");
}
Also used : BeanDefinitionBuilder(org.springframework.beans.factory.support.BeanDefinitionBuilder) AbstractBeanDefinition(org.springframework.beans.factory.support.AbstractBeanDefinition) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) Test(org.junit.Test)

Example 40 with DefaultListableBeanFactory

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

the class DefaultListableBeanFactoryTests method testCircularReferenceThroughAutowiring.

@Test
public void testCircularReferenceThroughAutowiring() {
    DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
    RootBeanDefinition bd = new RootBeanDefinition(ConstructorDependencyBean.class);
    bd.setAutowireMode(RootBeanDefinition.AUTOWIRE_CONSTRUCTOR);
    lbf.registerBeanDefinition("test", bd);
    try {
        lbf.preInstantiateSingletons();
        fail("Should have thrown UnsatisfiedDependencyException");
    } catch (UnsatisfiedDependencyException expected) {
    }
}
Also used : DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) 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