Home >Java >javaTutorial >How to use the @AliasFor annotation in SpringBoot
It can be annotated to two attributes of the custom annotation, indicating that the two attributes are aliases for each other, that is to say, this The two attributes actually have the same meaning.
One of the attribute names must be "value"
No matter which attribute name is specified to set the attribute value, the other attribute name is also the same attribute value, or the default attribute name.
If both specify attribute values, the required values must be the same, otherwise an error will be reported.
Use concisely. After using it like this, @MyAnno(location="shanghai") can be written directly as: @MyAnno("shanghai");
The reason for this function:
If the custom annotation has an attribute, and the attribute is named to reflect its meaning, the caller must write the attribute and then set it every time he uses the custom annotation, which is a little troublesome.
Annotation
package com.example.annotation; import org.springframework.core.annotation.AliasFor; import java.lang.annotation.*; @Retention(RetentionPolicy.RUNTIME) @Target({ElementType.METHOD, ElementType.TYPE}) @Documented @Inherited public @interface MyAnnotation { @AliasFor(attribute = "location") String value() default ""; @AliasFor(attribute = "value") String location() default ""; }
Controller
package com.example.controller; import com.example.annotation.MyAnnotation; import org.springframework.core.annotation.AnnotationUtils; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("/hello") public class HelloController { @MyAnnotation(value = "location") /*//下边这两种写法结果与上边是相同的 @MyAnnotation("location") @MyAnnotation(location = "location")*/ @RequestMapping("/test1") public String test1() { MyAnnotation myAnnotation = null; try { myAnnotation = AnnotationUtils.getAnnotation(this.getClass().getMethod("test1"), MyAnnotation.class); } catch (NoSuchMethodException e) { e.printStackTrace(); } return "value:" + myAnnotation.value() + ";loation:" + myAnnotation.location(); } }
Test
Front-end access: http://localhost:8080/hello/test1
Front-end result (value and location are both the same value)
value:location; loation:location
The reading and writing of the attribute value of the child annotation is actually Read and write the attribute value of the parent annotation. (For inherited properties)
At this time, only inherited property values can be read and written.
Annotations
package com.example.annotation; import org.springframework.core.annotation.AliasFor; import java.lang.annotation.*; @Retention(RetentionPolicy.RUNTIME) @Target({ElementType.METHOD, ElementType.TYPE}) @Documented @Inherited public @interface MyAnnotation { @AliasFor(attribute = "location") String value() default ""; @AliasFor(attribute = "value") String location() default ""; }
package com.example.annotation; import org.springframework.core.annotation.AliasFor; import java.lang.annotation.*; @Retention(RetentionPolicy.RUNTIME) @Target({ElementType.METHOD, ElementType.TYPE}) @Documented @Inherited @MyAnnotation public @interface SubMyAnnotation { @AliasFor(annotation = MyAnnotation.class) String location() default ""; // 这个不能写,只能有一个与父属性名同名的属性,否则报错 // @AliasFor(annotation = MyAnnotation.class) // String value() default ""; }
Controller
package com.example.controller; import com.example.annotation.MyAnnotation; import com.example.annotation.SubMyAnnotation; import org.springframework.core.annotation.AnnotatedElementUtils; import org.springframework.core.annotation.AnnotationUtils; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("/hello") public class HelloController { @SubMyAnnotation(location = "location(my)") @RequestMapping("/test") public String test() { SubMyAnnotation subMyAnnotation = null; MyAnnotation myAnnotation = null; MyAnnotation myAnnotation1 = null; try { subMyAnnotation = AnnotationUtils.getAnnotation(this.getClass().getMethod("test"), SubMyAnnotation.class); myAnnotation = AnnotationUtils.getAnnotation(this.getClass().getMethod("test"), MyAnnotation.class); myAnnotation1 = AnnotatedElementUtils.getMergedAnnotation(this.getClass().getMethod("test"), MyAnnotation.class); } catch (NoSuchMethodException e) { e.printStackTrace(); } return "loation(sub):" + subMyAnnotation.location() + "\n" + "location:" + myAnnotation.location() + "\n" + "location:" + myAnnotation1.location(); } }
Test
Front-end access: http://localhost:8080/hello/test
Result
##loation(sub):location(my)Usage 3: Inherit the attributes of the parent annotation and rewrite the attribute nameIntroduction The reading and writing of the attribute value of the child annotation is actually the reading and writing of the attribute value of the parent annotation. (For overridden attributes) No matter which attribute name is specified to set the attribute value, the other attribute name also has the same attribute value, and the default attribute name cannot be used. If both specify attribute values, the required values must be the same, otherwise an error will be reported. Codelocation:
location:location(my)
Annotations
package com.example.annotation; import org.springframework.core.annotation.AliasFor; import java.lang.annotation.*; @Retention(RetentionPolicy.RUNTIME) @Target({ElementType.METHOD, ElementType.TYPE}) @Documented @Inherited public @interface MyAnnotation { @AliasFor(attribute = "location") String value() default ""; @AliasFor(attribute = "value") String location() default ""; }
package com.example.annotation; import org.springframework.core.annotation.AliasFor; import java.lang.annotation.*; @Retention(RetentionPolicy.RUNTIME) @Target({ElementType.METHOD, ElementType.TYPE}) @Documented @Inherited @MyAnnotation public @interface SubMyAnnotation { @AliasFor(attribute = "value", annotation = MyAnnotation.class) String subValue() default ""; @AliasFor(attribute = "location", annotation = MyAnnotation.class) String subLocation() default ""; // subLocation属性写成下边这两种结果是一样的 // @AliasFor(attribute = "value", annotation = MyAnnotation.class) // String subLocation() default ""; // @AliasFor(value = "location", annotation = MyAnnotation.class) // String subLocation() default ""; // }
Controller
package com.example.controller; import com.example.annotation.MyAnnotation; import com.example.annotation.SubMyAnnotation; import org.springframework.core.annotation.AnnotatedElementUtils; import org.springframework.core.annotation.AnnotationUtils; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("/hello") public class HelloController { @SubMyAnnotation(subValue = "subLocation") // @SubMyAnnotation(subLocation = "subLocation") //这个与上边结果相同 // @SubMyAnnotation("subLocation") //缺省属性名会报错 @RequestMapping("/test") public String test() { SubMyAnnotation subMyAnnotation = null; MyAnnotation myAnnotation = null; MyAnnotation myAnnotation1 = null; try { subMyAnnotation = AnnotationUtils.getAnnotation(this.getClass().getMethod("test"), SubMyAnnotation.class); myAnnotation = AnnotationUtils.getAnnotation(this.getClass().getMethod("test"), MyAnnotation.class); myAnnotation1 = AnnotatedElementUtils.getMergedAnnotation(this.getClass().getMethod("test"), MyAnnotation.class); } catch (NoSuchMethodException e) { e.printStackTrace(); } return "subValue:" + subMyAnnotation.subValue() + ";subLoation:" + subMyAnnotation.subLocation() + "\n" + "value:" + myAnnotation.value() + ";location:" + myAnnotation.location() + "\n" + "value:" + myAnnotation1.value() + ";location:" + myAnnotation1.location(); } }
Test
Front-end access: http://localhost:8080/hello/testResult
##subValue:subLocation;subLoation:subLocationvalue:;location:value:subLocation;location:subLocation
The above is the detailed content of How to use the @AliasFor annotation in SpringBoot. For more information, please follow other related articles on the PHP Chinese website!