Search in sources :

Example 26 with DefaultListableBeanFactory

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

the class DefaultListableBeanFactoryTests method testGetBeanByTypeWithPriorityAndNullInstance.

@Test
public void testGetBeanByTypeWithPriorityAndNullInstance() throws Exception {
    DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
    lbf.setDependencyComparator(AnnotationAwareOrderComparator.INSTANCE);
    RootBeanDefinition bd1 = new RootBeanDefinition(HighPriorityTestBean.class);
    RootBeanDefinition bd2 = new RootBeanDefinition(NullTestBeanFactoryBean.class);
    lbf.registerBeanDefinition("bd1", bd1);
    lbf.registerBeanDefinition("bd2", bd2);
    TestBean bean = lbf.getBean(TestBean.class);
    assertThat(bean.getBeanName(), equalTo("bd1"));
}
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) Test(org.junit.Test)

Example 27 with DefaultListableBeanFactory

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

the class DefaultListableBeanFactoryTests method testAutowireBeanByTypeWithTwoMatchesAndOnePrimary.

@Test
public void testAutowireBeanByTypeWithTwoMatchesAndOnePrimary() {
    DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
    RootBeanDefinition bd = new RootBeanDefinition(TestBean.class);
    bd.setPrimary(true);
    RootBeanDefinition bd2 = new RootBeanDefinition(TestBean.class);
    lbf.registerBeanDefinition("test", bd);
    lbf.registerBeanDefinition("spouse", bd2);
    DependenciesBean bean = (DependenciesBean) lbf.autowire(DependenciesBean.class, AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE, true);
    assertThat(bean.getSpouse(), equalTo(lbf.getBean("test")));
}
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 28 with DefaultListableBeanFactory

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

the class DefaultListableBeanFactoryTests method testConstructorDependencyWithUnresolvableClass.

@Test
public void testConstructorDependencyWithUnresolvableClass() {
    DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
    RootBeanDefinition bd = new RootBeanDefinition(ConstructorDependencyWithClassResolution.class);
    bd.getConstructorArgumentValues().addGenericArgumentValue("java.lang.Strin");
    lbf.registerBeanDefinition("test", bd);
    try {
        lbf.preInstantiateSingletons();
        fail("Should have thrown UnsatisfiedDependencyException");
    } catch (UnsatisfiedDependencyException expected) {
        assertTrue(expected.toString().contains("java.lang.Strin"));
    }
}
Also used : DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) Test(org.junit.Test)

Example 29 with DefaultListableBeanFactory

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

the class DefaultListableBeanFactoryTests method testBeanDefinitionWithAbstractClass.

@Test
public void testBeanDefinitionWithAbstractClass() {
    DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
    lbf.registerBeanDefinition("test", new RootBeanDefinition(AbstractBeanFactory.class));
    try {
        lbf.getBean("test");
        fail("Should have thrown BeanCreationException");
    } catch (BeanCreationException ex) {
        assertEquals("test", ex.getBeanName());
        assertTrue(ex.getMessage().toLowerCase().contains("abstract"));
    }
}
Also used : AbstractBeanFactory(org.springframework.beans.factory.support.AbstractBeanFactory) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) Test(org.junit.Test)

Example 30 with DefaultListableBeanFactory

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

the class DefaultListableBeanFactoryTests method testUnresolvedReference.

@Test
public void testUnresolvedReference() {
    String PREFIX = "beans.";
    DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
    Properties p = new Properties();
    try {
        p.setProperty(PREFIX + "kerry.(class)", TestBean.class.getName());
        p.setProperty(PREFIX + "kerry.name", "Kerry");
        p.setProperty(PREFIX + "kerry.age", "35");
        p.setProperty(PREFIX + "kerry.spouse(ref)", "rod");
        (new PropertiesBeanDefinitionReader(lbf)).registerBeanDefinitions(p, PREFIX);
        lbf.getBean("kerry");
        fail("Unresolved reference should have been detected");
    } catch (BeansException ex) {
    // cool
    }
}
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) BeansException(org.springframework.beans.BeansException) 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