use of org.springframework.beans.factory.support.DefaultListableBeanFactory 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);
}
use of org.springframework.beans.factory.support.DefaultListableBeanFactory 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();
}
use of org.springframework.beans.factory.support.DefaultListableBeanFactory 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);
}
use of org.springframework.beans.factory.support.DefaultListableBeanFactory in project spring-framework by spring-projects.
the class AutowiredAnnotationBeanPostProcessorTests method testMethodInjectionWithMap.
@Test
public void testMethodInjectionWithMap() {
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor();
bpp.setBeanFactory(bf);
bf.addBeanPostProcessor(bpp);
RootBeanDefinition bd = new RootBeanDefinition(MapMethodInjectionBean.class);
bd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE);
bf.registerBeanDefinition("annotatedBean", bd);
TestBean tb = new TestBean();
bf.registerSingleton("testBean", tb);
MapMethodInjectionBean bean = (MapMethodInjectionBean) bf.getBean("annotatedBean");
assertEquals(1, bean.getTestBeanMap().size());
assertTrue(bean.getTestBeanMap().keySet().contains("testBean"));
assertTrue(bean.getTestBeanMap().values().contains(tb));
assertSame(tb, bean.getTestBean());
bean = (MapMethodInjectionBean) bf.getBean("annotatedBean");
assertEquals(1, bean.getTestBeanMap().size());
assertTrue(bean.getTestBeanMap().keySet().contains("testBean"));
assertTrue(bean.getTestBeanMap().values().contains(tb));
assertSame(tb, bean.getTestBean());
}
use of org.springframework.beans.factory.support.DefaultListableBeanFactory in project spring-framework by spring-projects.
the class AutowiredAnnotationBeanPostProcessorTests method testSelfReferenceCollection.
@Test
public void testSelfReferenceCollection() {
DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
bf.setAutowireCandidateResolver(new QualifierAnnotationAutowireCandidateResolver());
AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor();
bpp.setBeanFactory(bf);
bf.addBeanPostProcessor(bpp);
bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(SelfInjectionCollectionBean.class));
SelfInjectionCollectionBean bean = (SelfInjectionCollectionBean) bf.getBean("annotatedBean");
assertSame(bean, bean.reference);
assertNull(bean.referenceCollection);
}
Aggregations