|
19 | 19 | import java.io.IOException; |
20 | 20 | import java.lang.annotation.Retention; |
21 | 21 | import java.lang.annotation.RetentionPolicy; |
| 22 | +import java.util.Collections; |
22 | 23 | import java.util.List; |
23 | 24 | import java.util.Optional; |
24 | 25 |
|
|
29 | 30 | import org.springframework.beans.factory.InitializingBean; |
30 | 31 | import org.springframework.beans.factory.ObjectFactory; |
31 | 32 | import org.springframework.beans.factory.annotation.Autowired; |
| 33 | +import org.springframework.beans.factory.annotation.Qualifier; |
32 | 34 | import org.springframework.beans.factory.annotation.Value; |
33 | 35 | import org.springframework.beans.factory.parsing.BeanDefinitionParsingException; |
34 | 36 | import org.springframework.beans.factory.support.DefaultListableBeanFactory; |
@@ -100,6 +102,16 @@ void testAutowiredConfigurationMethodDependenciesWithOptionalAndNotAvailable() { |
100 | 102 | context.close(); |
101 | 103 | } |
102 | 104 |
|
| 105 | + @Test |
| 106 | + void testAutowiredConfigurationMethodDependenciesWithQualifier() { |
| 107 | + AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext( |
| 108 | + QualifiedAutowiredMethodConfig.class); |
| 109 | + |
| 110 | + assertThat(context.getBeansOfType(Colour.class)).isEmpty(); |
| 111 | + assertThat(context.getBean(TestBean.class).getName()).isEmpty(); |
| 112 | + context.close(); |
| 113 | + } |
| 114 | + |
103 | 115 | @Test |
104 | 116 | void testAutowiredSingleConstructorSupported() { |
105 | 117 | DefaultListableBeanFactory factory = new DefaultListableBeanFactory(); |
@@ -297,6 +309,25 @@ public TestBean testBean(Optional<Colour> colour, Optional<List<Colour>> colours |
297 | 309 | } |
298 | 310 |
|
299 | 311 |
|
| 312 | + @Configuration |
| 313 | + static class QualifiedAutowiredMethodConfig { |
| 314 | + |
| 315 | + @Bean |
| 316 | + @Qualifier("testBean") |
| 317 | + public TestBean testBean(Optional<Colour> colour, Optional<List<Colour>> colours) { |
| 318 | + if (!colour.isEmpty() || !colours.isEmpty()) { |
| 319 | + throw new IllegalStateException("Unexpected match: " + colour + " " + colours); |
| 320 | + } |
| 321 | + return new TestBean(""); |
| 322 | + } |
| 323 | + |
| 324 | + @Bean |
| 325 | + public List<?> someList() { |
| 326 | + return Collections.singletonList(new TestBean("shouldNotMatch")); |
| 327 | + } |
| 328 | + } |
| 329 | + |
| 330 | + |
300 | 331 | @Configuration |
301 | 332 | static class AutowiredConstructorConfig { |
302 | 333 |
|
|
0 commit comments