Search in sources :

Example 51 with DefaultListableBeanFactory

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

the class FieldRetrievingFactoryBeanTests method testBeanNameSyntaxWithBeanFactory.

@Test
public void testBeanNameSyntaxWithBeanFactory() throws Exception {
    DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
    new XmlBeanDefinitionReader(bf).loadBeanDefinitions(CONTEXT);
    TestBean testBean = (TestBean) bf.getBean("testBean");
    assertEquals(new Integer(Connection.TRANSACTION_SERIALIZABLE), testBean.getSomeIntegerArray()[0]);
    assertEquals(new Integer(Connection.TRANSACTION_SERIALIZABLE), testBean.getSomeIntegerArray()[1]);
}
Also used : TestBean(org.springframework.tests.sample.beans.TestBean) XmlBeanDefinitionReader(org.springframework.beans.factory.xml.XmlBeanDefinitionReader) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) Test(org.junit.Test)

Example 52 with DefaultListableBeanFactory

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

the class ObjectFactoryCreatingFactoryBeanTests method setUp.

@Before
public void setUp() {
    this.beanFactory = new DefaultListableBeanFactory();
    new XmlBeanDefinitionReader(this.beanFactory).loadBeanDefinitions(CONTEXT);
    this.beanFactory.setSerializationId("test");
}
Also used : XmlBeanDefinitionReader(org.springframework.beans.factory.xml.XmlBeanDefinitionReader) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) Before(org.junit.Before)

Example 53 with DefaultListableBeanFactory

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

the class SimpleScopeTests method setUp.

@Before
public void setUp() {
    beanFactory = new DefaultListableBeanFactory();
    Scope scope = new NoOpScope() {

        private int index;

        private List<TestBean> objects = new LinkedList<>();

        {
            objects.add(new TestBean());
            objects.add(new TestBean());
        }

        @Override
        public Object get(String name, ObjectFactory<?> objectFactory) {
            if (index >= objects.size()) {
                index = 0;
            }
            return objects.get(index++);
        }
    };
    beanFactory.registerScope("myScope", scope);
    String[] scopeNames = beanFactory.getRegisteredScopeNames();
    assertEquals(1, scopeNames.length);
    assertEquals("myScope", scopeNames[0]);
    assertSame(scope, beanFactory.getRegisteredScope("myScope"));
    XmlBeanDefinitionReader xbdr = new XmlBeanDefinitionReader(beanFactory);
    xbdr.loadBeanDefinitions(CONTEXT);
}
Also used : ObjectFactory(org.springframework.beans.factory.ObjectFactory) TestBean(org.springframework.tests.sample.beans.TestBean) XmlBeanDefinitionReader(org.springframework.beans.factory.xml.XmlBeanDefinitionReader) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) List(java.util.List) LinkedList(java.util.LinkedList) Before(org.junit.Before)

Example 54 with DefaultListableBeanFactory

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

the class AutowiredAnnotationBeanPostProcessorTests method testGenericsBasedFieldInjection.

