Search in sources :

Example 61 with TestBean

use of org.springframework.tests.sample.beans.TestBean in project spring-framework by spring-projects.

the class InjectAnnotationBeanPostProcessorTests method testObjectFactoryFieldInjectionIntoPrototypeBean.

@Test
public void testObjectFactoryFieldInjectionIntoPrototypeBean() {
    DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
    bf.setAutowireCandidateResolver(new QualifierAnnotationAutowireCandidateResolver());
    AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor();
    bpp.setBeanFactory(bf);
    bf.addBeanPostProcessor(bpp);
    RootBeanDefinition annotatedBeanDefinition = new RootBeanDefinition(ObjectFactoryQualifierFieldInjectionBean.class);
    annotatedBeanDefinition.setScope(BeanDefinition.SCOPE_PROTOTYPE);
    bf.registerBeanDefinition("annotatedBean", annotatedBeanDefinition);
    RootBeanDefinition bd = new RootBeanDefinition(TestBean.class);
    bd.addQualifier(new AutowireCandidateQualifier(Qualifier.class, "testBean"));
    bf.registerBeanDefinition("testBean", bd);
    bf.registerBeanDefinition("testBean2", new RootBeanDefinition(TestBean.class));
    ObjectFactoryQualifierFieldInjectionBean bean = (ObjectFactoryQualifierFieldInjectionBean) bf.getBean("annotatedBean");
    assertSame(bf.getBean("testBean"), bean.getTestBean());
    ObjectFactoryQualifierFieldInjectionBean anotherBean = (ObjectFactoryQualifierFieldInjectionBean) bf.getBean("annotatedBean");
    assertNotSame(anotherBean, bean);
    assertSame(bf.getBean("testBean"), bean.getTestBean());
}
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 62 with TestBean

use of org.springframework.tests.sample.beans.TestBean in project spring-framework by spring-projects.

the class InjectAnnotationBeanPostProcessorTests method testProviderOfOptionalFieldInjectionWithBeanAvailable.

@Test
public void testProviderOfOptionalFieldInjectionWithBeanAvailable() {
    DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
    AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor();
    bpp.setBeanFactory(bf);
    bf.addBeanPostProcessor(bpp);
    bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(ProviderOfOptionalFieldInjectionBean.class));
    bf.registerBeanDefinition("testBean", new RootBeanDefinition(TestBean.class));
    ProviderOfOptionalFieldInjectionBean bean = (ProviderOfOptionalFieldInjectionBean) bf.getBean("annotatedBean");
    assertTrue(bean.getTestBean().isPresent());
    assertSame(bf.getBean("testBean"), bean.getTestBean().get());
    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) Test(org.junit.Test)

Example 63 with TestBean

use of org.springframework.tests.sample.beans.TestBean in project spring-framework by spring-projects.

the class InjectAnnotationBeanPostProcessorTests method testMethodInjectionWithMapAndMultipleMatchesButOnlyOneAutowireCandidate.

@Test
public void testMethodInjectionWithMapAndMultipleMatchesButOnlyOneAutowireCandidate() {
    DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
    AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor();
    bpp.setBeanFactory(bf);
    bf.addBeanPostProcessor(bpp);
    bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(MapMethodInjectionBean.class));
    bf.registerBeanDefinition("testBean1", new RootBeanDefinition(TestBean.class));
    RootBeanDefinition rbd2 = new RootBeanDefinition(TestBean.class);
    rbd2.setAutowireCandidate(false);
    bf.registerBeanDefinition("testBean2", rbd2);
    MapMethodInjectionBean bean = (MapMethodInjectionBean) bf.getBean("annotatedBean");
    TestBean tb = (TestBean) bf.getBean("testBean1");
    assertEquals(1, bean.getTestBeanMap().size());
    assertTrue(bean.getTestBeanMap().keySet().contains("testBean1"));
    assertTrue(bean.getTestBeanMap().values().contains(tb));
    assertSame(tb, 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) Test(org.junit.Test)

Example 64 with TestBean

use of org.springframework.tests.sample.beans.TestBean in project spring-framework by spring-projects.

the class InjectAnnotationBeanPostProcessorTests method testNullableFieldInjectionWithBeanAvailable.

@Test
public void testNullableFieldInjectionWithBeanAvailable() {
    DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
    AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor();
    bpp.setBeanFactory(bf);
    bf.addBeanPostProcessor(bpp);
    bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(NullableFieldInjectionBean.class));
    bf.registerBeanDefinition("testBean", new RootBeanDefinition(TestBean.class));
    NullableFieldInjectionBean bean = (NullableFieldInjectionBean) bf.getBean("annotatedBean");
    assertSame(bf.getBean("testBean"), 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) Test(org.junit.Test)

Example 65 with TestBean

use of org.springframework.tests.sample.beans.TestBean in project spring-framework by spring-projects.

the class InjectAnnotationBeanPostProcessorTests method testNullableMethodInjectionWithBeanAvailable.

@Test
public void testNullableMethodInjectionWithBeanAvailable() {
    DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
    AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor();
    bpp.setBeanFactory(bf);
    bf.addBeanPostProcessor(bpp);
    bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(NullableMethodInjectionBean.class));
    bf.registerBeanDefinition("testBean", new RootBeanDefinition(TestBean.class));
    NullableMethodInjectionBean bean = (NullableMethodInjectionBean) bf.getBean("annotatedBean");
    assertSame(bf.getBean("testBean"), 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) Test(org.junit.Test)

Aggregations

TestBean (org.springframework.tests.sample.beans.TestBean)788 Test (org.junit.Test)740 ITestBean (org.springframework.tests.sample.beans.ITestBean)456 IndexedTestBean (org.springframework.tests.sample.beans.IndexedTestBean)248 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)225 NestedTestBean (org.springframework.tests.sample.beans.NestedTestBean)164 DerivedTestBean (org.springframework.tests.sample.beans.DerivedTestBean)160 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)144 MutablePropertyValues (org.springframework.beans.MutablePropertyValues)86 MockHttpServletRequest (org.springframework.mock.web.test.MockHttpServletRequest)44 BooleanTestBean (org.springframework.tests.sample.beans.BooleanTestBean)40 NumberTestBean (org.springframework.tests.sample.beans.NumberTestBean)40 Properties (java.util.Properties)35 Errors (org.springframework.validation.Errors)34 NopInterceptor (org.springframework.tests.aop.interceptor.NopInterceptor)33 PropertyEditorSupport (java.beans.PropertyEditorSupport)31 HashMap (java.util.HashMap)30 List (java.util.List)27 Map (java.util.Map)27 PageContext (javax.servlet.jsp.PageContext)27