@Retention(value=RUNTIME) @Target(value=METHOD) @Documented public @interface AliasFor
@AliasFor is an annotation that is used to declare aliases for
annotation attributes.
@AliasFor can be declared on a pair of attributes to
signal that they are interchangeable aliases for each other.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.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 the utility methods in
AnnotationUtils. Behind the scenes, Spring will synthesize
an annotation by wrapping it in a dynamic proxy that transparently enforces
attribute alias semantics for annotation attributes that are
annotated with @AliasFor. Similarly, AnnotatedElementUtils
supports explicit meta-annotation attribute overrides when @AliasFor
is used within an annotation hierarchy. Typically you will not need to
manually synthesize annotations on your own since Spring will do that for
you transparently when looking up annotations on Spring-managed components.
@AliasFor, and the attribute() must reference the
other attribute in the pair.annotation() attribute may remain set to the default,
although setting it to the declaring class for both attributes in the
pair is also valid.@AliasFor; the attribute() must
reference the aliased attribute in the meta-annotation; and the
annotation() must reference the meta-annotation. public @interface ContextConfiguration {
@AliasFor(attribute = "locations")
String[] value() default {};
@AliasFor(attribute = "value")
String[] locations() default {};
// ...
}
@ContextConfiguration
public @interface MyTestConfig {
@AliasFor(annotation = ContextConfiguration.class, attribute = "locations")
String[] xmlFiles();
}
As of Spring Framework 4.2, the following annotations within core
Spring have been updated to use @AliasFor to configure their
internal attribute aliases.
org.springframework.jmx.export.annotation.ManagedResourceorg.springframework.test.context.ActiveProfilesorg.springframework.test.context.ContextConfigurationorg.springframework.test.context.TestExecutionListenersorg.springframework.test.context.TestPropertySourceorg.springframework.test.context.jdbc.Sqlorg.springframework.web.bind.annotation.ControllerAdviceorg.springframework.web.bind.annotation.RequestMappingAnnotatedElementUtils,
AnnotationUtils,
AnnotationUtils.synthesizeAnnotation(Annotation, java.lang.reflect.AnnotatedElement),
SynthesizedAnnotation| Modifier and Type | Required Element and Description |
|---|---|
String |
attribute
The name of the attribute that this attribute is an alias for.
|
| Modifier and Type | Optional Element and Description |
|---|---|
Class<? extends Annotation> |
annotation
The type of annotation in which the aliased
attribute() is declared. |
public abstract String attribute
public abstract Class<? extends Annotation> annotation
attribute() is declared.
Defaults to Annotation, implying that the aliased attribute is
declared in the same annotation as this attribute.