Previously, one annotation attribute only supported one alias. You can only do like this:
@Target({ElementType.ANNOTATION_TYPE, ElementType.FIELD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface Test1 {
String test1() default "test1";
}
@Target({ElementType.ANNOTATION_TYPE, ElementType.FIELD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface Test2 {
String test2() default "test2";
}
@Target({ElementType.ANNOTATION_TYPE, ElementType.FIELD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Test1
@Test2
public @interface Test3 {
@AliasFor(annotation = Test1.class, attribute = "test1")
String test3() default "test3";
@AliasFor(annotation = Test2.class, attribute = "test2")
String test4() default "test4";
}
But now, you can do this more easily:
@Target({ElementType.ANNOTATION_TYPE, ElementType.FIELD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Test1
@Test2
public @interface Test3 {
@AliasFor(annotation = Test1.class, attribute = "test1")
@AliasFor(annotation = Test2.class, attribute = "test2")
String test3() default "test3";
}
Moreover, the mutual aliasing of different attributes in the same annotation can break the original limitation (although this kind of use scenario is less):
@Target({ElementType.ANNOTATION_TYPE, ElementType.FIELD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface Test4 {
@AliasFor("test2")
@AliasFor("test3")
String test1() default "test";
@AliasFor("test1")
@AliasFor("test3")
String test2() default "test";
@AliasFor("test1")
@AliasFor("test2")
String test3() default "test";
}
Combine examples above, you can use @AliasFor
like this:
@Target({ElementType.ANNOTATION_TYPE, ElementType.FIELD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface Test5 {
@AliasFor("test2")
@AliasFor("test3")
String test1() default "test1";
@AliasFor("test1")
@AliasFor("test3")
String test2() default "test1";
@AliasFor("test1")
@AliasFor("test2")
String test3() default "test1";
}
@Target({ElementType.ANNOTATION_TYPE, ElementType.FIELD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Test5
public @interface Test6 {
@AliasFor("test2")
@AliasFor("test3")
String test1() default "test2";
@AliasFor("test1")
@AliasFor("test3")
String test2() default "test2";
@AliasFor(annotation = Test5.class)
@AliasFor("test1")
@AliasFor("test2")
String test3() default "test2";
}
@Target({ElementType.ANNOTATION_TYPE, ElementType.FIELD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Test6
public @interface Test7 {
@AliasFor(annotation = Test6.class)
String test3() default "test3";
}
@Test7(test3 = "override the method")
public static class Element4 {
}
@Test
public void test3() {
Test5 test5 = AnnotatedElementUtils.getMergedAnnotation(Element4.class, Test5.class);
Test6 test6 = AnnotatedElementUtils.getMergedAnnotation(Element4.class, Test6.class);
System.out.println(test5.toString());
System.out.println(test6.toString());
assertEquals("override the method", test6.test1());
assertEquals("override the method", test6.test2());
assertEquals("override the method", test6.test3());
assertEquals("override the method", test5.test1());
assertEquals("override the method", test5.test2());
assertEquals("override the method", test5.test3());
}
For more details, please read org.springframework.core.annotation.alisfors.AlisforsTests
.
Comment From: pivotal-issuemaster
@dragon-zhang Please sign the Contributor License Agreement!
Click here to manually synchronize the status of this Pull Request.
See the FAQ for frequently asked questions.
Comment From: pivotal-issuemaster
@dragon-zhang Thank you for signing the Contributor License Agreement!
Comment From: sbrannen
Related to #18852
Comment From: sbrannen
Please note that we would not introduce this new feature in 4.3.x
. Rather, if the team decides to introduce this, it would most likely be for 5.3.x or later.
In any case, please do not re-implement the feature against master
until the team has decided in favor of the feature.
Comment From: loongs-zhang
Thank you for your valuable suggestions. I will re-support the sub-features in the 5.3.x branch, so please re-examine them at that time
Comment From: sbrannen
In https://github.com/spring-projects/spring-framework/pull/25592#issuecomment-674548229, I asked that you:
not re-implement the feature against master until the team has decided in favor of the feature.
However, since you disregarded that and submitted #25857 anyway, I am closing this as a duplicate.
Comment From: loongs-zhang
I'm very sorry to misunderstood your idea. I thought you asked me to redevelop this feature based on the latest code.
------------------ 原始邮件 ------------------ 发件人: "spring-projects/spring-framework" <notifications@github.com>; 发送时间: 2020年11月2日(星期一) 下午5:41 收件人: "spring-projects/spring-framework"<spring-framework@noreply.github.com>; 抄送: "炼龙"<1936978077@qq.com>;"Mention"<mention@noreply.github.com>; 主题: Re: [spring-projects/spring-framework] Support multiple aliases via @AliasFor (#25592)
In #25592 (comment), I asked that you:
not re-implement the feature against master until the team has decided in favor of the feature.
However, since you disregarded that and submitted #25857 anyway, I am closing this as a duplicate.
— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub, or unsubscribe.