|
1 | 1 | /*
|
2 |
| - * Copyright 2002-2012 the original author or authors. |
| 2 | + * Copyright 2002-2016 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
21 | 21 | import java.lang.annotation.RetentionPolicy;
|
22 | 22 | import java.lang.annotation.Target;
|
23 | 23 |
|
| 24 | +import org.junit.Ignore; |
24 | 25 | import org.junit.Test;
|
25 | 26 |
|
26 | 27 | import org.springframework.aop.scope.ScopedProxyUtils;
|
|
33 | 34 | import org.springframework.beans.factory.config.ConstructorArgumentValues;
|
34 | 35 | import org.springframework.context.annotation.AnnotationConfigUtils;
|
35 | 36 | import org.springframework.context.support.GenericApplicationContext;
|
| 37 | +import org.springframework.core.annotation.AliasFor; |
36 | 38 |
|
37 | 39 | import static org.junit.Assert.*;
|
38 | 40 |
|
|
42 | 44 | * @author Mark Fisher
|
43 | 45 | * @author Juergen Hoeller
|
44 | 46 | * @author Chris Beams
|
| 47 | + * @author Sam Brannen |
45 | 48 | */
|
46 | 49 | public class QualifierAnnotationAutowireContextTests {
|
47 | 50 |
|
48 | 51 | private static final String JUERGEN = "juergen";
|
49 | 52 |
|
50 | 53 | private static final String MARK = "mark";
|
51 | 54 |
|
| 55 | + private static final String SAM = "sam"; |
52 | 56 |
|
53 | 57 | @Test
|
54 | 58 | public void testAutowiredFieldWithSingleNonQualifiedCandidate() {
|
@@ -308,6 +312,24 @@ public void testAutowiredFieldResolvesMetaQualifiedCandidate() {
|
308 | 312 | assertEquals(JUERGEN, bean.getPerson().getName());
|
309 | 313 | }
|
310 | 314 |
|
| 315 | + /** |
| 316 | + * @see SpringBean |
| 317 | + */ |
| 318 | + @Test |
| 319 | + @Ignore("Disabled until SPR-14058 is resolved") |
| 320 | + public void autowiredFieldResolutionIgnoresEmptyQualifierFromComposedQualifierAnnotation() { |
| 321 | + GenericApplicationContext context = new GenericApplicationContext(); |
| 322 | + ConstructorArgumentValues cavs1 = new ConstructorArgumentValues(); |
| 323 | + cavs1.addGenericArgumentValue(SAM); |
| 324 | + RootBeanDefinition person1 = new RootBeanDefinition(Person.class, cavs1, null); |
| 325 | + context.registerBeanDefinition(SAM, person1); |
| 326 | + context.registerBeanDefinition("autowired", new RootBeanDefinition(ComposedAnnotationQualifiedFieldTestBean.class)); |
| 327 | + AnnotationConfigUtils.registerAnnotationConfigProcessors(context); |
| 328 | + context.refresh(); |
| 329 | + ComposedAnnotationQualifiedFieldTestBean bean = context.getBean(ComposedAnnotationQualifiedFieldTestBean.class); |
| 330 | + assertEquals(SAM, bean.getPerson().getName()); |
| 331 | + } |
| 332 | + |
311 | 333 | @Test
|
312 | 334 | public void testAutowiredMethodParameterResolvesQualifiedCandidate() {
|
313 | 335 | GenericApplicationContext context = new GenericApplicationContext();
|
@@ -635,6 +657,37 @@ public Person getPerson() {
|
635 | 657 | }
|
636 | 658 |
|
637 | 659 |
|
| 660 | + /** |
| 661 | + * {@code @SpringBean} is a composed annotation that combines the semantics of |
| 662 | + * {@code Autowired @Autowired} and {@code Qualifier @Qualifier} |
| 663 | + */ |
| 664 | + @Autowired |
| 665 | + @Qualifier |
| 666 | + @Retention(RetentionPolicy.RUNTIME) |
| 667 | + public @interface SpringBean { |
| 668 | + |
| 669 | + @AliasFor(annotation = Qualifier.class) |
| 670 | + String value() default ""; |
| 671 | + |
| 672 | + @AliasFor(annotation = Qualifier.class, attribute = "value") |
| 673 | + String qualifier() default ""; |
| 674 | + |
| 675 | + @AliasFor(annotation = Autowired.class) |
| 676 | + boolean required() default true; |
| 677 | + |
| 678 | + } |
| 679 | + |
| 680 | + private static class ComposedAnnotationQualifiedFieldTestBean { |
| 681 | + |
| 682 | + @SpringBean |
| 683 | + private Person person; |
| 684 | + |
| 685 | + public Person getPerson() { |
| 686 | + return this.person; |
| 687 | + } |
| 688 | + } |
| 689 | + |
| 690 | + |
638 | 691 | private static class QualifiedMethodParameterTestBean {
|
639 | 692 |
|
640 | 693 | private Person person;
|
|
0 commit comments