Package org.springframework.core.annotation

# Annotation Interface AliasFor

@Retention(RUNTIME) @Target(METHOD) @Documented public @interface AliasFor
`@AliasFor` is an annotation that is used to declare aliases for annotation attributes.

### Usage Scenarios

- Explicit aliases within an annotation: within a single annotation, `@AliasFor` can be declared on a pair of attributes to signal that they are interchangeable aliases for each other.
- Explicit alias for attribute in meta-annotation: if the `annotation()` attribute of `@AliasFor` is set to a different annotation than the one that declares it, the `attribute()` is interpreted as an alias for an attribute in a meta-annotation (i.e., an explicit meta-annotation attribute override). This enables fine-grained control over exactly which attributes are overridden within an annotation hierarchy. In fact, with `@AliasFor` it is even possible to declare an alias for the `value` attribute of a meta-annotation.
- Implicit aliases within an annotation: if one or more attributes within an annotation are declared as attribute overrides for the same meta-annotation attribute (either directly or transitively), those attributes will be treated as a set of implicit aliases for each other, resulting in behavior analogous to that for explicit aliases within an annotation.

### 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

- Explicit aliases within an annotation:
  1. Each attribute that makes up an aliased pair should be annotated with `@AliasFor`, and either `attribute()` or `value()` must reference the other attribute in the pair. Since Spring Framework 5.2.1 it is technically possible to annotate only one of the attributes in an aliased pair; however, it is recommended to annotate both attributes in an aliased pair for better documentation as well as compatibility with previous versions of the Spring Framework.
  2. Aliased attributes must declare the same return type.
  3. Aliased attributes must declare a default value.
  4. Aliased attributes must declare the same default value.
  5. `annotation()` should not be declared.
- Explicit alias for attribute in meta-annotation:
  1. The attribute that is an alias for an attribute in a meta-annotation must be annotated with `@AliasFor`, and `attribute()` must reference the attribute in the meta-annotation.
  2. Aliased attributes must declare the same return type.
  3. `annotation()` must reference the meta-annotation.
  4. The referenced meta-annotation must be meta-present on the annotation class that declares `@AliasFor`.
- Implicit aliases within an annotation:
  1. Each attribute that belongs to a set of implicit aliases must be annotated with `@AliasFor`, and `attribute()` must reference the same attribute in the same meta-annotation (either directly or transitively via other explicit meta-annotation attribute overrides within the annotation hierarchy).
  2. Aliased attributes must declare the same return type.
  3. Aliased attributes must declare a default value.
  4. Aliased attributes must declare the same default value.
  5. `annotation()` must reference an appropriate meta-annotation.
  6. The referenced meta-annotation must be meta-present on the annotation class that declares `@AliasFor`.

### 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.

Since:
  4.2
Author:
  Sam Brannen
See Also:
  - `MergedAnnotations`
  - `AnnotationUtils.isSynthesizedAnnotation(Annotation)`

## Optional Element Summary

Optional Elements
Modifier and Type
Optional Element
Description
`Class<? extends Annotation>`
`annotation`
The type of annotation in which the aliased `attribute()` is declared.
`String`
`attribute`
The name of the attribute that this attribute is an alias for.
`String`
`value`
Alias for `attribute()`.

## Element Details

### value

@AliasFor("attribute") 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")`.

Default:
  ""

### attribute

@AliasFor("value") String attribute
The name of the attribute that this attribute is an alias for.

See Also:
  - `value()`

Default:
  ""

### annotation

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.

Default:
  java.lang.annotation.Annotation.class