Search in sources :

Example 66 with RootBeanDefinition

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

use of org.springframework.beans.factory.support.RootBeanDefinition 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)

Example 68 with RootBeanDefinition

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

the class AutowiredAnnotationBeanPostProcessorTests method testGenericsBasedInjectionIntoMatchingTypeVariable.

@Test
@SuppressWarnings("rawtypes")
public void testGenericsBasedInjectionIntoMatchingTypeVariable() {
    DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
    bf.setAutowireCandidateResolver(new QualifierAnnotationAutowireCandidateResolver());
    AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor();
    bpp.setBeanFactory(bf);
    bf.addBeanPostProcessor(bpp);
    RootBeanDefinition bd = new RootBeanDefinition(GenericInterface1Impl.class);
    bd.setFactoryMethodName("create");
    bf.registerBeanDefinition("bean1", bd);
    bf.registerBeanDefinition("bean2", new RootBeanDefinition(GenericInterface2Impl.class));
    GenericInterface1Impl bean1 = (GenericInterface1Impl) bf.getBean("bean1");
    GenericInterface2Impl bean2 = (GenericInterface2Impl) bf.getBean("bean2");
    assertSame(bean2, bean1.gi2);
}
Also used : DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) Test(org.junit.Test)

Example 69 with RootBeanDefinition

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

the class AutowiredAnnotationBeanPostProcessorTests method testCustomAnnotationRequiredFieldResourceInjectionFailsWhenNoDependencyFound.

@Test
public void testCustomAnnotationRequiredFieldResourceInjectionFailsWhenNoDependencyFound() {
    DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
    AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor();
    bpp.setAutowiredAnnotationType(MyAutowired.class);
    bpp.setRequiredParameterName("optional");
    bpp.setRequiredParameterValue(false);
    bpp.setBeanFactory(bf);
    bf.addBeanPostProcessor(bpp);
    bf.registerBeanDefinition("customBean", new RootBeanDefinition(CustomAnnotationRequiredFieldResourceInjectionBean.class));
    try {
        bf.getBean("customBean");
        fail("Should have thrown UnsatisfiedDependencyException");
    } catch (UnsatisfiedDependencyException ex) {
        // expected
        assertSame(CustomAnnotationRequiredFieldResourceInjectionBean.class, ex.getInjectionPoint().getField().getDeclaringClass());
    }
    bf.destroySingletons();
}
Also used : UnsatisfiedDependencyException(org.springframework.beans.factory.UnsatisfiedDependencyException) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) Test(org.junit.Test)

Example 70 with RootBeanDefinition

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

the class AutowiredAnnotationBeanPostProcessorTests method testGenericsBasedFactoryBeanInjectionWithSingletonBean.

@Test
public void testGenericsBasedFactoryBeanInjectionWithSingletonBean() {
    DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
    bf.setAutowireCandidateResolver(new QualifierAnnotationAutowireCandidateResolver());
    AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor();
    bpp.setBeanFactory(bf);
    bf.addBeanPostProcessor(bpp);
    RootBeanDefinition bd = new RootBeanDefinition(RepositoryFactoryBeanInjectionBean.class);
    bd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE);
    bf.registerBeanDefinition("annotatedBean", bd);
    bf.registerSingleton("repoFactoryBean", new RepositoryFactoryBean<>());
    RepositoryFactoryBeanInjectionBean bean = (RepositoryFactoryBeanInjectionBean) bf.getBean("annotatedBean");
    RepositoryFactoryBean<?> repoFactoryBean = bf.getBean("&repoFactoryBean", RepositoryFactoryBean.class);
    assertSame(repoFactoryBean, bean.repositoryFactoryBean);
}
Also used : 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)704 Test (org.junit.Test)522 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)311 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)85 GenericApplicationContext (org.springframework.context.support.GenericApplicationContext)80 RuntimeBeanReference (org.springframework.beans.factory.config.RuntimeBeanReference)77 IndexedTestBean (org.springframework.tests.sample.beans.IndexedTestBean)77 BeanComponentDefinition (org.springframework.beans.factory.parsing.BeanComponentDefinition)47 Element (org.w3c.dom.Element)47 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