Skip to content

Commit

Permalink
Clarify fallback beans are included for autowiring Arrays, Collection…
Browse files Browse the repository at this point in the history
…s, and Maps

Test is improved to verify that.
  • Loading branch information
quaff committed Feb 21, 2024
1 parent 22b41c3 commit 3d546f5
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@
* <p>If all beans but one among multiple matching candidates are marked
* as a fallback, the remaining bean will be selected.
*
* <p>Fallback beans are included for autowiring Arrays, Collections, and Maps.
*
* @author Juergen Hoeller
* @author Yanming Zhou
* @since 6.2
* @see Primary
* @see Lazy
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -47,6 +49,7 @@
*
* @author Chris Beams
* @author Juergen Hoeller
* @author Yanming Zhou
*/
class BeanMethodQualificationTests {

Expand Down Expand Up @@ -91,21 +94,26 @@ 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 =
new AnnotationConfigApplicationContext(FallbackConfig.class, StandardPojo.class, ConstructorPojo.class);
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<TestBean>) pojo.testBean2.getSomeList()).contains(ctx.getBean("testBean1x", TestBean.class), ctx.getBean("testBean2x", TestBean.class)); // list injection
assertThat((Map<String, TestBean>) 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();
}

Expand Down Expand Up @@ -270,9 +278,12 @@ public static TestBean testBean1x() {
}

@Bean @Boring
public TestBean testBean2(TestBean testBean1) {
public TestBean testBean2(TestBean testBean1, TestBean[] testBeanArray, List<TestBean> testBeanList, Map<String, TestBean> 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;
}

Expand Down

0 comments on commit 3d546f5

Please sign in to comment.