1.概念
@AliasFor注解可以為注解本身提供別名
2.配對別名
public class AliasForDemo {
public static class TestBean
{
@AliasFor("test")
public void test()
{}
}
@Test
public void test1() throws NoSuchMethodException {
Method method=TestBean.class.getMethod("test",null);
AliasFor aliasFor1=method.getAnnotation(AliasFor.class);
AliasFor aliasFor2=AnnotationUtils.synthesizeAnnotation(aliasFor1,null);
AliasFor aliasFor3=AnnotationUtils.getAnnotation(method,AliasFor.class);
}
@Test
public void test2() throws NoSuchMethodException {
ContextConfig contextConfig1 = SimpleConfigTestCase.class.getAnnotation(ContextConfig.class);
ContextConfig contextConfig2=AnnotationUtils.synthesizeAnnotation(contextConfig1,null);
ContextConfig contextConfig3=AnnotationUtils.getAnnotation(SimpleConfigTestCase.class,ContextConfig.class);
}
@ContextConfig("simple.xml")
static class SimpleConfigTestCase {
}
@Retention(RetentionPolicy.RUNTIME)
@interface ContextConfig {
@AliasFor("location")
String value() default "";
@AliasFor("value")
String location() default "";
Class<?> klass() default Object.class;
}
}
輸出結果:
可以看到,如果使用jdk內(nèi)置方法獲取注解是無法使用別名特性的,因為該特性是spring提供,所以可以調(diào)用AnnotationUtils.synthesizeAnnotation方法來獲取一個動態(tài)代理注解
也可以直接調(diào)用AnnotationUtils.getAnnotation來獲取注解,內(nèi)部會調(diào)用AnnotationUtils.synthesizeAnnotation方法
參考:
https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/core/annotation/AliasFor.html
http://ifeve.com/annotation-programming-model/