-
Notifications
You must be signed in to change notification settings - Fork 38.9k
Description
Sam Brannen opened SPR-13345 and commented
Status Quo
Spring Framework 4.2 introduced support for explicit annotation attribute overrides in meta-annotations via @AliasFor.
For example, the following @SingleAliasConfig works as expected. xmlFiles functions as an attribute override for locations in @ContextConfiguration.
@ContextConfiguration
public @interface SingleAliasConfig {
@AliasFor(annotation = ContextConfiguration.class, attribute = "locations")
String[] xmlFiles() default {};
// ...
}However, the following @ImplicitAliasesConfig does not work as expected. One would intuitively expect that any one of xmlFiles, groovyScripts, or value could be used as an attribute override for locations in @ContextConfiguration, but at runtime, we don't actually know which one of the aliases will be ultimately used. In other words, one would expect xmlFiles, groovyScripts, and value to be considered implicit aliases for each other.
The reason is that the merge algorithm in MergedAnnotationAttributesProcessor in AnnotatedElementUtils overwrites previous aliased values with subsequent aliased values, thereby letting the last one win -- which happens silently and is unintuitive.
@ContextConfiguration
public @interface ImplicitAliasesConfig {
@AliasFor(annotation = ContextConfiguration.class, attribute = "locations")
String[] xmlFiles() default {};
@AliasFor(annotation = ContextConfiguration.class, attribute = "locations")
String[] groovyScripts() default {};
@AliasFor(annotation = ContextConfiguration.class, attribute = "locations")
String[] value() default {};
// ...
}Similarly, the following declaration of transitive implicit aliases is not supported: xml and groovy will not be considered as implicit aliases for each other even though they override attributes in @ImplicitAliasesConfig which are implicit aliases for each other.
@ImplicitAliasesConfig
public @interface TransitiveImplicitAliasesConfig {
@AliasFor(annotation = ImplicitAliasesConfig.class, attribute = "xmlFiles")
String[] xml() default {};
@AliasFor(annotation = ImplicitAliasesConfig.class, attribute = "groovyScripts")
String[] groovy() default {};
// ...
}Deliverables
- Support implicit aliases configured via
@AliasForfor the same attribute in a meta-annotation. - Support transitive implicit aliases.
Affects: 4.2 GA
Reference URL: spring-projects/spring-boot#3635
Issue Links:
- @AliasFor potentially overrides attribute in wrong meta-annotation [SPR-13325] #17910
@AliasForpotentially overrides attribute in wrong meta-annotation ("depends on") - Support transitive implicit attribute aliases with @AliasFor [SPR-13405] #17985 Support transitive implicit attribute aliases with
@AliasFor("is depended on by")