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