@Test
public void testGenericsBasedFieldInjection() {
    DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
    bf.setAutowireCandidateResolver(new QualifierAnnotationAutowireCandidateResolver());
    AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor();
    bpp.setBeanFactory(bf);
    bf.addBeanPostProcessor(bpp);
    RootBeanDefinition bd = new RootBeanDefinition(RepositoryFieldInjectionBean.class);
    bd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE);
    bf.registerBeanDefinition("annotatedBean", bd);
    String sv = "X";
    bf.registerSingleton("stringValue", sv);
    Integer iv = 1;
    bf.registerSingleton("integerValue", iv);
    StringRepository sr = new StringRepository();
    bf.registerSingleton("stringRepo", sr);
    IntegerRepository ir = new IntegerRepository();
    bf.registerSingleton("integerRepo", ir);
    RepositoryFieldInjectionBean bean = (RepositoryFieldInjectionBean) bf.getBean("annotatedBean");
    assertSame(sv, bean.string);
    assertSame(iv, bean.integer);
    assertSame(1, bean.stringArray.length);
    assertSame(1, bean.integerArray.length);
    assertSame(sv, bean.stringArray[0]);
    assertSame(iv, bean.integerArray[0]);
    assertSame(1, bean.stringList.size());
    assertSame(1, bean.integerList.size());
    assertSame(sv, bean.stringList.get(0));
    assertSame(iv, bean.integerList.get(0));
    assertSame(1, bean.stringMap.size());
    assertSame(1, bean.integerMap.size());
    assertSame(sv, bean.stringMap.get("stringValue"));
    assertSame(iv, bean.integerMap.get("integerValue"));
    assertSame(sr, bean.stringRepository);
    assertSame(ir, bean.integerRepository);
    assertSame(1, bean.stringRepositoryArray.length);
    assertSame(1, bean.integerRepositoryArray.length);
    assertSame(sr, bean.stringRepositoryArray[0]);
    assertSame(ir, bean.integerRepositoryArray[0]);
    assertSame(1, bean.stringRepositoryList.size());
    assertSame(1, bean.integerRepositoryList.size());
    assertSame(sr, bean.stringRepositoryList.get(0));
    assertSame(ir, bean.integerRepositoryList.get(0));
    assertSame(1, bean.stringRepositoryMap.size());
    assertSame(1, bean.integerRepositoryMap.size());
    assertSame(sr, bean.stringRepositoryMap.get("stringRepo"));
    assertSame(ir, bean.integerRepositoryMap.get("integerRepo"));
}
Also used : DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) Test(org.junit.Test)

Example 55 with DefaultListableBeanFactory

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

the class AutowiredAnnotationBeanPostProcessorTests method testObjectFactoryQualifierInjection.

@Test
public void testObjectFactoryQualifierInjection() {
    DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
    bf.setAutowireCandidateResolver(new QualifierAnnotationAutowireCandidateResolver());
    AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor();
    bpp.setBeanFactory(bf);
    bf.addBeanPostProcessor(bpp);
    bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(ObjectFactoryQualifierInjectionBean.class));
    RootBeanDefinition bd = new RootBeanDefinition(TestBean.class);
    bd.addQualifier(new AutowireCandidateQualifier(Qualifier.class, "testBean"));
    bf.registerBeanDefinition("dependencyBean", bd);
    bf.registerBeanDefinition("dependencyBean2", new RootBeanDefinition(TestBean.class));
    ObjectFactoryQualifierInjectionBean bean = (ObjectFactoryQualifierInjectionBean) bf.getBean("annotatedBean");
    assertSame(bf.getBean("dependencyBean"), bean.getTestBean());
    bf.destroySingletons();
}
Also used : IndexedTestBean(org.springframework.tests.sample.beans.IndexedTestBean) ITestBean(org.springframework.tests.sample.beans.ITestBean) 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) AutowireCandidateQualifier(org.springframework.beans.factory.support.AutowireCandidateQualifier) AutowireCandidateQualifier(org.springframework.beans.factory.support.AutowireCandidateQualifier) Test(org.junit.Test)

Aggregations

DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)652 Test (org.junit.Test)576 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)317 TestBean (org.springframework.tests.sample.beans.TestBean)249 ITestBean (org.springframework.tests.sample.beans.ITestBean)199 NestedTestBean (org.springframework.tests.sample.beans.NestedTestBean)152 IndexedTestBean (org.springframework.tests.sample.beans.IndexedTestBean)101 DerivedTestBean (org.springframework.tests.sample.beans.DerivedTestBean)96 XmlBeanDefinitionReader (org.springframework.beans.factory.xml.XmlBeanDefinitionReader)82 ClassPathResource (org.springframework.core.io.ClassPathResource)80 Properties (java.util.Properties)32 Before (org.junit.Before)27 BeanCreationException (org.springframework.beans.factory.BeanCreationException)26 ResourceTestBean (org.springframework.tests.sample.beans.ResourceTestBean)26 MutablePropertyValues (org.springframework.beans.MutablePropertyValues)24 HashMap (java.util.HashMap)22 PropertiesBeanDefinitionReader (org.springframework.beans.factory.support.PropertiesBeanDefinitionReader)22 DependenciesBean (org.springframework.tests.sample.beans.DependenciesBean)17 ConstructorDependenciesBean (org.springframework.beans.factory.xml.ConstructorDependenciesBean)16 BeanDefinition (org.springframework.beans.factory.config.BeanDefinition)14