Closed
Description
Sam Brannen opened SPR-11512 and commented
Status Quo
Limited support for annotation attribute aliases already exists in the Spring Framework but is scattered across various modules. Currently, the following annotations are known to support aliases:
@ManagedResource
@ActiveProfiles
@ContextConfiguration
@Sql
@TestExecutionListeners
@TestPropertySource
@ControllerAdvice
Goal
The goal of this issue is to introduce unified support for declaring and looking up annotation attribute aliases (within a given annotation).
Deliverables
All annotations introduced in conjunction with this issue must reside in the org.springframework.core.annotation
package.
- Introduce an annotation that can be declared on annotation attributes to indicate that the given attribute is an alias for another attribute of the same type within the same annotation class.
- Proposed name:
@AliasFor
- Proposed name:
- Consider introducing another annotation to be used on the aliased attribute itself in order to provide a two-way link between all such aliases.
- Proposed name:
@AliasedBy
- Proposed name:
- Extract code from the TestContext framework that is responsible for handling the look-up of annotation aliases and incorporate this code in a generic fashion in
AnnotationUtils
andAnnotatedElementUtils
as appropriate.- This new look-up mechanism must be based on the annotation introduced in deliverable Spring core JMS pom.xml #1.
- If multiple values for an aliased attribute are present in a declared annotation, an
AnnotationConfigurationException
must be thrown indicating that only one attribute for an aliased attribute may be specified in a concrete instance of the given annotation. - See also the current implementations for attribute aliases related to
@ControllerAdvice
and@ManagedResource
.
- Migrate all existing code that supports attribute aliases to use this new mechanism.
Example: Modified @ContextConfiguration
Declaration
The following demonstrates how @ContextConfiguration
could be rewritten to take advantage of the functionality proposed by this issue.
public @interface ContextConfiguration {
@AliasedBy("locations")
String[] value() default {};
@AliasFor("value")
String[] locations() default {};
// ...
}
Affects: 4.0 GA
Issue Links:
- Custom @RequestMapping annotations [SPR-12296] #16901 Custom
@RequestMapping
annotations ("is depended on by") - Document Spring Annotation Programming Model in the Wiki [SPR-11515] #16140 Document Spring Annotation Programming Model in the Wiki ("is depended on by")
- Introduce aliases for 'value' annotation attributes [SPR-11393] #16020 Introduce aliases for 'value' annotation attributes ("is depended on by")
- Implement toString() for synthesized annotations [SPR-13064] #17656 Implement toString() for synthesized annotations ("is depended on by")
- Implement equals() for synthesized annotations [SPR-13065] #17657 Implement equals() for synthesized annotations ("is depended on by")
- Implement hashCode() for synthesized annotations [SPR-13066] #17658 Implement hashCode() for synthesized annotations ("is depended on by")
- Introduce 'value' alias for 'attribute' in @AliasFor [SPR-13289] #17879 Introduce 'value' alias for 'attribute' in
@AliasFor
("is depended on by") - SynthesizedAnnotation must be public [SPR-13057] #17649 SynthesizedAnnotation must be public
- Introduce support for explicit annotation attribute overrides [SPR-11513] #16138 Introduce support for explicit annotation attribute overrides
- Introduce support for synthesizing AnnotationAttributes into an annotation [SPR-13067] #17659 Introduce support for synthesizing AnnotationAttributes into an annotation
- Introduce aliases for 'value' annotation attributes [SPR-11393] #16020 Introduce aliases for 'value' annotation attributes