Original URL: Spring Tool Class--Usage of AnnotatedElementUtils_IT Knives Out Blog-CSDN Blog
Introduction
This article introduces the use of Spring's AnnotatedElementUtils tool class.
The difference between AnnotatedElementUtils and AnnotationUtils: (AnnotatedElementUtils is more powerful)
characteristic |
AnnotatedElementUtils |
AnnotationUtils |
Function |
Handle multiple annotations on elements such as classes, methods, fields, etc. |
Process individual annotations. |
scope |
Can handle multiple annotations, and can handle the situation where multiple annotations are related to each other. |
Only specified annotations can be processed, and relationships between multiple annotations cannot be processed. |
Annotation inheritance relationship |
Can identify the inheritance relationship of annotations. for example: 1. Child annotations are used, but they can be judged by the parent annotation class. 2. Obtain the value of the child annotation through the parent annotation |
Unable to recognize inheritance relationship of annotations |
Annotation search scope |
Searching for annotations will recursively look for annotations on the element's parent class and interface. |
Only searches for annotations on the passed in element itself. |
usage
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 "";
}
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
The above is part of the article. For the convenience of maintenance, the full text has been transferred to this URL: Spring Tool Class-Usage of AnnotatedElementUtils-Self-Study Elf