From 3d546f5b8b8e1c7238d12ea31bdc754b6b0cee87 Mon Sep 17 00:00:00 2001 From: Yanming Zhou Date: Wed, 21 Feb 2024 10:25:15 +0800 Subject: [PATCH] Clarify fallback beans are included for autowiring Arrays, Collections, and Maps Test is improved to verify that. --- .../context/annotation/Fallback.java | 3 +++ .../BeanMethodQualificationTests.java | 21 ++++++++++++++----- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/spring-context/src/main/java/org/springframework/context/annotation/Fallback.java b/spring-context/src/main/java/org/springframework/context/annotation/Fallback.java index 9ff6d16d7383..c57237f2554e 100644 --- a/spring-context/src/main/java/org/springframework/context/annotation/Fallback.java +++ b/spring-context/src/main/java/org/springframework/context/annotation/Fallback.java @@ -29,7 +29,10 @@ *

If all beans but one among multiple matching candidates are marked * as a fallback, the remaining bean will be selected. * + *

Fallback beans are included for autowiring Arrays, Collections, and Maps. + * * @author Juergen Hoeller + * @author Yanming Zhou * @since 6.2 * @see Primary * @see Lazy diff --git a/spring-context/src/test/java/org/springframework/context/annotation/configuration/BeanMethodQualificationTests.java b/spring-context/src/test/java/org/springframework/context/annotation/configuration/BeanMethodQualificationTests.java index d9ed7e70d031..1c7d5cc6452e 100644 --- a/spring-context/src/test/java/org/springframework/context/annotation/configuration/BeanMethodQualificationTests.java +++ b/spring-context/src/test/java/org/springframework/context/annotation/configuration/BeanMethodQualificationTests.java @@ -18,6 +18,8 @@ import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; +import java.util.List; +import java.util.Map; import java.util.Optional; import org.junit.jupiter.api.Test; @@ -47,6 +49,7 @@ * * @author Chris Beams * @author Juergen Hoeller + * @author Yanming Zhou */ class BeanMethodQualificationTests { @@ -91,11 +94,12 @@ void primary() { assertThat(pojo.testBean.getName()).isEqualTo("interesting"); assertThat(pojo.testBean2.getName()).isEqualTo("boring"); ConstructorPojo pojo2 = ctx.getBean(ConstructorPojo.class); - assertThat(pojo2.testBean.getName()).isEqualTo("interesting"); - assertThat(pojo2.testBean2.getName()).isEqualTo("boring"); + assertThat(pojo2.testBean).isSameAs(pojo.testBean); + assertThat(pojo2.testBean2).isSameAs(pojo.testBean2); ctx.close(); } + @SuppressWarnings("unchecked") @Test void fallback() { AnnotationConfigApplicationContext ctx = @@ -103,9 +107,13 @@ void fallback() { StandardPojo pojo = ctx.getBean(StandardPojo.class); assertThat(pojo.testBean.getName()).isEqualTo("interesting"); assertThat(pojo.testBean2.getName()).isEqualTo("boring"); + assertThat(pojo.testBean2.getSpouse().getName()).isEqualTo("interesting"); + assertThat(pojo.testBean2.getFriends()).contains(ctx.getBean("testBean1x", TestBean.class), ctx.getBean("testBean2x", TestBean.class)); // array injection + assertThat((List) pojo.testBean2.getSomeList()).contains(ctx.getBean("testBean1x", TestBean.class), ctx.getBean("testBean2x", TestBean.class)); // list injection + assertThat((Map) pojo.testBean2.getSomeMap()).containsKeys("testBean1x", "testBean2x"); // map injection ConstructorPojo pojo2 = ctx.getBean(ConstructorPojo.class); - assertThat(pojo2.testBean.getName()).isEqualTo("interesting"); - assertThat(pojo2.testBean2.getName()).isEqualTo("boring"); + assertThat(pojo2.testBean).isSameAs(pojo.testBean); + assertThat(pojo2.testBean2).isSameAs(pojo.testBean2); ctx.close(); } @@ -270,9 +278,12 @@ public static TestBean testBean1x() { } @Bean @Boring - public TestBean testBean2(TestBean testBean1) { + public TestBean testBean2(TestBean testBean1, TestBean[] testBeanArray, List testBeanList, Map testBeanMap) { TestBean tb = new TestBean("boring"); tb.setSpouse(testBean1); + tb.setFriends(List.of((Object[]) testBeanArray)); // add cast to avoid varargs warning + tb.setSomeList(testBeanList); + tb.setSomeMap(testBeanMap); return tb; }