diff --git a/spring-beans/src/main/java/org/springframework/beans/factory/support/ConstructorResolver.java b/spring-beans/src/main/java/org/springframework/beans/factory/support/ConstructorResolver.java index 2369fc4043..0e15eced53 100644 --- a/spring-beans/src/main/java/org/springframework/beans/factory/support/ConstructorResolver.java +++ b/spring-beans/src/main/java/org/springframework/beans/factory/support/ConstructorResolver.java @@ -17,6 +17,7 @@ package org.springframework.beans.factory.support; import java.beans.ConstructorProperties; +import java.lang.reflect.Array; import java.lang.reflect.Constructor; import java.lang.reflect.Executable; import java.lang.reflect.Method; @@ -824,9 +825,12 @@ class ConstructorResolver { } catch (NoSuchBeanDefinitionException ex) { if (fallback) { - // Single constructor or factory method -> let's return an - // empty collection for a non-null collection parameter. - if (CollectionFactory.isApproximableCollectionType(paramType)) { + // Single constructor or factory method -> let's return an empty array/collection + // for e.g. a vararg or a non-null List/Set/Map parameter. + if (paramType.isArray()) { + return Array.newInstance(paramType.getComponentType(), 0); + } + else if (CollectionFactory.isApproximableCollectionType(paramType)) { return CollectionFactory.createCollection(paramType, 0); } else if (CollectionFactory.isApproximableMapType(paramType)) { diff --git a/spring-beans/src/main/java/org/springframework/beans/factory/support/DefaultListableBeanFactory.java b/spring-beans/src/main/java/org/springframework/beans/factory/support/DefaultListableBeanFactory.java index 2ed1c5aba3..f0f8163e30 100644 --- a/spring-beans/src/main/java/org/springframework/beans/factory/support/DefaultListableBeanFactory.java +++ b/spring-beans/src/main/java/org/springframework/beans/factory/support/DefaultListableBeanFactory.java @@ -1229,7 +1229,7 @@ public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFacto if (descriptor.isStreamAccess()) { Map matchingBeans = findAutowireCandidates(beanName, type, - new MultiElementDescriptor(descriptor)); + new MultiElementDescriptor(descriptor, false)); if (autowiredBeanNames != null) { autowiredBeanNames.addAll(matchingBeans.keySet()); } @@ -1247,7 +1247,7 @@ public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFacto return null; } Map matchingBeans = findAutowireCandidates(beanName, componentType, - new MultiElementDescriptor(descriptor)); + new MultiElementDescriptor(descriptor, true)); if (matchingBeans.isEmpty()) { return null; } @@ -1267,7 +1267,7 @@ public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFacto return null; } Map matchingBeans = findAutowireCandidates(beanName, elementType, - new MultiElementDescriptor(descriptor)); + new MultiElementDescriptor(descriptor, true)); if (matchingBeans.isEmpty()) { return null; } @@ -1292,7 +1292,7 @@ public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFacto return null; } Map matchingBeans = findAutowireCandidates(beanName, valueType, - new MultiElementDescriptor(descriptor)); + new MultiElementDescriptor(descriptor, true)); if (matchingBeans.isEmpty()) { return null; } @@ -1716,10 +1716,13 @@ public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFacto /** * A dependency descriptor marker for multiple elements. */ - private static class MultiElementDescriptor extends NestedDependencyDescriptor { + private static class MultiElementDescriptor extends DependencyDescriptor { - public MultiElementDescriptor(DependencyDescriptor original) { + public MultiElementDescriptor(DependencyDescriptor original, boolean nested) { super(original); + if (nested) { + increaseNestingLevel(); + } } } diff --git a/spring-beans/src/test/java/org/springframework/beans/factory/annotation/AutowiredAnnotationBeanPostProcessorTests.java b/spring-beans/src/test/java/org/springframework/beans/factory/annotation/AutowiredAnnotationBeanPostProcessorTests.java index b6218143e0..ad74600d4b 100644 --- a/spring-beans/src/test/java/org/springframework/beans/factory/annotation/AutowiredAnnotationBeanPostProcessorTests.java +++ b/spring-beans/src/test/java/org/springframework/beans/factory/annotation/AutowiredAnnotationBeanPostProcessorTests.java @@ -24,6 +24,7 @@ import java.lang.annotation.Target; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; import java.lang.reflect.Proxy; +import java.util.Arrays; import java.util.Collections; import java.util.HashMap; import java.util.HashSet; @@ -37,6 +38,8 @@ import java.util.Set; import java.util.concurrent.Callable; import java.util.stream.Collectors; +import org.junit.After; +import org.junit.Before; import org.junit.Ignore; import org.junit.Test; import org.mockito.Mockito; @@ -80,12 +83,30 @@ import static org.junit.Assert.*; */ public class AutowiredAnnotationBeanPostProcessorTests { - @Test - public void testIncompleteBeanDefinition() { - DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); - AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor(); + private DefaultListableBeanFactory bf; + + private AutowiredAnnotationBeanPostProcessor bpp; + + + @Before + public void setup() { + bf = new DefaultListableBeanFactory(); + bf.registerResolvableDependency(BeanFactory.class, bf); + bpp = new AutowiredAnnotationBeanPostProcessor(); bpp.setBeanFactory(bf); bf.addBeanPostProcessor(bpp); + bf.setAutowireCandidateResolver(new QualifierAnnotationAutowireCandidateResolver()); + bf.setDependencyComparator(AnnotationAwareOrderComparator.INSTANCE); + } + + @After + public void close() { + bf.destroySingletons(); + } + + + @Test + public void testIncompleteBeanDefinition() { bf.registerBeanDefinition("testBean", new GenericBeanDefinition()); try { bf.getBean("testBean"); @@ -98,10 +119,6 @@ public class AutowiredAnnotationBeanPostProcessorTests { @Test public void testResourceInjection() { - DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); - AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor(); - bpp.setBeanFactory(bf); - bf.addBeanPostProcessor(bpp); RootBeanDefinition bd = new RootBeanDefinition(ResourceInjectionBean.class); bd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE); bf.registerBeanDefinition("annotatedBean", bd); @@ -119,11 +136,6 @@ public class AutowiredAnnotationBeanPostProcessorTests { @Test public void testExtendedResourceInjection() { - DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); - bf.registerResolvableDependency(BeanFactory.class, bf); - AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor(); - bpp.setBeanFactory(bf); - bf.addBeanPostProcessor(bpp); RootBeanDefinition bd = new RootBeanDefinition(TypedExtendedResourceInjectionBean.class); bd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE); bf.registerBeanDefinition("annotatedBean", bd); @@ -156,11 +168,6 @@ public class AutowiredAnnotationBeanPostProcessorTests { @Test public void testExtendedResourceInjectionWithDestruction() { - DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); - bf.registerResolvableDependency(BeanFactory.class, bf); - AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor(); - bpp.setBeanFactory(bf); - bf.addBeanPostProcessor(bpp); bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(TypedExtendedResourceInjectionBean.class)); bf.registerBeanDefinition("testBean", new RootBeanDefinition(TestBean.class)); NestedTestBean ntb = new NestedTestBean(); @@ -185,11 +192,6 @@ public class AutowiredAnnotationBeanPostProcessorTests { @Test public void testExtendedResourceInjectionWithOverriding() { - DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); - bf.registerResolvableDependency(BeanFactory.class, bf); - AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor(); - bpp.setBeanFactory(bf); - bf.addBeanPostProcessor(bpp); RootBeanDefinition annotatedBd = new RootBeanDefinition(TypedExtendedResourceInjectionBean.class); TestBean tb2 = new TestBean(); annotatedBd.getPropertyValues().add("testBean2", tb2); @@ -206,16 +208,10 @@ public class AutowiredAnnotationBeanPostProcessorTests { assertSame(tb, bean.getTestBean4()); assertSame(ntb, bean.getNestedTestBean()); assertSame(bf, bean.getBeanFactory()); - bf.destroySingletons(); } @Test public void testExtendedResourceInjectionWithSkippedOverriddenMethods() { - DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); - bf.registerResolvableDependency(BeanFactory.class, bf); - AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor(); - bpp.setBeanFactory(bf); - bf.addBeanPostProcessor(bpp); RootBeanDefinition annotatedBd = new RootBeanDefinition(OverriddenExtendedResourceInjectionBean.class); bf.registerBeanDefinition("annotatedBean", annotatedBd); TestBean tb = new TestBean(); @@ -232,16 +228,10 @@ public class AutowiredAnnotationBeanPostProcessorTests { assertNull(bean.getBeanFactory()); assertTrue(bean.baseInjected); assertTrue(bean.subInjected); - bf.destroySingletons(); } @Test public void testExtendedResourceInjectionWithDefaultMethod() { - DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); - bf.registerResolvableDependency(BeanFactory.class, bf); - AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor(); - bpp.setBeanFactory(bf); - bf.addBeanPostProcessor(bpp); RootBeanDefinition annotatedBd = new RootBeanDefinition(DefaultMethodResourceInjectionBean.class); bf.registerBeanDefinition("annotatedBean", annotatedBd); TestBean tb = new TestBean(); @@ -258,16 +248,10 @@ public class AutowiredAnnotationBeanPostProcessorTests { assertNull(bean.getBeanFactory()); assertTrue(bean.baseInjected); assertTrue(bean.subInjected); - bf.destroySingletons(); } @Test public void testExtendedResourceInjectionWithAtRequired() { - DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); - bf.registerResolvableDependency(BeanFactory.class, bf); - AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor(); - bpp.setBeanFactory(bf); - bf.addBeanPostProcessor(bpp); bf.addBeanPostProcessor(new RequiredAnnotationBeanPostProcessor()); RootBeanDefinition bd = new RootBeanDefinition(TypedExtendedResourceInjectionBean.class); bd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE); @@ -288,10 +272,6 @@ public class AutowiredAnnotationBeanPostProcessorTests { @Test public void testOptionalResourceInjection() { - DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); - AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor(); - bpp.setBeanFactory(bf); - bf.addBeanPostProcessor(bpp); bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(OptionalResourceInjectionBean.class)); TestBean tb = new TestBean(); bf.registerSingleton("testBean", tb); @@ -314,15 +294,10 @@ public class AutowiredAnnotationBeanPostProcessorTests { assertEquals(2, bean.nestedTestBeansField.length); assertSame(ntb1, bean.nestedTestBeansField[0]); assertSame(ntb2, bean.nestedTestBeansField[1]); - bf.destroySingletons(); } @Test public void testOptionalCollectionResourceInjection() { - DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); - AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor(); - bpp.setBeanFactory(bf); - bf.addBeanPostProcessor(bpp); RootBeanDefinition rbd = new RootBeanDefinition(OptionalCollectionResourceInjectionBean.class); rbd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE); bf.registerBeanDefinition("annotatedBean", rbd); @@ -352,15 +327,10 @@ public class AutowiredAnnotationBeanPostProcessorTests { assertEquals(2, bean.nestedTestBeansField.size()); assertSame(ntb1, bean.nestedTestBeansField.get(0)); assertSame(ntb2, bean.nestedTestBeansField.get(1)); - bf.destroySingletons(); } @Test public void testOptionalCollectionResourceInjectionWithSingleElement() { - DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); - AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor(); - bpp.setBeanFactory(bf); - bf.addBeanPostProcessor(bpp); RootBeanDefinition rbd = new RootBeanDefinition(OptionalCollectionResourceInjectionBean.class); rbd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE); bf.registerBeanDefinition("annotatedBean", rbd); @@ -385,15 +355,10 @@ public class AutowiredAnnotationBeanPostProcessorTests { assertSame(ntb1, bean.nestedTestBeansSetter.get(0)); assertEquals(1, bean.nestedTestBeansField.size()); assertSame(ntb1, bean.nestedTestBeansField.get(0)); - bf.destroySingletons(); } @Test public void testOptionalResourceInjectionWithIncompleteDependencies() { - DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); - AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor(); - bpp.setBeanFactory(bf); - bf.addBeanPostProcessor(bpp); bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(OptionalResourceInjectionBean.class)); TestBean tb = new TestBean(); bf.registerSingleton("testBean", tb); @@ -404,15 +369,10 @@ public class AutowiredAnnotationBeanPostProcessorTests { assertSame(tb, bean.getTestBean3()); assertNull(bean.getTestBean4()); assertNull(bean.getNestedTestBeans()); - bf.destroySingletons(); } @Test public void testOptionalResourceInjectionWithNoDependencies() { - DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); - AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor(); - bpp.setBeanFactory(bf); - bf.addBeanPostProcessor(bpp); bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(OptionalResourceInjectionBean.class)); OptionalResourceInjectionBean bean = (OptionalResourceInjectionBean) bf.getBean("annotatedBean"); @@ -421,16 +381,10 @@ public class AutowiredAnnotationBeanPostProcessorTests { assertNull(bean.getTestBean3()); assertNull(bean.getTestBean4()); assertNull(bean.getNestedTestBeans()); - bf.destroySingletons(); } @Test public void testOrderedResourceInjection() { - DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); - bf.setDependencyComparator(AnnotationAwareOrderComparator.INSTANCE); - AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor(); - bpp.setBeanFactory(bf); - bf.addBeanPostProcessor(bpp); bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(OptionalResourceInjectionBean.class)); TestBean tb = new TestBean(); bf.registerSingleton("testBean", tb); @@ -455,16 +409,10 @@ public class AutowiredAnnotationBeanPostProcessorTests { assertEquals(2, bean.nestedTestBeansField.length); assertSame(ntb2, bean.nestedTestBeansField[0]); assertSame(ntb1, bean.nestedTestBeansField[1]); - bf.destroySingletons(); } @Test public void testAnnotationOrderedResourceInjection() { - DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); - bf.setDependencyComparator(AnnotationAwareOrderComparator.INSTANCE); - AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor(); - bpp.setBeanFactory(bf); - bf.addBeanPostProcessor(bpp); bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(OptionalResourceInjectionBean.class)); TestBean tb = new TestBean(); bf.registerSingleton("testBean", tb); @@ -487,16 +435,10 @@ public class AutowiredAnnotationBeanPostProcessorTests { assertEquals(2, bean.nestedTestBeansField.length); assertSame(ntb2, bean.nestedTestBeansField[0]); assertSame(ntb1, bean.nestedTestBeansField[1]); - bf.destroySingletons(); } @Test public void testOrderedCollectionResourceInjection() { - DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); - bf.setDependencyComparator(AnnotationAwareOrderComparator.INSTANCE); - AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor(); - bpp.setBeanFactory(bf); - bf.addBeanPostProcessor(bpp); RootBeanDefinition rbd = new RootBeanDefinition(OptionalCollectionResourceInjectionBean.class); rbd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE); bf.registerBeanDefinition("annotatedBean", rbd); @@ -528,16 +470,10 @@ public class AutowiredAnnotationBeanPostProcessorTests { assertEquals(2, bean.nestedTestBeansField.size()); assertSame(ntb2, bean.nestedTestBeansField.get(0)); assertSame(ntb1, bean.nestedTestBeansField.get(1)); - bf.destroySingletons(); } @Test public void testAnnotationOrderedCollectionResourceInjection() { - DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); - bf.setDependencyComparator(AnnotationAwareOrderComparator.INSTANCE); - AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor(); - bpp.setBeanFactory(bf); - bf.addBeanPostProcessor(bpp); RootBeanDefinition rbd = new RootBeanDefinition(OptionalCollectionResourceInjectionBean.class); rbd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE); bf.registerBeanDefinition("annotatedBean", rbd); @@ -567,16 +503,10 @@ public class AutowiredAnnotationBeanPostProcessorTests { assertEquals(2, bean.nestedTestBeansField.size()); assertSame(ntb2, bean.nestedTestBeansField.get(0)); assertSame(ntb1, bean.nestedTestBeansField.get(1)); - bf.destroySingletons(); } @Test public void testConstructorResourceInjection() { - DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); - bf.registerResolvableDependency(BeanFactory.class, bf); - AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor(); - bpp.setBeanFactory(bf); - bf.addBeanPostProcessor(bpp); RootBeanDefinition bd = new RootBeanDefinition(ConstructorResourceInjectionBean.class); bd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE); bf.registerBeanDefinition("annotatedBean", bd); @@ -604,12 +534,6 @@ public class AutowiredAnnotationBeanPostProcessorTests { @Test public void testConstructorResourceInjectionWithNullFromFactoryBean() { - DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); - bf.setAutowireCandidateResolver(new QualifierAnnotationAutowireCandidateResolver()); - bf.registerResolvableDependency(BeanFactory.class, bf); - AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor(); - bpp.setBeanFactory(bf); - bf.addBeanPostProcessor(bpp); RootBeanDefinition bd = new RootBeanDefinition(ConstructorResourceInjectionBean.class); bd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE); bf.registerBeanDefinition("annotatedBean", bd); @@ -637,12 +561,6 @@ public class AutowiredAnnotationBeanPostProcessorTests { @Test public void testConstructorResourceInjectionWithNullFromFactoryMethod() { - DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); - bf.setAutowireCandidateResolver(new QualifierAnnotationAutowireCandidateResolver()); - bf.registerResolvableDependency(BeanFactory.class, bf); - AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor(); - bpp.setBeanFactory(bf); - bf.addBeanPostProcessor(bpp); RootBeanDefinition bd = new RootBeanDefinition(ConstructorResourceInjectionBean.class); bd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE); bf.registerBeanDefinition("annotatedBean", bd); @@ -673,10 +591,6 @@ public class AutowiredAnnotationBeanPostProcessorTests { @Test public void testConstructorResourceInjectionWithMultipleCandidates() { - DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); - AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor(); - bpp.setBeanFactory(bf); - bf.addBeanPostProcessor(bpp); bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(ConstructorsResourceInjectionBean.class)); TestBean tb = new TestBean(); bf.registerSingleton("testBean", tb); @@ -691,15 +605,10 @@ public class AutowiredAnnotationBeanPostProcessorTests { assertEquals(2, bean.getNestedTestBeans().length); assertSame(ntb1, bean.getNestedTestBeans()[0]); assertSame(ntb2, bean.getNestedTestBeans()[1]); - bf.destroySingletons(); } @Test public void testConstructorResourceInjectionWithNoCandidatesAndNoFallback() { - DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); - AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor(); - bpp.setBeanFactory(bf); - bf.addBeanPostProcessor(bpp); bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(ConstructorWithoutFallbackBean.class)); try { @@ -714,10 +623,6 @@ public class AutowiredAnnotationBeanPostProcessorTests { @Test public void testConstructorResourceInjectionWithCollectionAndNullFromFactoryBean() { - DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); - AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor(); - bpp.setBeanFactory(bf); - bf.addBeanPostProcessor(bpp); bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition( ConstructorsCollectionResourceInjectionBean.class)); TestBean tb = new TestBean(); @@ -735,16 +640,10 @@ public class AutowiredAnnotationBeanPostProcessorTests { Map map = bf.getBeansOfType(NestedTestBean.class); assertNull(map.get("nestedTestBean1")); assertSame(ntb2, map.get("nestedTestBean2")); - - bf.destroySingletons(); } @Test public void testConstructorResourceInjectionWithMultipleCandidatesAsCollection() { - DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); - AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor(); - bpp.setBeanFactory(bf); - bf.addBeanPostProcessor(bpp); bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition( ConstructorsCollectionResourceInjectionBean.class)); TestBean tb = new TestBean(); @@ -760,16 +659,10 @@ public class AutowiredAnnotationBeanPostProcessorTests { assertEquals(2, bean.getNestedTestBeans().size()); assertSame(ntb1, bean.getNestedTestBeans().get(0)); assertSame(ntb2, bean.getNestedTestBeans().get(1)); - bf.destroySingletons(); } @Test public void testConstructorResourceInjectionWithMultipleOrderedCandidates() { - DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); - bf.setDependencyComparator(AnnotationAwareOrderComparator.INSTANCE); - AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor(); - bpp.setBeanFactory(bf); - bf.addBeanPostProcessor(bpp); bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(ConstructorsResourceInjectionBean.class)); TestBean tb = new TestBean(); bf.registerSingleton("testBean", tb); @@ -784,16 +677,10 @@ public class AutowiredAnnotationBeanPostProcessorTests { assertEquals(2, bean.getNestedTestBeans().length); assertSame(ntb2, bean.getNestedTestBeans()[0]); assertSame(ntb1, bean.getNestedTestBeans()[1]); - bf.destroySingletons(); } @Test public void testConstructorResourceInjectionWithMultipleCandidatesAsOrderedCollection() { - DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); - bf.setDependencyComparator(AnnotationAwareOrderComparator.INSTANCE); - AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor(); - bpp.setBeanFactory(bf); - bf.addBeanPostProcessor(bpp); bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(ConstructorsCollectionResourceInjectionBean.class)); TestBean tb = new TestBean(); bf.registerSingleton("testBean", tb); @@ -808,16 +695,39 @@ public class AutowiredAnnotationBeanPostProcessorTests { assertEquals(2, bean.getNestedTestBeans().size()); assertSame(ntb2, bean.getNestedTestBeans().get(0)); assertSame(ntb1, bean.getNestedTestBeans().get(1)); - bf.destroySingletons(); + } + + @Test + public void testSingleConstructorInjectionWithMultipleCandidatesAsRequiredVararg() { + bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(SingleConstructorVarargBean.class)); + TestBean tb = new TestBean(); + bf.registerSingleton("testBean", tb); + FixedOrder2NestedTestBean ntb1 = new FixedOrder2NestedTestBean(); + bf.registerSingleton("nestedTestBean1", ntb1); + FixedOrder1NestedTestBean ntb2 = new FixedOrder1NestedTestBean(); + bf.registerSingleton("nestedTestBean2", ntb2); + + SingleConstructorVarargBean bean = (SingleConstructorVarargBean) bf.getBean("annotatedBean"); + assertSame(tb, bean.getTestBean()); + assertEquals(2, bean.getNestedTestBeans().size()); + assertSame(ntb2, bean.getNestedTestBeans().get(0)); + assertSame(ntb1, bean.getNestedTestBeans().get(1)); + } + + @Test + public void testSingleConstructorInjectionWithEmptyVararg() { + bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(SingleConstructorVarargBean.class)); + TestBean tb = new TestBean(); + bf.registerSingleton("testBean", tb); + + SingleConstructorVarargBean bean = (SingleConstructorVarargBean) bf.getBean("annotatedBean"); + assertSame(tb, bean.getTestBean()); + assertNotNull(bean.getNestedTestBeans()); + assertTrue(bean.getNestedTestBeans().isEmpty()); } @Test public void testSingleConstructorInjectionWithMultipleCandidatesAsRequiredCollection() { - DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); - bf.setDependencyComparator(AnnotationAwareOrderComparator.INSTANCE); - AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor(); - bpp.setBeanFactory(bf); - bf.addBeanPostProcessor(bpp); bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(SingleConstructorRequiredCollectionBean.class)); TestBean tb = new TestBean(); bf.registerSingleton("testBean", tb); @@ -831,16 +741,10 @@ public class AutowiredAnnotationBeanPostProcessorTests { assertEquals(2, bean.getNestedTestBeans().size()); assertSame(ntb2, bean.getNestedTestBeans().get(0)); assertSame(ntb1, bean.getNestedTestBeans().get(1)); - bf.destroySingletons(); } @Test public void testSingleConstructorInjectionWithEmptyCollection() { - DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); - bf.setAutowireCandidateResolver(new QualifierAnnotationAutowireCandidateResolver()); - AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor(); - bpp.setBeanFactory(bf); - bf.addBeanPostProcessor(bpp); bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(SingleConstructorRequiredCollectionBean.class)); TestBean tb = new TestBean(); bf.registerSingleton("testBean", tb); @@ -849,16 +753,10 @@ public class AutowiredAnnotationBeanPostProcessorTests { assertSame(tb, bean.getTestBean()); assertNotNull(bean.getNestedTestBeans()); assertTrue(bean.getNestedTestBeans().isEmpty()); - bf.destroySingletons(); } @Test public void testSingleConstructorInjectionWithMultipleCandidatesAsOrderedCollection() { - DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); - bf.setDependencyComparator(AnnotationAwareOrderComparator.INSTANCE); - AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor(); - bpp.setBeanFactory(bf); - bf.addBeanPostProcessor(bpp); bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(SingleConstructorOptionalCollectionBean.class)); TestBean tb = new TestBean(); bf.registerSingleton("testBean", tb); @@ -872,16 +770,10 @@ public class AutowiredAnnotationBeanPostProcessorTests { assertEquals(2, bean.getNestedTestBeans().size()); assertSame(ntb2, bean.getNestedTestBeans().get(0)); assertSame(ntb1, bean.getNestedTestBeans().get(1)); - bf.destroySingletons(); } @Test public void testSingleConstructorInjectionWithEmptyCollectionAsNull() { - DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); - bf.setAutowireCandidateResolver(new QualifierAnnotationAutowireCandidateResolver()); - AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor(); - bpp.setBeanFactory(bf); - bf.addBeanPostProcessor(bpp); bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(SingleConstructorOptionalCollectionBean.class)); TestBean tb = new TestBean(); bf.registerSingleton("testBean", tb); @@ -889,28 +781,16 @@ public class AutowiredAnnotationBeanPostProcessorTests { SingleConstructorOptionalCollectionBean bean = (SingleConstructorOptionalCollectionBean) bf.getBean("annotatedBean"); assertSame(tb, bean.getTestBean()); assertNull(bean.getNestedTestBeans()); - bf.destroySingletons(); } @Test(expected = UnsatisfiedDependencyException.class) public void testSingleConstructorInjectionWithMissingDependency() { - DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); - bf.setAutowireCandidateResolver(new QualifierAnnotationAutowireCandidateResolver()); - AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor(); - bpp.setBeanFactory(bf); - bf.addBeanPostProcessor(bpp); bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(SingleConstructorOptionalCollectionBean.class)); - bf.getBean("annotatedBean"); } @Test(expected = UnsatisfiedDependencyException.class) public void testSingleConstructorInjectionWithNullDependency() { - DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); - bf.setAutowireCandidateResolver(new QualifierAnnotationAutowireCandidateResolver()); - AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor(); - bpp.setBeanFactory(bf); - bf.addBeanPostProcessor(bpp); bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(SingleConstructorOptionalCollectionBean.class)); RootBeanDefinition tb = new RootBeanDefinition(NullFactoryMethods.class); tb.setFactoryMethodName("createTestBean"); @@ -921,10 +801,6 @@ public class AutowiredAnnotationBeanPostProcessorTests { @Test public void testConstructorResourceInjectionWithMultipleCandidatesAndFallback() { - DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); - AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor(); - bpp.setBeanFactory(bf); - bf.addBeanPostProcessor(bpp); bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(ConstructorsResourceInjectionBean.class)); TestBean tb = new TestBean(); bf.registerSingleton("testBean", tb); @@ -932,29 +808,19 @@ public class AutowiredAnnotationBeanPostProcessorTests { ConstructorsResourceInjectionBean bean = (ConstructorsResourceInjectionBean) bf.getBean("annotatedBean"); assertSame(tb, bean.getTestBean3()); assertNull(bean.getTestBean4()); - bf.destroySingletons(); } @Test public void testConstructorResourceInjectionWithMultipleCandidatesAndDefaultFallback() { - DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); - AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor(); - bpp.setBeanFactory(bf); - bf.addBeanPostProcessor(bpp); bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(ConstructorsResourceInjectionBean.class)); ConstructorsResourceInjectionBean bean = (ConstructorsResourceInjectionBean) bf.getBean("annotatedBean"); assertNull(bean.getTestBean3()); assertNull(bean.getTestBean4()); - bf.destroySingletons(); } @Test public void testConstructorInjectionWithMap() { - DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); - AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor(); - bpp.setBeanFactory(bf); - bf.addBeanPostProcessor(bpp); RootBeanDefinition bd = new RootBeanDefinition(MapConstructorInjectionBean.class); bd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE); bf.registerBeanDefinition("annotatedBean", bd); @@ -977,10 +843,6 @@ public class AutowiredAnnotationBeanPostProcessorTests { @Test public void testFieldInjectionWithMap() { - DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); - AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor(); - bpp.setBeanFactory(bf); - bf.addBeanPostProcessor(bpp); RootBeanDefinition bd = new RootBeanDefinition(MapFieldInjectionBean.class); bd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE); bf.registerBeanDefinition("annotatedBean", bd); @@ -1006,10 +868,6 @@ public class AutowiredAnnotationBeanPostProcessorTests { @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); @@ -1031,10 +889,6 @@ public class AutowiredAnnotationBeanPostProcessorTests { @Test public void testMethodInjectionWithMapAndMultipleMatches() { - 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)); bf.registerBeanDefinition("testBean2", new RootBeanDefinition(TestBean.class)); @@ -1047,15 +901,10 @@ public class AutowiredAnnotationBeanPostProcessorTests { // expected assertSame(MapMethodInjectionBean.class, ex.getInjectionPoint().getMethodParameter().getDeclaringClass()); } - bf.destroySingletons(); } @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); @@ -1068,30 +917,19 @@ public class AutowiredAnnotationBeanPostProcessorTests { assertTrue(bean.getTestBeanMap().keySet().contains("testBean1")); assertTrue(bean.getTestBeanMap().values().contains(tb)); assertSame(tb, bean.getTestBean()); - bf.destroySingletons(); } @Test public void testMethodInjectionWithMapAndNoMatches() { - DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); - AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor(); - bpp.setBeanFactory(bf); - bf.addBeanPostProcessor(bpp); bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(MapMethodInjectionBean.class)); MapMethodInjectionBean bean = (MapMethodInjectionBean) bf.getBean("annotatedBean"); assertNull(bean.getTestBeanMap()); assertNull(bean.getTestBean()); - bf.destroySingletons(); } @Test public void testConstructorInjectionWithTypedMapAsBean() { - DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); - bf.setAutowireCandidateResolver(new QualifierAnnotationAutowireCandidateResolver()); - AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor(); - bpp.setBeanFactory(bf); - bf.addBeanPostProcessor(bpp); RootBeanDefinition bd = new RootBeanDefinition(MapConstructorInjectionBean.class); bd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE); bf.registerBeanDefinition("annotatedBean", bd); @@ -1109,11 +947,6 @@ public class AutowiredAnnotationBeanPostProcessorTests { @Test public void testConstructorInjectionWithPlainMapAsBean() { - DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); - bf.setAutowireCandidateResolver(new QualifierAnnotationAutowireCandidateResolver()); - AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor(); - bpp.setBeanFactory(bf); - bf.addBeanPostProcessor(bpp); RootBeanDefinition bd = new RootBeanDefinition(MapConstructorInjectionBean.class); bd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE); bf.registerBeanDefinition("annotatedBean", bd); @@ -1130,11 +963,6 @@ public class AutowiredAnnotationBeanPostProcessorTests { @Test public void testConstructorInjectionWithCustomMapAsBean() { - DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); - bf.setAutowireCandidateResolver(new QualifierAnnotationAutowireCandidateResolver()); - AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor(); - bpp.setBeanFactory(bf); - bf.addBeanPostProcessor(bpp); RootBeanDefinition bd = new RootBeanDefinition(CustomMapConstructorInjectionBean.class); bd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE); bf.registerBeanDefinition("annotatedBean", bd); @@ -1152,11 +980,6 @@ public class AutowiredAnnotationBeanPostProcessorTests { @Test public void testConstructorInjectionWithTypedSetAsBean() { - DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); - bf.setAutowireCandidateResolver(new QualifierAnnotationAutowireCandidateResolver()); - AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor(); - bpp.setBeanFactory(bf); - bf.addBeanPostProcessor(bpp); RootBeanDefinition bd = new RootBeanDefinition(SetConstructorInjectionBean.class); bd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE); bf.registerBeanDefinition("annotatedBean", bd); @@ -1174,11 +997,6 @@ public class AutowiredAnnotationBeanPostProcessorTests { @Test public void testConstructorInjectionWithPlainSetAsBean() { - DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); - bf.setAutowireCandidateResolver(new QualifierAnnotationAutowireCandidateResolver()); - AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor(); - bpp.setBeanFactory(bf); - bf.addBeanPostProcessor(bpp); RootBeanDefinition bd = new RootBeanDefinition(SetConstructorInjectionBean.class); bd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE); bf.registerBeanDefinition("annotatedBean", bd); @@ -1195,11 +1013,6 @@ public class AutowiredAnnotationBeanPostProcessorTests { @Test public void testConstructorInjectionWithCustomSetAsBean() { - DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); - bf.setAutowireCandidateResolver(new QualifierAnnotationAutowireCandidateResolver()); - AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor(); - bpp.setBeanFactory(bf); - bf.addBeanPostProcessor(bpp); RootBeanDefinition bd = new RootBeanDefinition(CustomSetConstructorInjectionBean.class); bd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE); bf.registerBeanDefinition("annotatedBean", bd); @@ -1215,11 +1028,6 @@ public class AutowiredAnnotationBeanPostProcessorTests { @Test public void testSelfReference() { - DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); - bf.setAutowireCandidateResolver(new QualifierAnnotationAutowireCandidateResolver()); - AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor(); - bpp.setBeanFactory(bf); - bf.addBeanPostProcessor(bpp); bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(SelfInjectionBean.class)); SelfInjectionBean bean = (SelfInjectionBean) bf.getBean("annotatedBean"); @@ -1229,11 +1037,6 @@ public class AutowiredAnnotationBeanPostProcessorTests { @Test public void testSelfReferenceWithOther() { - DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); - bf.setAutowireCandidateResolver(new QualifierAnnotationAutowireCandidateResolver()); - AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor(); - bpp.setBeanFactory(bf); - bf.addBeanPostProcessor(bpp); bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(SelfInjectionBean.class)); bf.registerBeanDefinition("annotatedBean2", new RootBeanDefinition(SelfInjectionBean.class)); @@ -1246,11 +1049,6 @@ public class AutowiredAnnotationBeanPostProcessorTests { @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"); @@ -1260,11 +1058,6 @@ public class AutowiredAnnotationBeanPostProcessorTests { @Test public void testSelfReferenceCollectionWithOther() { - 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)); bf.registerBeanDefinition("annotatedBean2", new RootBeanDefinition(SelfInjectionCollectionBean.class)); @@ -1277,38 +1070,24 @@ public class AutowiredAnnotationBeanPostProcessorTests { @Test public void testObjectFactoryFieldInjection() { - DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); - AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor(); - bpp.setBeanFactory(bf); - bf.addBeanPostProcessor(bpp); bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(ObjectFactoryFieldInjectionBean.class)); bf.registerBeanDefinition("testBean", new RootBeanDefinition(TestBean.class)); ObjectFactoryFieldInjectionBean bean = (ObjectFactoryFieldInjectionBean) bf.getBean("annotatedBean"); assertSame(bf.getBean("testBean"), bean.getTestBean()); - bf.destroySingletons(); } @Test public void testObjectFactoryConstructorInjection() { - DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); - AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor(); - bpp.setBeanFactory(bf); - bf.addBeanPostProcessor(bpp); bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(ObjectFactoryConstructorInjectionBean.class)); bf.registerBeanDefinition("testBean", new RootBeanDefinition(TestBean.class)); ObjectFactoryConstructorInjectionBean bean = (ObjectFactoryConstructorInjectionBean) bf.getBean("annotatedBean"); assertSame(bf.getBean("testBean"), bean.getTestBean()); - bf.destroySingletons(); } @Test public void testObjectFactoryInjectionIntoPrototypeBean() { - DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); - AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor(); - bpp.setBeanFactory(bf); - bf.addBeanPostProcessor(bpp); RootBeanDefinition annotatedBeanDefinition = new RootBeanDefinition(ObjectFactoryFieldInjectionBean.class); annotatedBeanDefinition.setScope(BeanDefinition.SCOPE_PROTOTYPE); bf.registerBeanDefinition("annotatedBean", annotatedBeanDefinition); @@ -1323,11 +1102,6 @@ public class AutowiredAnnotationBeanPostProcessorTests { @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")); @@ -1336,16 +1110,10 @@ public class AutowiredAnnotationBeanPostProcessorTests { ObjectFactoryQualifierInjectionBean bean = (ObjectFactoryQualifierInjectionBean) bf.getBean("annotatedBean"); assertSame(bf.getBean("dependencyBean"), bean.getTestBean()); - bf.destroySingletons(); } @Test public void testObjectFactoryQualifierProviderInjection() { - 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.setQualifiedElement(ReflectionUtils.findMethod(getClass(), "testBeanQualifierProvider")); @@ -1354,15 +1122,10 @@ public class AutowiredAnnotationBeanPostProcessorTests { ObjectFactoryQualifierInjectionBean bean = (ObjectFactoryQualifierInjectionBean) bf.getBean("annotatedBean"); assertSame(bf.getBean("dependencyBean"), bean.getTestBean()); - bf.destroySingletons(); } @Test public void testObjectFactorySerialization() throws Exception { - DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); - AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor(); - bpp.setBeanFactory(bf); - bf.addBeanPostProcessor(bpp); bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(ObjectFactoryFieldInjectionBean.class)); bf.registerBeanDefinition("testBean", new RootBeanDefinition(TestBean.class)); bf.setSerializationId("test"); @@ -1371,15 +1134,10 @@ public class AutowiredAnnotationBeanPostProcessorTests { assertSame(bf.getBean("testBean"), bean.getTestBean()); bean = (ObjectFactoryFieldInjectionBean) SerializationTestUtils.serializeAndDeserialize(bean); assertSame(bf.getBean("testBean"), bean.getTestBean()); - bf.destroySingletons(); } @Test public void testObjectProviderInjectionWithPrototype() { - DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); - AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor(); - bpp.setBeanFactory(bf); - bf.addBeanPostProcessor(bpp); bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(ObjectProviderInjectionBean.class)); RootBeanDefinition tbd = new RootBeanDefinition(TestBean.class); tbd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE); @@ -1404,16 +1162,10 @@ public class AutowiredAnnotationBeanPostProcessorTests { testBeans = bean.streamTestBeans(); assertEquals(1, testBeans.size()); assertTrue(testBeans.contains(bf.getBean("testBean"))); - - bf.destroySingletons(); } @Test public void testObjectProviderInjectionWithSingletonTarget() { - DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); - AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor(); - bpp.setBeanFactory(bf); - bf.addBeanPostProcessor(bpp); bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(ObjectProviderInjectionBean.class)); bf.registerBeanDefinition("testBean", new RootBeanDefinition(TestBean.class)); @@ -1435,16 +1187,10 @@ public class AutowiredAnnotationBeanPostProcessorTests { testBeans = bean.streamTestBeans(); assertEquals(1, testBeans.size()); assertTrue(testBeans.contains(bf.getBean("testBean"))); - - bf.destroySingletons(); } @Test public void testObjectProviderInjectionWithTargetNotAvailable() { - DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); - AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor(); - bpp.setBeanFactory(bf); - bf.addBeanPostProcessor(bpp); bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(ObjectProviderInjectionBean.class)); ObjectProviderInjectionBean bean = (ObjectProviderInjectionBean) bf.getBean("annotatedBean"); @@ -1468,16 +1214,10 @@ public class AutowiredAnnotationBeanPostProcessorTests { assertTrue(testBeans.isEmpty()); testBeans = bean.streamTestBeans(); assertTrue(testBeans.isEmpty()); - - bf.destroySingletons(); } @Test public void testObjectProviderInjectionWithTargetNotUnique() { - DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); - AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor(); - bpp.setBeanFactory(bf); - bf.addBeanPostProcessor(bpp); bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(ObjectProviderInjectionBean.class)); bf.registerBeanDefinition("testBean1", new RootBeanDefinition(TestBean.class)); bf.registerBeanDefinition("testBean2", new RootBeanDefinition(TestBean.class)); @@ -1519,16 +1259,10 @@ public class AutowiredAnnotationBeanPostProcessorTests { assertEquals(2, testBeans.size()); assertTrue(testBeans.contains(bf.getBean("testBean1"))); assertTrue(testBeans.contains(bf.getBean("testBean2"))); - - bf.destroySingletons(); } @Test public void testObjectProviderInjectionWithTargetPrimary() { - DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); - AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor(); - bpp.setBeanFactory(bf); - bf.addBeanPostProcessor(bpp); bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(ObjectProviderInjectionBean.class)); RootBeanDefinition tb1 = new RootBeanDefinition(TestBean.class); tb1.setPrimary(true); @@ -1557,19 +1291,13 @@ public class AutowiredAnnotationBeanPostProcessorTests { assertEquals(2, testBeans.size()); assertTrue(testBeans.contains(bf.getBean("testBean1"))); assertTrue(testBeans.contains(bf.getBean("testBean2"))); - - bf.destroySingletons(); } @Test public void testCustomAnnotationRequiredFieldResourceInjection() { - 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)); TestBean tb = new TestBean(); @@ -1578,18 +1306,13 @@ public class AutowiredAnnotationBeanPostProcessorTests { CustomAnnotationRequiredFieldResourceInjectionBean bean = (CustomAnnotationRequiredFieldResourceInjectionBean) bf.getBean("customBean"); assertSame(tb, bean.getTestBean()); - bf.destroySingletons(); } @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)); @@ -1602,18 +1325,13 @@ public class AutowiredAnnotationBeanPostProcessorTests { assertSame(CustomAnnotationRequiredFieldResourceInjectionBean.class, ex.getInjectionPoint().getField().getDeclaringClass()); } - bf.destroySingletons(); } @Test public void testCustomAnnotationRequiredFieldResourceInjectionFailsWhenMultipleDependenciesFound() { - 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)); TestBean tb1 = new TestBean(); @@ -1630,18 +1348,13 @@ public class AutowiredAnnotationBeanPostProcessorTests { assertSame(CustomAnnotationRequiredFieldResourceInjectionBean.class, ex.getInjectionPoint().getField().getDeclaringClass()); } - bf.destroySingletons(); } @Test public void testCustomAnnotationRequiredMethodResourceInjection() { - 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( CustomAnnotationRequiredMethodResourceInjectionBean.class)); TestBean tb = new TestBean(); @@ -1650,18 +1363,13 @@ public class AutowiredAnnotationBeanPostProcessorTests { CustomAnnotationRequiredMethodResourceInjectionBean bean = (CustomAnnotationRequiredMethodResourceInjectionBean) bf.getBean("customBean"); assertSame(tb, bean.getTestBean()); - bf.destroySingletons(); } @Test public void testCustomAnnotationRequiredMethodResourceInjectionFailsWhenNoDependencyFound() { - 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( CustomAnnotationRequiredMethodResourceInjectionBean.class)); @@ -1674,18 +1382,13 @@ public class AutowiredAnnotationBeanPostProcessorTests { assertSame(CustomAnnotationRequiredMethodResourceInjectionBean.class, ex.getInjectionPoint().getMethodParameter().getDeclaringClass()); } - bf.destroySingletons(); } @Test public void testCustomAnnotationRequiredMethodResourceInjectionFailsWhenMultipleDependenciesFound() { - 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( CustomAnnotationRequiredMethodResourceInjectionBean.class)); TestBean tb1 = new TestBean(); @@ -1702,18 +1405,13 @@ public class AutowiredAnnotationBeanPostProcessorTests { assertSame(CustomAnnotationRequiredMethodResourceInjectionBean.class, ex.getInjectionPoint().getMethodParameter().getDeclaringClass()); } - bf.destroySingletons(); } @Test public void testCustomAnnotationOptionalFieldResourceInjection() { - 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( CustomAnnotationOptionalFieldResourceInjectionBean.class)); TestBean tb = new TestBean(); @@ -1724,18 +1422,13 @@ public class AutowiredAnnotationBeanPostProcessorTests { assertSame(tb, bean.getTestBean3()); assertNull(bean.getTestBean()); assertNull(bean.getTestBean2()); - bf.destroySingletons(); } @Test public void testCustomAnnotationOptionalFieldResourceInjectionWhenNoDependencyFound() { - 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( CustomAnnotationOptionalFieldResourceInjectionBean.class)); @@ -1744,18 +1437,13 @@ public class AutowiredAnnotationBeanPostProcessorTests { assertNull(bean.getTestBean3()); assertNull(bean.getTestBean()); assertNull(bean.getTestBean2()); - bf.destroySingletons(); } @Test public void testCustomAnnotationOptionalFieldResourceInjectionWhenMultipleDependenciesFound() { - 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( CustomAnnotationOptionalFieldResourceInjectionBean.class)); TestBean tb1 = new TestBean(); @@ -1772,18 +1460,13 @@ public class AutowiredAnnotationBeanPostProcessorTests { assertSame(CustomAnnotationOptionalFieldResourceInjectionBean.class, ex.getInjectionPoint().getField().getDeclaringClass()); } - bf.destroySingletons(); } @Test public void testCustomAnnotationOptionalMethodResourceInjection() { - 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( CustomAnnotationOptionalMethodResourceInjectionBean.class)); TestBean tb = new TestBean(); @@ -1794,18 +1477,13 @@ public class AutowiredAnnotationBeanPostProcessorTests { assertSame(tb, bean.getTestBean3()); assertNull(bean.getTestBean()); assertNull(bean.getTestBean2()); - bf.destroySingletons(); } @Test public void testCustomAnnotationOptionalMethodResourceInjectionWhenNoDependencyFound() { - 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( CustomAnnotationOptionalMethodResourceInjectionBean.class)); @@ -1814,18 +1492,13 @@ public class AutowiredAnnotationBeanPostProcessorTests { assertNull(bean.getTestBean3()); assertNull(bean.getTestBean()); assertNull(bean.getTestBean2()); - bf.destroySingletons(); } @Test public void testCustomAnnotationOptionalMethodResourceInjectionWhenMultipleDependenciesFound() { - 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( CustomAnnotationOptionalMethodResourceInjectionBean.class)); TestBean tb1 = new TestBean(); @@ -1842,7 +1515,6 @@ public class AutowiredAnnotationBeanPostProcessorTests { assertSame(CustomAnnotationOptionalMethodResourceInjectionBean.class, ex.getInjectionPoint().getMethodParameter().getDeclaringClass()); } - bf.destroySingletons(); } /** @@ -1854,10 +1526,6 @@ public class AutowiredAnnotationBeanPostProcessorTests { */ @Test public void testBeanAutowiredWithFactoryBean() { - DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); - AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor(); - bpp.setBeanFactory(bf); - bf.addBeanPostProcessor(bpp); bf.registerBeanDefinition("factoryBeanDependentBean", new RootBeanDefinition(FactoryBeanDependentBean.class)); bf.registerSingleton("stringFactoryBean", new StringFactoryBean()); @@ -1868,17 +1536,10 @@ public class AutowiredAnnotationBeanPostProcessorTests { assertNotNull("The factoryBeanDependentBean should have been registered.", bean); assertEquals("The FactoryBeanDependentBean should have been autowired 'by type' with the StringFactoryBean.", factoryBean, bean.getFactoryBean()); - - bf.destroySingletons(); } @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); @@ -1924,11 +1585,6 @@ public class AutowiredAnnotationBeanPostProcessorTests { @Test public void testGenericsBasedFieldInjectionWithSubstitutedVariables() { - DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); - bf.setAutowireCandidateResolver(new QualifierAnnotationAutowireCandidateResolver()); - AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor(); - bpp.setBeanFactory(bf); - bf.addBeanPostProcessor(bpp); RootBeanDefinition bd = new RootBeanDefinition(RepositoryFieldInjectionBeanWithSubstitutedVariables.class); bd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE); bf.registerBeanDefinition("annotatedBean", bd); @@ -1974,11 +1630,6 @@ public class AutowiredAnnotationBeanPostProcessorTests { @Test public void testGenericsBasedFieldInjectionWithQualifiers() { - DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); - bf.setAutowireCandidateResolver(new QualifierAnnotationAutowireCandidateResolver()); - AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor(); - bpp.setBeanFactory(bf); - bf.addBeanPostProcessor(bpp); RootBeanDefinition bd = new RootBeanDefinition(RepositoryFieldInjectionBeanWithQualifiers.class); bd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE); bf.registerBeanDefinition("annotatedBean", bd); @@ -2006,11 +1657,6 @@ public class AutowiredAnnotationBeanPostProcessorTests { @Test public void testGenericsBasedFieldInjectionWithMocks() { - DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); - bf.setAutowireCandidateResolver(new QualifierAnnotationAutowireCandidateResolver()); - AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor(); - bpp.setBeanFactory(bf); - bf.addBeanPostProcessor(bpp); RootBeanDefinition bd = new RootBeanDefinition(RepositoryFieldInjectionBeanWithQualifiers.class); bd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE); bf.registerBeanDefinition("annotatedBean", bd); @@ -2050,11 +1696,6 @@ public class AutowiredAnnotationBeanPostProcessorTests { @Test public void testGenericsBasedFieldInjectionWithSimpleMatch() { - DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); - bf.setAutowireCandidateResolver(new QualifierAnnotationAutowireCandidateResolver()); - AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor(); - bpp.setBeanFactory(bf); - bf.addBeanPostProcessor(bpp); RootBeanDefinition bd = new RootBeanDefinition(RepositoryFieldInjectionBeanWithSimpleMatch.class); bd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE); bf.registerBeanDefinition("annotatedBean", bd); @@ -2083,11 +1724,6 @@ public class AutowiredAnnotationBeanPostProcessorTests { @Test public void testGenericsBasedFactoryBeanInjectionWithBeanDefinition() { - 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); @@ -2100,11 +1736,6 @@ public class AutowiredAnnotationBeanPostProcessorTests { @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); @@ -2117,11 +1748,6 @@ public class AutowiredAnnotationBeanPostProcessorTests { @Test public void testGenericsBasedFieldInjectionWithSimpleMatchAndMock() { - DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); - bf.setAutowireCandidateResolver(new QualifierAnnotationAutowireCandidateResolver()); - AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor(); - bpp.setBeanFactory(bf); - bf.addBeanPostProcessor(bpp); RootBeanDefinition bd = new RootBeanDefinition(RepositoryFieldInjectionBeanWithSimpleMatch.class); bd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE); bf.registerBeanDefinition("annotatedBean", bd); @@ -2154,11 +1780,6 @@ public class AutowiredAnnotationBeanPostProcessorTests { @Test public void testGenericsBasedFieldInjectionWithSimpleMatchAndMockito() { - DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); - bf.setAutowireCandidateResolver(new QualifierAnnotationAutowireCandidateResolver()); - AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor(); - bpp.setBeanFactory(bf); - bf.addBeanPostProcessor(bpp); RootBeanDefinition bd = new RootBeanDefinition(RepositoryFieldInjectionBeanWithSimpleMatch.class); bd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE); bf.registerBeanDefinition("annotatedBean", bd); @@ -2190,11 +1811,6 @@ public class AutowiredAnnotationBeanPostProcessorTests { @Test public void testGenericsBasedMethodInjection() { - DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); - bf.setAutowireCandidateResolver(new QualifierAnnotationAutowireCandidateResolver()); - AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor(); - bpp.setBeanFactory(bf); - bf.addBeanPostProcessor(bpp); RootBeanDefinition bd = new RootBeanDefinition(RepositoryMethodInjectionBean.class); bd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE); bf.registerBeanDefinition("annotatedBean", bd); @@ -2240,11 +1856,6 @@ public class AutowiredAnnotationBeanPostProcessorTests { @Test public void testGenericsBasedMethodInjectionWithSubstitutedVariables() { - DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); - bf.setAutowireCandidateResolver(new QualifierAnnotationAutowireCandidateResolver()); - AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor(); - bpp.setBeanFactory(bf); - bf.addBeanPostProcessor(bpp); RootBeanDefinition bd = new RootBeanDefinition(RepositoryMethodInjectionBeanWithSubstitutedVariables.class); bd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE); bf.registerBeanDefinition("annotatedBean", bd); @@ -2290,11 +1901,6 @@ public class AutowiredAnnotationBeanPostProcessorTests { @Test public void testGenericsBasedConstructorInjection() { - DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); - bf.setAutowireCandidateResolver(new QualifierAnnotationAutowireCandidateResolver()); - AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor(); - bpp.setBeanFactory(bf); - bf.addBeanPostProcessor(bpp); RootBeanDefinition bd = new RootBeanDefinition(RepositoryConstructorInjectionBean.class); bd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE); bf.registerBeanDefinition("annotatedBean", bd); @@ -2323,11 +1929,6 @@ public class AutowiredAnnotationBeanPostProcessorTests { @Test @SuppressWarnings("rawtypes") public void testGenericsBasedConstructorInjectionWithNonTypedTarget() { - DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); - bf.setAutowireCandidateResolver(new QualifierAnnotationAutowireCandidateResolver()); - AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor(); - bpp.setBeanFactory(bf); - bf.addBeanPostProcessor(bpp); RootBeanDefinition bd = new RootBeanDefinition(RepositoryConstructorInjectionBean.class); bd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE); bf.registerBeanDefinition("annotatedBean", bd); @@ -2353,11 +1954,6 @@ public class AutowiredAnnotationBeanPostProcessorTests { @Test public void testGenericsBasedConstructorInjectionWithNonGenericTarget() { - DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); - bf.setAutowireCandidateResolver(new QualifierAnnotationAutowireCandidateResolver()); - AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor(); - bpp.setBeanFactory(bf); - bf.addBeanPostProcessor(bpp); RootBeanDefinition bd = new RootBeanDefinition(RepositoryConstructorInjectionBean.class); bd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE); bf.registerBeanDefinition("annotatedBean", bd); @@ -2384,11 +1980,6 @@ public class AutowiredAnnotationBeanPostProcessorTests { @Test @SuppressWarnings("rawtypes") public void testGenericsBasedConstructorInjectionWithMixedTargets() { - DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); - bf.setAutowireCandidateResolver(new QualifierAnnotationAutowireCandidateResolver()); - AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor(); - bpp.setBeanFactory(bf); - bf.addBeanPostProcessor(bpp); RootBeanDefinition bd = new RootBeanDefinition(RepositoryConstructorInjectionBean.class); bd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE); bf.registerBeanDefinition("annotatedBean", bd); @@ -2416,11 +2007,6 @@ public class AutowiredAnnotationBeanPostProcessorTests { @Test public void testGenericsBasedConstructorInjectionWithMixedTargetsIncludingNonGeneric() { - DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); - bf.setAutowireCandidateResolver(new QualifierAnnotationAutowireCandidateResolver()); - AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor(); - bpp.setBeanFactory(bf); - bf.addBeanPostProcessor(bpp); RootBeanDefinition bd = new RootBeanDefinition(RepositoryConstructorInjectionBean.class); bd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE); bf.registerBeanDefinition("annotatedBean", bd); @@ -2449,11 +2035,6 @@ public class AutowiredAnnotationBeanPostProcessorTests { @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); @@ -2468,11 +2049,6 @@ public class AutowiredAnnotationBeanPostProcessorTests { @Test @SuppressWarnings("rawtypes") public void testGenericsBasedInjectionIntoUnresolvedTypeVariable() { - 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("createPlain"); bf.registerBeanDefinition("bean1", bd); @@ -2487,11 +2063,6 @@ public class AutowiredAnnotationBeanPostProcessorTests { @Test @SuppressWarnings("rawtypes") public void testGenericsBasedInjectionIntoTypeVariableSelectingBestMatch() { - 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); @@ -2510,11 +2081,6 @@ public class AutowiredAnnotationBeanPostProcessorTests { @Ignore // SPR-11521 @SuppressWarnings("rawtypes") public void testGenericsBasedInjectionIntoTypeVariableSelectingBestMatchAgainstFactoryMethodSignature() { - 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("createErased"); bf.registerBeanDefinition("bean1", bd); @@ -2529,11 +2095,6 @@ public class AutowiredAnnotationBeanPostProcessorTests { @Test public void testGenericsBasedInjectionWithBeanDefinitionTargetResolvableType() { - DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); - bf.setAutowireCandidateResolver(new QualifierAnnotationAutowireCandidateResolver()); - AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor(); - bpp.setBeanFactory(bf); - bf.addBeanPostProcessor(bpp); RootBeanDefinition bd1 = new RootBeanDefinition(GenericInterface2Bean.class); bd1.setTargetType(ResolvableType.forClassWithGenerics(GenericInterface2Bean.class, String.class)); bf.registerBeanDefinition("bean1", bd1); @@ -2549,11 +2110,6 @@ public class AutowiredAnnotationBeanPostProcessorTests { @Test public void testCircularTypeReference() { - DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); - bf.setAutowireCandidateResolver(new QualifierAnnotationAutowireCandidateResolver()); - AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor(); - bpp.setBeanFactory(bf); - bf.addBeanPostProcessor(bpp); bf.registerBeanDefinition("bean1", new RootBeanDefinition(StockServiceImpl.class)); bf.registerBeanDefinition("bean2", new RootBeanDefinition(StockMovementDaoImpl.class)); bf.registerBeanDefinition("bean3", new RootBeanDefinition(StockMovementImpl.class)); @@ -2565,10 +2121,6 @@ public class AutowiredAnnotationBeanPostProcessorTests { @Test public void testBridgeMethodHandling() { - DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); - AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor(); - bpp.setBeanFactory(bf); - bf.addBeanPostProcessor(bpp); bf.registerBeanDefinition("bean1", new RootBeanDefinition(MyCallable.class)); bf.registerBeanDefinition("bean2", new RootBeanDefinition(SecondCallable.class)); bf.registerBeanDefinition("bean3", new RootBeanDefinition(FooBar.class)); @@ -2577,10 +2129,6 @@ public class AutowiredAnnotationBeanPostProcessorTests { @Test public void testSingleConstructorWithProvidedArgument() { - DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); - AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor(); - bpp.setBeanFactory(bf); - bf.addBeanPostProcessor(bpp); RootBeanDefinition bd = new RootBeanDefinition(ProvidedArgumentBean.class); bd.getConstructorArgumentValues().addGenericArgumentValue(Collections.singletonList("value")); bf.registerBeanDefinition("beanWithArgs", bd); @@ -2589,8 +2137,6 @@ public class AutowiredAnnotationBeanPostProcessorTests { @Test public void testAnnotatedDefaultConstructor() { - DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); - bf.addBeanPostProcessor(new AutowiredAnnotationBeanPostProcessor()); bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(AnnotatedDefaultConstructorBean.class)); assertNotNull(bf.getBean("annotatedBean")); @@ -2598,10 +2144,6 @@ public class AutowiredAnnotationBeanPostProcessorTests { @Test // SPR-15125 public void testFactoryBeanSelfInjection() { - DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); - AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor(); - bpp.setBeanFactory(bf); - bf.addBeanPostProcessor(bpp); bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(SelfInjectingFactoryBean.class)); SelfInjectingFactoryBean bean = bf.getBean(SelfInjectingFactoryBean.class); @@ -2610,10 +2152,6 @@ public class AutowiredAnnotationBeanPostProcessorTests { @Test // SPR-15125 public void testFactoryBeanSelfInjectionViaFactoryMethod() { - DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); - AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor(); - bpp.setBeanFactory(bf); - bf.addBeanPostProcessor(bpp); RootBeanDefinition bd = new RootBeanDefinition(SelfInjectingFactoryBean.class); bd.setFactoryMethodName("create"); bf.registerBeanDefinition("annotatedBean", bd); @@ -3041,6 +2579,27 @@ public class AutowiredAnnotationBeanPostProcessorTests { } + public static class SingleConstructorVarargBean { + + private ITestBean testBean; + + private List nestedTestBeans; + + public SingleConstructorVarargBean(ITestBean testBean, NestedTestBean... nestedTestBeans) { + this.testBean = testBean; + this.nestedTestBeans = Arrays.asList(nestedTestBeans); + } + + public ITestBean getTestBean() { + return this.testBean; + } + + public List getNestedTestBeans() { + return this.nestedTestBeans; + } + } + + public static class SingleConstructorRequiredCollectionBean { private ITestBean testBean; diff --git a/spring-beans/src/test/java/org/springframework/beans/factory/annotation/InjectAnnotationBeanPostProcessorTests.java b/spring-beans/src/test/java/org/springframework/beans/factory/annotation/InjectAnnotationBeanPostProcessorTests.java index cc56cccb54..8c2fa870a0 100644 --- a/spring-beans/src/test/java/org/springframework/beans/factory/annotation/InjectAnnotationBeanPostProcessorTests.java +++ b/spring-beans/src/test/java/org/springframework/beans/factory/annotation/InjectAnnotationBeanPostProcessorTests.java @@ -26,6 +26,8 @@ import javax.inject.Inject; import javax.inject.Named; import javax.inject.Provider; +import org.junit.After; +import org.junit.Before; import org.junit.Test; import org.springframework.beans.factory.BeanCreationException; @@ -54,12 +56,29 @@ import static org.junit.Assert.*; */ public class InjectAnnotationBeanPostProcessorTests { - @Test - public void testIncompleteBeanDefinition() { - DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); - AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor(); + private DefaultListableBeanFactory bf; + + private AutowiredAnnotationBeanPostProcessor bpp; + + + @Before + public void setup() { + bf = new DefaultListableBeanFactory(); + bf.registerResolvableDependency(BeanFactory.class, bf); + bpp = new AutowiredAnnotationBeanPostProcessor(); bpp.setBeanFactory(bf); bf.addBeanPostProcessor(bpp); + bf.setAutowireCandidateResolver(new QualifierAnnotationAutowireCandidateResolver()); + } + + @After + public void close() { + bf.destroySingletons(); + } + + + @Test + public void testIncompleteBeanDefinition() { bf.registerBeanDefinition("testBean", new GenericBeanDefinition()); try { bf.getBean("testBean"); @@ -71,10 +90,6 @@ public class InjectAnnotationBeanPostProcessorTests { @Test public void testResourceInjection() { - DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); - AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor(); - bpp.setBeanFactory(bf); - bf.addBeanPostProcessor(bpp); RootBeanDefinition bd = new RootBeanDefinition(ResourceInjectionBean.class); bd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE); bf.registerBeanDefinition("annotatedBean", bd); @@ -92,11 +107,6 @@ public class InjectAnnotationBeanPostProcessorTests { @Test public void testExtendedResourceInjection() { - DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); - bf.registerResolvableDependency(BeanFactory.class, bf); - AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor(); - bpp.setBeanFactory(bf); - bf.addBeanPostProcessor(bpp); RootBeanDefinition bd = new RootBeanDefinition(TypedExtendedResourceInjectionBean.class); bd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE); bf.registerBeanDefinition("annotatedBean", bd); @@ -124,11 +134,6 @@ public class InjectAnnotationBeanPostProcessorTests { @Test public void testExtendedResourceInjectionWithOverriding() { - DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); - bf.registerResolvableDependency(BeanFactory.class, bf); - AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor(); - bpp.setBeanFactory(bf); - bf.addBeanPostProcessor(bpp); RootBeanDefinition annotatedBd = new RootBeanDefinition(TypedExtendedResourceInjectionBean.class); TestBean tb2 = new TestBean(); annotatedBd.getPropertyValues().add("testBean2", tb2); @@ -145,16 +150,10 @@ public class InjectAnnotationBeanPostProcessorTests { assertSame(tb, bean.getTestBean4()); assertSame(ntb, bean.getNestedTestBean()); assertSame(bf, bean.getBeanFactory()); - bf.destroySingletons(); } @Test public void testExtendedResourceInjectionWithAtRequired() { - DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); - bf.registerResolvableDependency(BeanFactory.class, bf); - AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor(); - bpp.setBeanFactory(bf); - bf.addBeanPostProcessor(bpp); bf.addBeanPostProcessor(new RequiredAnnotationBeanPostProcessor()); RootBeanDefinition bd = new RootBeanDefinition(TypedExtendedResourceInjectionBean.class); bd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE); @@ -175,11 +174,6 @@ public class InjectAnnotationBeanPostProcessorTests { @Test public void testConstructorResourceInjection() { - DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); - bf.registerResolvableDependency(BeanFactory.class, bf); - AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor(); - bpp.setBeanFactory(bf); - bf.addBeanPostProcessor(bpp); RootBeanDefinition bd = new RootBeanDefinition(ConstructorResourceInjectionBean.class); bd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE); bf.registerBeanDefinition("annotatedBean", bd); @@ -207,10 +201,6 @@ public class InjectAnnotationBeanPostProcessorTests { @Test public void testConstructorResourceInjectionWithMultipleCandidatesAsCollection() { - DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); - AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor(); - bpp.setBeanFactory(bf); - bf.addBeanPostProcessor(bpp); bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(ConstructorsCollectionResourceInjectionBean.class)); TestBean tb = new TestBean(); @@ -226,15 +216,10 @@ public class InjectAnnotationBeanPostProcessorTests { assertEquals(2, bean.getNestedTestBeans().size()); assertSame(ntb1, bean.getNestedTestBeans().get(0)); assertSame(ntb2, bean.getNestedTestBeans().get(1)); - bf.destroySingletons(); } @Test public void testConstructorResourceInjectionWithMultipleCandidatesAndFallback() { - DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); - AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor(); - bpp.setBeanFactory(bf); - bf.addBeanPostProcessor(bpp); bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(ConstructorsResourceInjectionBean.class)); TestBean tb = new TestBean(); bf.registerSingleton("testBean", tb); @@ -242,15 +227,10 @@ public class InjectAnnotationBeanPostProcessorTests { ConstructorsResourceInjectionBean bean = (ConstructorsResourceInjectionBean) bf.getBean("annotatedBean"); assertSame(tb, bean.getTestBean3()); assertNull(bean.getTestBean4()); - bf.destroySingletons(); } @Test public void testConstructorInjectionWithMap() { - DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); - AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor(); - bpp.setBeanFactory(bf); - bf.addBeanPostProcessor(bpp); RootBeanDefinition bd = new RootBeanDefinition(MapConstructorInjectionBean.class); bd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE); bf.registerBeanDefinition("annotatedBean", bd); @@ -276,10 +256,6 @@ public class InjectAnnotationBeanPostProcessorTests { @Test public void testFieldInjectionWithMap() { - DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); - AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor(); - bpp.setBeanFactory(bf); - bf.addBeanPostProcessor(bpp); RootBeanDefinition bd = new RootBeanDefinition(MapFieldInjectionBean.class); bd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE); bf.registerBeanDefinition("annotatedBean", bd); @@ -305,10 +281,6 @@ public class InjectAnnotationBeanPostProcessorTests { @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); @@ -330,10 +302,6 @@ public class InjectAnnotationBeanPostProcessorTests { @Test public void testMethodInjectionWithMapAndMultipleMatches() { - 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)); bf.registerBeanDefinition("testBean2", new RootBeanDefinition(TestBean.class)); @@ -345,15 +313,10 @@ public class InjectAnnotationBeanPostProcessorTests { catch (BeanCreationException e) { // expected } - bf.destroySingletons(); } @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); @@ -366,16 +329,10 @@ public class InjectAnnotationBeanPostProcessorTests { assertTrue(bean.getTestBeanMap().keySet().contains("testBean1")); assertTrue(bean.getTestBeanMap().values().contains(tb)); assertSame(tb, bean.getTestBean()); - bf.destroySingletons(); } @Test public void testObjectFactoryInjection() { - DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); - bf.setAutowireCandidateResolver(new QualifierAnnotationAutowireCandidateResolver()); - AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor(); - bpp.setBeanFactory(bf); - bf.addBeanPostProcessor(bpp); bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(ObjectFactoryQualifierFieldInjectionBean.class)); RootBeanDefinition bd = new RootBeanDefinition(TestBean.class); bd.addQualifier(new AutowireCandidateQualifier(Qualifier.class, "testBean")); @@ -384,15 +341,10 @@ public class InjectAnnotationBeanPostProcessorTests { ObjectFactoryQualifierFieldInjectionBean bean = (ObjectFactoryQualifierFieldInjectionBean) bf.getBean("annotatedBean"); assertSame(bf.getBean("testBean"), bean.getTestBean()); - bf.destroySingletons(); } @Test public void testObjectFactoryQualifierInjection() { - DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); - AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor(); - bpp.setBeanFactory(bf); - bf.addBeanPostProcessor(bpp); bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(ObjectFactoryQualifierFieldInjectionBean.class)); RootBeanDefinition bd = new RootBeanDefinition(TestBean.class); bd.addQualifier(new AutowireCandidateQualifier(Qualifier.class, "testBean")); @@ -400,16 +352,10 @@ public class InjectAnnotationBeanPostProcessorTests { ObjectFactoryQualifierFieldInjectionBean bean = (ObjectFactoryQualifierFieldInjectionBean) bf.getBean("annotatedBean"); assertSame(bf.getBean("testBean"), bean.getTestBean()); - bf.destroySingletons(); } @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); @@ -427,11 +373,6 @@ public class InjectAnnotationBeanPostProcessorTests { @Test public void testObjectFactoryMethodInjectionIntoPrototypeBean() { - DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); - bf.setAutowireCandidateResolver(new QualifierAnnotationAutowireCandidateResolver()); - AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor(); - bpp.setBeanFactory(bf); - bf.addBeanPostProcessor(bpp); RootBeanDefinition annotatedBeanDefinition = new RootBeanDefinition(ObjectFactoryQualifierMethodInjectionBean.class); annotatedBeanDefinition.setScope(BeanDefinition.SCOPE_PROTOTYPE); bf.registerBeanDefinition("annotatedBean", annotatedBeanDefinition); @@ -449,10 +390,6 @@ public class InjectAnnotationBeanPostProcessorTests { @Test public void testObjectFactoryWithBeanField() throws Exception { - DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); - AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor(); - bpp.setBeanFactory(bf); - bf.addBeanPostProcessor(bpp); bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(ObjectFactoryFieldInjectionBean.class)); bf.registerBeanDefinition("testBean", new RootBeanDefinition(TestBean.class)); bf.setSerializationId("test"); @@ -461,15 +398,10 @@ public class InjectAnnotationBeanPostProcessorTests { assertSame(bf.getBean("testBean"), bean.getTestBean()); bean = (ObjectFactoryFieldInjectionBean) SerializationTestUtils.serializeAndDeserialize(bean); assertSame(bf.getBean("testBean"), bean.getTestBean()); - bf.destroySingletons(); } @Test public void testObjectFactoryWithBeanMethod() throws Exception { - DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); - AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor(); - bpp.setBeanFactory(bf); - bf.addBeanPostProcessor(bpp); bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(ObjectFactoryMethodInjectionBean.class)); bf.registerBeanDefinition("testBean", new RootBeanDefinition(TestBean.class)); bf.setSerializationId("test"); @@ -478,15 +410,10 @@ public class InjectAnnotationBeanPostProcessorTests { assertSame(bf.getBean("testBean"), bean.getTestBean()); bean = (ObjectFactoryMethodInjectionBean) SerializationTestUtils.serializeAndDeserialize(bean); assertSame(bf.getBean("testBean"), bean.getTestBean()); - bf.destroySingletons(); } @Test public void testObjectFactoryWithTypedListField() throws Exception { - DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); - AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor(); - bpp.setBeanFactory(bf); - bf.addBeanPostProcessor(bpp); bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(ObjectFactoryListFieldInjectionBean.class)); bf.registerBeanDefinition("testBean", new RootBeanDefinition(TestBean.class)); bf.setSerializationId("test"); @@ -495,15 +422,10 @@ public class InjectAnnotationBeanPostProcessorTests { assertSame(bf.getBean("testBean"), bean.getTestBean()); bean = (ObjectFactoryListFieldInjectionBean) SerializationTestUtils.serializeAndDeserialize(bean); assertSame(bf.getBean("testBean"), bean.getTestBean()); - bf.destroySingletons(); } @Test public void testObjectFactoryWithTypedListMethod() throws Exception { - DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); - AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor(); - bpp.setBeanFactory(bf); - bf.addBeanPostProcessor(bpp); bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(ObjectFactoryListMethodInjectionBean.class)); bf.registerBeanDefinition("testBean", new RootBeanDefinition(TestBean.class)); bf.setSerializationId("test"); @@ -512,15 +434,10 @@ public class InjectAnnotationBeanPostProcessorTests { assertSame(bf.getBean("testBean"), bean.getTestBean()); bean = (ObjectFactoryListMethodInjectionBean) SerializationTestUtils.serializeAndDeserialize(bean); assertSame(bf.getBean("testBean"), bean.getTestBean()); - bf.destroySingletons(); } @Test public void testObjectFactoryWithTypedMapField() throws Exception { - DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); - AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor(); - bpp.setBeanFactory(bf); - bf.addBeanPostProcessor(bpp); bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(ObjectFactoryMapFieldInjectionBean.class)); bf.registerBeanDefinition("testBean", new RootBeanDefinition(TestBean.class)); bf.setSerializationId("test"); @@ -529,15 +446,10 @@ public class InjectAnnotationBeanPostProcessorTests { assertSame(bf.getBean("testBean"), bean.getTestBean()); bean = (ObjectFactoryMapFieldInjectionBean) SerializationTestUtils.serializeAndDeserialize(bean); assertSame(bf.getBean("testBean"), bean.getTestBean()); - bf.destroySingletons(); } @Test public void testObjectFactoryWithTypedMapMethod() throws Exception { - DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); - AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor(); - bpp.setBeanFactory(bf); - bf.addBeanPostProcessor(bpp); bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(ObjectFactoryMapMethodInjectionBean.class)); bf.registerBeanDefinition("testBean", new RootBeanDefinition(TestBean.class)); bf.setSerializationId("test"); @@ -546,7 +458,6 @@ public class InjectAnnotationBeanPostProcessorTests { assertSame(bf.getBean("testBean"), bean.getTestBean()); bean = (ObjectFactoryMapMethodInjectionBean) SerializationTestUtils.serializeAndDeserialize(bean); assertSame(bf.getBean("testBean"), bean.getTestBean()); - bf.destroySingletons(); } /** @@ -556,10 +467,6 @@ public class InjectAnnotationBeanPostProcessorTests { */ @Test public void testBeanAutowiredWithFactoryBean() { - DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); - AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor(); - bpp.setBeanFactory(bf); - bf.addBeanPostProcessor(bpp); bf.registerBeanDefinition("factoryBeanDependentBean", new RootBeanDefinition(FactoryBeanDependentBean.class)); bf.registerSingleton("stringFactoryBean", new StringFactoryBean()); @@ -570,236 +477,152 @@ public class InjectAnnotationBeanPostProcessorTests { assertNotNull("The factoryBeanDependentBean should have been registered.", bean); assertEquals("The FactoryBeanDependentBean should have been autowired 'by type' with the StringFactoryBean.", factoryBean, bean.getFactoryBean()); - - bf.destroySingletons(); } @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(); } @Test public void testNullableFieldInjectionWithBeanNotAvailable() { - DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); - AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor(); - bpp.setBeanFactory(bf); - bf.addBeanPostProcessor(bpp); bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(NullableFieldInjectionBean.class)); NullableFieldInjectionBean bean = (NullableFieldInjectionBean) bf.getBean("annotatedBean"); assertNull(bean.getTestBean()); - bf.destroySingletons(); } @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(); } @Test public void testNullableMethodInjectionWithBeanNotAvailable() { - DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); - AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor(); - bpp.setBeanFactory(bf); - bf.addBeanPostProcessor(bpp); bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(NullableMethodInjectionBean.class)); NullableMethodInjectionBean bean = (NullableMethodInjectionBean) bf.getBean("annotatedBean"); assertNull(bean.getTestBean()); - bf.destroySingletons(); } @Test public void testOptionalFieldInjectionWithBeanAvailable() { - DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); - AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor(); - bpp.setBeanFactory(bf); - bf.addBeanPostProcessor(bpp); bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(OptionalFieldInjectionBean.class)); bf.registerBeanDefinition("testBean", new RootBeanDefinition(TestBean.class)); OptionalFieldInjectionBean bean = (OptionalFieldInjectionBean) bf.getBean("annotatedBean"); assertTrue(bean.getTestBean().isPresent()); assertSame(bf.getBean("testBean"), bean.getTestBean().get()); - bf.destroySingletons(); } @Test public void testOptionalFieldInjectionWithBeanNotAvailable() { - DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); - AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor(); - bpp.setBeanFactory(bf); - bf.addBeanPostProcessor(bpp); bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(OptionalFieldInjectionBean.class)); OptionalFieldInjectionBean bean = (OptionalFieldInjectionBean) bf.getBean("annotatedBean"); assertFalse(bean.getTestBean().isPresent()); - bf.destroySingletons(); } @Test public void testOptionalMethodInjectionWithBeanAvailable() { - DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); - AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor(); - bpp.setBeanFactory(bf); - bf.addBeanPostProcessor(bpp); bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(OptionalMethodInjectionBean.class)); bf.registerBeanDefinition("testBean", new RootBeanDefinition(TestBean.class)); OptionalMethodInjectionBean bean = (OptionalMethodInjectionBean) bf.getBean("annotatedBean"); assertTrue(bean.getTestBean().isPresent()); assertSame(bf.getBean("testBean"), bean.getTestBean().get()); - bf.destroySingletons(); } @Test public void testOptionalMethodInjectionWithBeanNotAvailable() { - DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); - AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor(); - bpp.setBeanFactory(bf); - bf.addBeanPostProcessor(bpp); bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(OptionalMethodInjectionBean.class)); OptionalMethodInjectionBean bean = (OptionalMethodInjectionBean) bf.getBean("annotatedBean"); assertFalse(bean.getTestBean().isPresent()); - bf.destroySingletons(); } @Test public void testOptionalListFieldInjectionWithBeanAvailable() { - DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); - AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor(); - bpp.setBeanFactory(bf); - bf.addBeanPostProcessor(bpp); bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(OptionalListFieldInjectionBean.class)); bf.registerBeanDefinition("testBean", new RootBeanDefinition(TestBean.class)); OptionalListFieldInjectionBean bean = (OptionalListFieldInjectionBean) bf.getBean("annotatedBean"); assertTrue(bean.getTestBean().isPresent()); assertSame(bf.getBean("testBean"), bean.getTestBean().get().get(0)); - bf.destroySingletons(); } @Test public void testOptionalListFieldInjectionWithBeanNotAvailable() { - DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); - AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor(); - bpp.setBeanFactory(bf); - bf.addBeanPostProcessor(bpp); bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(OptionalListFieldInjectionBean.class)); OptionalListFieldInjectionBean bean = (OptionalListFieldInjectionBean) bf.getBean("annotatedBean"); assertFalse(bean.getTestBean().isPresent()); - bf.destroySingletons(); } @Test public void testOptionalListMethodInjectionWithBeanAvailable() { - DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); - AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor(); - bpp.setBeanFactory(bf); - bf.addBeanPostProcessor(bpp); bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(OptionalListMethodInjectionBean.class)); bf.registerBeanDefinition("testBean", new RootBeanDefinition(TestBean.class)); OptionalListMethodInjectionBean bean = (OptionalListMethodInjectionBean) bf.getBean("annotatedBean"); assertTrue(bean.getTestBean().isPresent()); assertSame(bf.getBean("testBean"), bean.getTestBean().get().get(0)); - bf.destroySingletons(); } @Test public void testOptionalListMethodInjectionWithBeanNotAvailable() { - DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); - AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor(); - bpp.setBeanFactory(bf); - bf.addBeanPostProcessor(bpp); bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(OptionalListMethodInjectionBean.class)); OptionalListMethodInjectionBean bean = (OptionalListMethodInjectionBean) bf.getBean("annotatedBean"); assertFalse(bean.getTestBean().isPresent()); - bf.destroySingletons(); } @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(); } @Test public void testProviderOfOptionalFieldInjectionWithBeanNotAvailable() { - DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); - AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor(); - bpp.setBeanFactory(bf); - bf.addBeanPostProcessor(bpp); bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(ProviderOfOptionalFieldInjectionBean.class)); ProviderOfOptionalFieldInjectionBean bean = (ProviderOfOptionalFieldInjectionBean) bf.getBean("annotatedBean"); assertFalse(bean.getTestBean().isPresent()); - bf.destroySingletons(); } @Test public void testProviderOfOptionalMethodInjectionWithBeanAvailable() { - DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); - AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor(); - bpp.setBeanFactory(bf); - bf.addBeanPostProcessor(bpp); bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(ProviderOfOptionalMethodInjectionBean.class)); bf.registerBeanDefinition("testBean", new RootBeanDefinition(TestBean.class)); ProviderOfOptionalMethodInjectionBean bean = (ProviderOfOptionalMethodInjectionBean) bf.getBean("annotatedBean"); assertTrue(bean.getTestBean().isPresent()); assertSame(bf.getBean("testBean"), bean.getTestBean().get()); - bf.destroySingletons(); } @Test public void testProviderOfOptionalMethodInjectionWithBeanNotAvailable() { - DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); - AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor(); - bpp.setBeanFactory(bf); - bf.addBeanPostProcessor(bpp); bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(ProviderOfOptionalMethodInjectionBean.class)); ProviderOfOptionalMethodInjectionBean bean = (ProviderOfOptionalMethodInjectionBean) bf.getBean("annotatedBean"); assertFalse(bean.getTestBean().isPresent()); - bf.destroySingletons(); } @Test public void testAnnotatedDefaultConstructor() { - DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); - bf.addBeanPostProcessor(new AutowiredAnnotationBeanPostProcessor()); bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(AnnotatedDefaultConstructorBean.class)); assertNotNull(bf.getBean("annotatedBean")); diff --git a/spring-context/src/test/java/org/springframework/context/annotation/ConfigurationClassPostProcessorTests.java b/spring-context/src/test/java/org/springframework/context/annotation/ConfigurationClassPostProcessorTests.java index 6d831b48fd..b38f436b6b 100644 --- a/spring-context/src/test/java/org/springframework/context/annotation/ConfigurationClassPostProcessorTests.java +++ b/spring-context/src/test/java/org/springframework/context/annotation/ConfigurationClassPostProcessorTests.java @@ -801,6 +801,23 @@ public class ConfigurationClassPostProcessorTests { assertSame(ctx.getBean(BarImpl.class), ctx.getBean(FooImpl.class).bar); } + @Test + public void testVarargOnBeanMethod() { + ApplicationContext ctx = new AnnotationConfigApplicationContext(VarargConfiguration.class, TestBean.class); + VarargConfiguration bean = ctx.getBean(VarargConfiguration.class); + assertNotNull(bean.testBeans); + assertEquals(1, bean.testBeans.length); + assertSame(ctx.getBean(TestBean.class), bean.testBeans[0]); + } + + @Test + public void testEmptyVarargOnBeanMethod() { + ApplicationContext ctx = new AnnotationConfigApplicationContext(VarargConfiguration.class); + VarargConfiguration bean = ctx.getBean(VarargConfiguration.class); + assertNotNull(bean.testBeans); + assertEquals(0, bean.testBeans.length); + } + @Test public void testCollectionArgumentOnBeanMethod() { ApplicationContext ctx = new AnnotationConfigApplicationContext(CollectionArgumentConfiguration.class, TestBean.class); @@ -1606,6 +1623,18 @@ public class ConfigurationClassPostProcessorTests { } } + @Configuration + static class VarargConfiguration { + + TestBean[] testBeans; + + @Bean(autowireCandidate = false) + public TestBean thing(TestBean... testBeans) { + this.testBeans = testBeans; + return new TestBean(); + } + } + @Configuration static class CollectionArgumentConfiguration {