Top Description Methods
org.springframework.core.annotation

public @Interface AliasFor

extends Annotation
Annotations
@Retention:RUNTIME
@Target:METHOD
@Documented
Imports
java.lang.annotation.Annotation, .Documented, .ElementType, .Retention, .RetentionPolicy, .Target

@AliasFor is an annotation that is used to declare aliases for annotation attributes.

Usage Scenarios

Usage Requirements

Like with any annotation in Java, the mere presence of @AliasFor on its own will not enforce alias semantics. For alias semantics to be enforced, annotations must be loaded via MergedAnnotations.

Implementation Requirements

Example: Explicit Aliases within an Annotation

In @ContextConfiguration, value and locations are explicit aliases for each other.

 public @interface ContextConfiguration {

   @AliasFor("locations")
   String[] value() default {};

   @AliasFor("value")
   String[] locations() default {};

   // ...
}

Example: Explicit Alias for Attribute in Meta-annotation

In @XmlTestConfig, xmlFiles is an explicit alias for locations in @ContextConfiguration. In other words, xmlFiles overrides the locations attribute in @ContextConfiguration.

 @ContextConfiguration
public @interface XmlTestConfig {

   @AliasFor(annotation = ContextConfiguration.class, attribute = "locations")
   String[] xmlFiles();
}

Example: Implicit Aliases within an Annotation

In @MyTestConfig, value, groovyScripts, and xmlFiles are all explicit meta-annotation attribute overrides for the locations attribute in @ContextConfiguration. These three attributes are therefore also implicit aliases for each other.

 @ContextConfiguration
public @interface MyTestConfig {

   @AliasFor(annotation = ContextConfiguration.class, attribute = "locations")
   String[] value() default {};

   @AliasFor(annotation = ContextConfiguration.class, attribute = "locations")
   String[] groovyScripts() default {};

   @AliasFor(annotation = ContextConfiguration.class, attribute = "locations")
   String[] xmlFiles() default {};
}

Example: Transitive Implicit Aliases within an Annotation

In @GroovyOrXmlTestConfig, groovy is an explicit override for the groovyScripts attribute in @MyTestConfig; whereas, xml is an explicit override for the locations attribute in @ContextConfiguration. Furthermore, groovy and xml are transitive implicit aliases for each other, since they both effectively override the locations attribute in @ContextConfiguration.

 @MyTestConfig
public @interface GroovyOrXmlTestConfig {

   @AliasFor(annotation = MyTestConfig.class, attribute = "groovyScripts")
   String[] groovy() default {};

   @AliasFor(annotation = ContextConfiguration.class, attribute = "locations")
   String[] xml() default {};
}

Spring Annotations Supporting Attribute Aliases

As of Spring Framework 4.2, several annotations within core Spring have been updated to use @AliasFor to configure their internal attribute aliases. Consult the Javadoc for individual annotations as well as the reference manual for details.

Author
Sam Brannen
Since
4.2
See Also
MergedAnnotations, AnnotationUtils#isSynthesizedAnnotation(Annotation)

Method Summary

Modifier and TypeMethod and Description
public Class<? extends Annotation>
annotation()

The type of annotation in which the aliased attribute is declared.

public String
attribute()

The name of the attribute that this attribute is an alias for.

public String
value()

Alias for attribute.

Inherited from java.lang.annotation.Annotation:
annotationTypeequalshashCodetoString

Method Detail

annotationback to summary
public Class<? extends Annotation> annotation()

The type of annotation in which the aliased attribute is declared.

Defaults to Annotation, implying that the aliased attribute is declared in the same annotation as this attribute.

attributeback to summary
public String attribute()

The name of the attribute that this attribute is an alias for.

Annotations
@AliasFor:value
See Also
value
valueback to summary
public String value()

Alias for attribute.

Intended to be used instead of attribute when annotation is not declared — for example: @AliasFor("value") instead of @AliasFor(attribute = "value").

Annotations
@AliasFor:attribute