@Retention(value=RUNTIME)
@Target(value=METHOD)
@Documented
public @interface AliasFor
@AliasFor
は、アノテーション属性のエイリアスを宣言するために使用されるアノテーションです。@AliasFor
を属性のペアで宣言して、それらが相互に交換可能なエイリアスであることを通知できます。@AliasFor
の annotation()
属性がそれを宣言するものとは異なるアノテーションに設定されている場合、attribute()
はメタアノテーションの属性のエイリアスとして解釈されます(つまり、明示的なメタアノテーション属性のオーバーライド)。これにより、アノテーション階層内でオーバーライドされる属性を正確に細かく制御できます。実際、@AliasFor
を使用すると、メタアノテーションの value
属性のエイリアスを宣言することもできます。Java の他のアノテーションと同様に、@AliasFor
が単独で存在するだけではエイリアスセマンティクスは適用されません。エイリアスセマンティクスを適用するには、AnnotationUtils
のユーティリティメソッドを介してアノテーションをロードする必要があります。Spring は背後で、@AliasFor
でアノテーションが付けられたアノテーション属性に属性エイリアスセマンティクスを透過的に適用する動的プロキシでラップすることにより、アノテーションを合成します。同様に、AnnotatedElementUtils
は、@AliasFor
がアノテーション階層内で使用される場合、明示的なメタアノテーション属性のオーバーライドをサポートします。通常、Spring が Spring 管理コンポーネントのアノテーションを検索するときに透過的に行うため、手動でアノテーションを合成する必要はありません。
@AliasFor
でアノテーションを付ける必要があり、attribute()
または value()
のいずれかがペアのもう一方の属性を参照する必要があります。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 を、詳細についてはリファレンスマニュアルを参照してください。
修飾子と型 | オプションの要素と説明 |
---|---|
java.lang.Class<? extends java.lang.annotation.Annotation> | annotation エイリアス化された attribute() が宣言されているアノテーションの型。 |
java.lang.String | attribute この属性がエイリアスである属性の名前。 |
java.lang.String | value attribute() のエイリアス。 |
@AliasFor(value="attribute") public abstract java.lang.String value
attribute()
のエイリアス。annotation()
が宣言されていない場合、attribute()
の代わりに使用することを意図しています。— 例: @AliasFor(attribute = "value")
の代わりに @AliasFor("value")
public abstract java.lang.Class<? extends java.lang.annotation.Annotation> annotation
attribute()
が宣言されているアノテーションの型。 デフォルトは Annotation
で、エイリアス属性がこの属性と同じアノテーションで宣言されていることを意味します。