Browse Source

Generic matching for ObjectProvider stream and empty vararg resolution

Issue: SPR-11419
Issue: SPR-15338
pull/1894/head
Juergen Hoeller 6 years ago
parent
commit
a9c9ba6601
  1. 10
      spring-beans/src/main/java/org/springframework/beans/factory/support/ConstructorResolver.java
  2. 15
      spring-beans/src/main/java/org/springframework/beans/factory/support/DefaultListableBeanFactory.java
  3. 591
      spring-beans/src/test/java/org/springframework/beans/factory/annotation/AutowiredAnnotationBeanPostProcessorTests.java
  4. 223
      spring-beans/src/test/java/org/springframework/beans/factory/annotation/InjectAnnotationBeanPostProcessorTests.java
  5. 29
      spring-context/src/test/java/org/springframework/context/annotation/ConfigurationClassPostProcessorTests.java

10
spring-beans/src/main/java/org/springframework/beans/factory/support/ConstructorResolver.java

@ -17,6 +17,7 @@ @@ -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 { @@ -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)) {

15
spring-beans/src/main/java/org/springframework/beans/factory/support/DefaultListableBeanFactory.java

@ -1229,7 +1229,7 @@ public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFacto @@ -1229,7 +1229,7 @@ public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFacto
if (descriptor.isStreamAccess()) {
Map<String, Object> 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 @@ -1247,7 +1247,7 @@ public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFacto
return null;
}
Map<String, Object> 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 @@ -1267,7 +1267,7 @@ public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFacto
return null;
}
Map<String, Object> 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 @@ -1292,7 +1292,7 @@ public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFacto
return null;
}
Map<String, Object> 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 @@ -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();
}
}
}

591
spring-beans/src/test/java/org/springframework/beans/factory/annotation/AutowiredAnnotationBeanPostProcessorTests.java

File diff suppressed because it is too large Load Diff

223
spring-beans/src/test/java/org/springframework/beans/factory/annotation/InjectAnnotationBeanPostProcessorTests.java

@ -26,6 +26,8 @@ import javax.inject.Inject; @@ -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.*; @@ -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 { @@ -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 { @@ -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 { @@ -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 { @@ -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 { @@ -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 { @@ -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 { @@ -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 { @@ -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 { @@ -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 { @@ -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 { @@ -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 { @@ -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 { @@ -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 { @@ -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 { @@ -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 { @@ -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 { @@ -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 { @@ -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 { @@ -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 { @@ -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 { @@ -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 { @@ -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 { @@ -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 { @@ -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 { @@ -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"));

29
spring-context/src/test/java/org/springframework/context/annotation/ConfigurationClassPostProcessorTests.java

@ -801,6 +801,23 @@ public class ConfigurationClassPostProcessorTests { @@ -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 { @@ -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 {

Loading…
Cancel
Save