@RetentionSE(valueSE=RUNTIMESE) @TargetSE(valueSE=METHODSE) @DocumentedSE public @interface AliasFor
@AliasFor
は、アノテーション属性のエイリアスを宣言するために使用されるアノテーションです。@AliasFor
を属性のペアで宣言して、それらが相互に交換可能なエイリアスであることを通知できます。@AliasFor
の annotation()
属性がそれを宣言するものとは異なるアノテーションに設定されている場合、attribute()
はメタアノテーションの属性のエイリアスとして解釈されます(つまり、明示的なメタアノテーション属性のオーバーライド)。これにより、アノテーション階層内でオーバーライドされる属性を正確に細かく制御できます。実際、@AliasFor
を使用すると、メタアノテーションの value
属性のエイリアスを宣言することもできます。Java の任意のアノテーションと同様に、@AliasFor
が単独で存在するだけではエイリアスセマンティクスは適用されません。エイリアスセマンティクスを適用するには、MergedAnnotations
を介してアノテーションをロードする必要があります。
@AliasFor
のアノテーションを付ける必要があり、attribute()
または value()
のいずれかがペアのもう一方の属性を参照する必要があります。Spring Framework 5.2.1 以降、エイリアスペアの属性の 1 つだけにアノテーションを付けることが技術的に可能です。ただし、ドキュメントを改善し、Spring Framework の以前のバージョンとの互換性を保つために、エイリアスペアの両方の属性にアノテーションを付けることをお勧めします。annotation()
は宣言しないでください。@AliasFor
でアノテーションを付ける必要があり、attribute()
はメタアノテーションの属性を参照する必要があります。annotation()
はメタアノテーションを参照する必要があります。@AliasFor
を宣言するアノテーションクラスにメタ存在する必要があります。@AliasFor
でアノテーションを付ける必要があり、attribute()
は同じメタアノテーション内の同じ属性を参照する必要があります(アノテーション階層内の他の明示的なメタアノテーション属性オーバーライドを介して直接または推移的に)。annotation()
は適切なメタアノテーションを参照する必要があります。@AliasFor
を宣言するアノテーションクラスにメタ存在する必要があります。@ContextConfiguration
、value
、locations
には、相互の明示的なエイリアスがあります。
public @interface ContextConfiguration { @AliasFor("locations") String[] value() default {}; @AliasFor("value") String[] locations() default {}; // ... }
@XmlTestConfig
では、xmlFiles
は @ContextConfiguration
の locations
の明示的なエイリアスです。つまり、xmlFiles
は @ContextConfiguration
の locations
属性をオーバーライドします。
@ContextConfiguration public @interface XmlTestConfig { @AliasFor(annotation = ContextConfiguration.class, attribute = "locations") String[] xmlFiles(); }
@MyTestConfig
、value
、groovyScripts
、xmlFiles
には、@ContextConfiguration
の locations
属性に対するすべての明示的なメタアノテーション属性のオーバーライドがあります。これら 3 つの属性は、相互の暗黙のエイリアスでもあります。
@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 {}; }
@GroovyOrXmlTestConfig
では、groovy
は @MyTestConfig
の groovyScripts
属性の明示的なオーバーライドです。一方、xml
は、@ContextConfiguration
の locations
属性の明示的なオーバーライドです。さらに、groovy
と xml
は、どちらも @ContextConfiguration
の locations
属性を効果的にオーバーライドするため、相互に推移的な暗黙のエイリアスです。
@MyTestConfig public @interface GroovyOrXmlTestConfig { @AliasFor(annotation = MyTestConfig.class, attribute = "groovyScripts") String[] groovy() default {}; @AliasFor(annotation = ContextConfiguration.class, attribute = "locations") String[] xml() default {}; }
Spring Framework 4.2 以降、コア Spring 内のいくつかのアノテーションが更新され、@AliasFor
を使用して内部属性のエイリアスを構成するようになりました。個々のアノテーションについては Javadoc を、詳細についてはリファレンスマニュアルを参照してください。
MergedAnnotations
, SynthesizedAnnotation
修飾子と型 | オプションの要素と説明 |
---|---|
ClassSE<? extends AnnotationSE> | annotation エイリアス化された attribute() が宣言されているアノテーションの型。 |
StringSE | attribute この属性がエイリアスである属性の名前。 |
StringSE | value attribute() のエイリアス。 |
@AliasFor(value="attribute") public abstract StringSE value
attribute()
のエイリアス。annotation()
が宣言されていない場合、attribute()
の代わりに使用することを意図しています。— 例: @AliasFor(attribute = "value")
の代わりに @AliasFor("value")
public abstract ClassSE<? extends AnnotationSE> annotation
attribute()
が宣言されているアノテーションの型。 デフォルトは Annotation
SE で、エイリアス属性がこの属性と同じアノテーションで宣言されていることを意味します。