Skip to content

Support implicit attribute aliases with @AliasFor [SPR-13345] #17929

@spring-projects-issues

Description

@spring-projects-issues

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

  1. Support implicit aliases configured via @AliasFor for the same attribute in a meta-annotation.
  2. Support transitive implicit aliases.

Affects: 4.2 GA

Reference URL: spring-projects/spring-boot#3635

Issue Links:

Referenced from: commits 2a6716d, d40a35b, 3eacb83

Metadata

Metadata

Assignees

Labels

in: coreIssues in core modules (aop, beans, core, context, expression)type: enhancementA general enhancement

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions