目录
@AliasFor是spring中的一个注解,可用来对注解属性声明别名。若想起到别名效果,需通过spring进行处理后才能有效果。
互为别名的属性只设置其中一个值,互为别名的其他属性同时也会设置相同值
@TestAliasFor(value="value")
class TestDemo{
}
如果同时设置别名属性值,则值必须一致,否则报错
@TestAliasFor(value="value",name="value")
class TestDemo{
}
1、注解内部的显性别名
@Retention(RetentionPolicy.RUNTIME)
public @interface TestAliasFor {
@AliasFor("name")
String value() default "";
@AliasFor("value")
String name() default "";
}
@Test
public void test6(){
TestAliasFor annotation = AnnotationUtils.findAnnotation(TestDemo.class,
TestAliasFor.class);
System.out.println(annotation.value());
System.out.println(annotation.name());
}
注意:
1.组成别名对的每个属性都必须用@AliasFor进行注释,并且AliasFor中的值必须指向别名对中的另一个属性
2.别名化的属性必须声明相同的返回类型
3.别名化的属性必须声明默认值
4.别名化的属性默认值必须相同
注解内部显示别名只能一对互相别名,若是有3个内部注解互为显性别名,则报错,如下
2、用于元注解属性的显性别名
如果被@AliasFor注解的属性指向的是它所在注解之外的其他注解,那么这个属性被解释成元注解属性的别名。(称之为显性的元注解属性重写)
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Documented
public @interface TestAliasFor1 {
String value() default "";
}
@Retention(RetentionPolicy.RUNTIME)
@TestAliasFor1
public @interface TestAliasFor {
@AliasFor(annotation = TestAliasFor1.class, attribute = "value")
String value() default "";
}
@Test
public void test6(){
TestAliasFor annotation = AnnotationUtils.findAnnotation(TestDemo.class, TestAliasFor.class);
TestAliasFor1 mergedAnnotation = AnnotatedElementUtils.getMergedAnnotation(TestDemo.class, TestAliasFor1.class);
System.out.println(annotation.value());
System.out.println(mergedAnnotation.value() );
}
注意:
1 如果一个属性是一个元注解属性的别名,那么这个属性必须用@AliasFor进行注解并且该属性必须指向元注解属性(若是没指定元注解属性名称,则默认与属性名称一致)
2 别名化的属性必须声明相同的返回结果
3.@AliasFor的annotation属性必须引用元注解
4.被引用的元注解必须放置在声明了@AliasFor的注解类上
3、注解中的隐性别名
如果注解中的一个或多个属性声明为同一个元注解属性的属性重写(直接地或传递地重写)那么这些注解会被当作彼此的隐性别名集来对待,结果是它们的行为类似于注解中的显性别名
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@TestAliasFor1
public @interface TestAliasFor {
@AliasFor(annotation = TestAliasFor1.class, attribute = "value")
String value() default "";
@AliasFor(annotation = TestAliasFor1.class, attribute = "value")
String name() default "";
}
@Test
public void test7(){
TestAliasFor testAliasFor = AnnotatedElementUtils.getMergedAnnotation(TestDemo.class, TestAliasFor.class);
System.out.println(testAliasFor.name());
System.out.println(testAliasFor.value());
TestAliasFor1 testAliasFor1 = AnnotatedElementUtils.getMergedAnnotation(TestDemo.class, TestAliasFor1.class);
System.out.println(testAliasFor1.value());
}
TestAliasFor中的value和name属性都复写了TestAliasFor1的value属性,所以TestAliasFor的value和name是彼此的隐性别名,如下TestAliasFor2的name2和value2也是彼此的隐性别名
@Retention(RetentionPolicy.RUNTIME)
@TestAliasFor
public @interface TestAliasFor2 {
@AliasFor(annotation = TestAliasFor.class, attribute = "name")
String name2() default "";
@AliasFor(annotation = TestAliasFor.class, value = "value")
String value2() default "";
}
注意:
1.属于隐性别名组中的每一个属性必须使用@AliasFor进行注解,并且attribute必须引用相同元注解中的同一个属性(或者相同元注解对应的别名属性)
2.别名化的属性必须声明相同的返回类型
3.别名化的属性必须定义默认值
4.别名化的属性必须声明相同的默认值
5.注解必须引用合适的元注解
6.被引用的元注解必须放置在声明了@AliasFor的注解上
4、别名传递
@AliasFor注解是允许别名之间的传递的,简单理解,如果A是B的别名,并且B是C的别名,那么A是C的别名
参考: