I'm using SpringBoot 2.4.2. And I'm struggling with @AliasFor with custom annotation.
I implemented below custom annotation.
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface CustomAnnotation {
@AliasFor("aliasAttribute")
String value() default "";
@AliasFor("value")
String aliasAttribute() "";
}
And using it like this.
@CustomAnnoatation("test")
@Component
public class TestClass() {
// codes here
}
And this test code fails.
@SpringBootTest(classes = TestClass.class)
public class CustomAnnotationTest {
@Autowired
TestClass testClass;
@Test
public void valueTest1() {
Annotation annotation = testClass.getClass().getAnnotation(CustomAnnotation.class);
assertThat(((CustomAnnotation) annotation).value()).isEqualTo(((CustomAnnotation) annotation).aliasAttribute());
}
}
with message
org.opentest4j.AssertionFailedError:
Expecting:
<"">
to be equal to:
<"test">
I don't know why, anyone know?
AnnotationUtils.AnnotationUtils. Hence it still is used. Without using the spring capabilities for synthesized annotations you will not get the value for aliases because it is a spring feature.