Skip to content
Advertisement

@AliasFor doesn’t work on attribute in custom annotation

I’m using SpringBoot 2.4.2. And I’m struggling with @AliasFor with custom annotation.

I implemented below custom annotation.

JavaScript

And using it like this.

JavaScript

And this test code fails.

JavaScript

with message

JavaScript

I don’t know why, anyone know?

Advertisement

Answer

An annotation is static metadata for a class, field etc. so Spring cannot change anything about it. To make features as @AliasFor possible Spring uses, what they call, synthesized annotations. For those to be used/detected you need to utilize the Spring internals to obtain that synthesized annotation and for the @AliasFor to work. For this use AnnotationUtils.findAnnotation (Spring uses that internally as well).

@AliasFor is a Spring feature so without using the Spring components this won’t work.

Your test method is basically the same as

JavaScript

Both this test and your test will fail, because they simply don’t use the Spring infrastructure for detecting annotations and apply the features of Spring.

When using AnnotationUtils.findAnnotation the test will pass.

JavaScript

The testStandardJava will fail, the testWithSpring will pass because it uses the proper mechanisms.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement