|
30 | 30 | import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
31 | 31 | import org.springframework.context.annotation.Bean;
|
32 | 32 | import org.springframework.context.annotation.Configuration;
|
| 33 | +import org.springframework.context.annotation.Fallback; |
33 | 34 | import org.springframework.context.annotation.Lazy;
|
| 35 | +import org.springframework.context.annotation.Primary; |
34 | 36 | import org.springframework.context.annotation.Scope;
|
35 | 37 | import org.springframework.context.annotation.ScopedProxyMode;
|
36 | 38 | import org.springframework.core.annotation.AliasFor;
|
@@ -80,6 +82,26 @@ void scopedProxy() {
|
80 | 82 | ctx.close();
|
81 | 83 | }
|
82 | 84 |
|
| 85 | + @Test |
| 86 | + void primary() { |
| 87 | + AnnotationConfigApplicationContext ctx = |
| 88 | + new AnnotationConfigApplicationContext(PrimaryConfig.class, StandardPojo.class); |
| 89 | + StandardPojo pojo = ctx.getBean(StandardPojo.class); |
| 90 | + assertThat(pojo.testBean.getName()).isEqualTo("interesting"); |
| 91 | + assertThat(pojo.testBean2.getName()).isEqualTo("boring"); |
| 92 | + ctx.close(); |
| 93 | + } |
| 94 | + |
| 95 | + @Test |
| 96 | + void fallback() { |
| 97 | + AnnotationConfigApplicationContext ctx = |
| 98 | + new AnnotationConfigApplicationContext(FallbackConfig.class, StandardPojo.class); |
| 99 | + StandardPojo pojo = ctx.getBean(StandardPojo.class); |
| 100 | + assertThat(pojo.testBean.getName()).isEqualTo("interesting"); |
| 101 | + assertThat(pojo.testBean2.getName()).isEqualTo("boring"); |
| 102 | + ctx.close(); |
| 103 | + } |
| 104 | + |
83 | 105 | @Test
|
84 | 106 | void customWithLazyResolution() {
|
85 | 107 | AnnotationConfigApplicationContext ctx =
|
@@ -201,6 +223,58 @@ public TestBean testBean2(TestBean testBean1) {
|
201 | 223 | }
|
202 | 224 | }
|
203 | 225 |
|
| 226 | + @Configuration |
| 227 | + static class PrimaryConfig { |
| 228 | + |
| 229 | + @Bean @Qualifier("interesting") @Primary |
| 230 | + public static TestBean testBean1() { |
| 231 | + return new TestBean("interesting"); |
| 232 | + } |
| 233 | + |
| 234 | + @Bean @Qualifier("interesting") |
| 235 | + public static TestBean testBean1x() { |
| 236 | + return new TestBean("interesting"); |
| 237 | + } |
| 238 | + |
| 239 | + @Bean @Boring @Primary |
| 240 | + public TestBean testBean2(TestBean testBean1) { |
| 241 | + TestBean tb = new TestBean("boring"); |
| 242 | + tb.setSpouse(testBean1); |
| 243 | + return tb; |
| 244 | + } |
| 245 | + |
| 246 | + @Bean @Boring |
| 247 | + public TestBean testBean2x() { |
| 248 | + return new TestBean("boring"); |
| 249 | + } |
| 250 | + } |
| 251 | + |
| 252 | + @Configuration |
| 253 | + static class FallbackConfig { |
| 254 | + |
| 255 | + @Bean @Qualifier("interesting") |
| 256 | + public static TestBean testBean1() { |
| 257 | + return new TestBean("interesting"); |
| 258 | + } |
| 259 | + |
| 260 | + @Bean @Qualifier("interesting") @Fallback |
| 261 | + public static TestBean testBean1x() { |
| 262 | + return new TestBean("interesting"); |
| 263 | + } |
| 264 | + |
| 265 | + @Bean @Boring |
| 266 | + public TestBean testBean2(TestBean testBean1) { |
| 267 | + TestBean tb = new TestBean("boring"); |
| 268 | + tb.setSpouse(testBean1); |
| 269 | + return tb; |
| 270 | + } |
| 271 | + |
| 272 | + @Bean @Boring @Fallback |
| 273 | + public TestBean testBean2x() { |
| 274 | + return new TestBean("boring"); |
| 275 | + } |
| 276 | + } |
| 277 | + |
204 | 278 | @Component @Lazy
|
205 | 279 | static class StandardPojo {
|
206 | 280 |
|
|
0 commit comments