一、前言
spring為開發人員提供了兩個搜索注解的工具類,分別是AnnotatedElementUtils和AnnotationUtils。在使用的時候,總是傻傻分不清,什么情況下使用哪一個。于是我做了如下的整理和總結。
二、AnnotationUtils官方解釋
功能
用于處理注解,處理元注解,橋接方法(編譯器為通用聲明生成)以及超級方法(用于可選注解繼承)的常規實用程序方法。請注意,JDK的內省工具本身并不提供此類的大多數功能。作為運行時保留注解的一般規則(例如,用于事務控制,授權或服務公開),始終在此類上使用查找方法(例如,findAnnotation(Method,Class),getAnnotation(Method,Class)和getAnnotations(方法))而不是JDK中的普通注解查找方法。您仍然可以在給定類級別的get查找(getAnnotation(Method,Class))和給定方法的整個繼承層次結構中的查找查找(findAnnotation(Method,Class))之間明確選擇。
術語
直接呈現,間接呈現和呈現的術語與AnnotatedElement的類級別javadoc中定義的含義相同(在Java 8中)。如果注解被聲明為元素上存在的其他注解上的元注解,則注解在元素上是元存在的。如果A在另一個注解上直接存在或元存在,則注解A在另一個注解上存在元。
元注解支持
大多數find *()方法和此類中的一些get *()方法都支持查找用作元注解的注解。有關詳細信息,請參閱此類中每個方法的javadoc。對于在組合注解中使用屬性覆蓋的元注解的細粒度支持,請考慮使用AnnotatedElementUtils的更具體的方法。
屬性別名
此類中返回注解,注解數組或AnnotationAttributes的所有公共方法都透明地支持通過@AliasFor配置的屬性別名。有關詳細信息,請參閱各種synthesizeAnnotation *(..)方法。
搜索范圍
一旦找到指定類型的第一個注解,此類中的方法使用的搜索算法將停止搜索注解。因此,將默默忽略指定類型的其他注解。
三、AnnotatedElementUtils官方解釋
功能
? 用于在AnnotatedElements上查找注解,元注解和可重復注解的常規實用程序方法。AnnotatedElementUtils為Spring的元注解編程模型定義了公共API,并支持注解屬性覆蓋。如果您不需要支持注解屬性覆蓋,請考慮使用AnnotationUtils。請注意,JDK的內省工具本身不提供此類的功能。
注解屬性覆蓋
getMergedAnnotationAttributes(),getMergedAnnotation(),getAllMergedAnnotations(),getMergedRepeatableAnnotations(),findMergedAnnotationAttributes(),findMergedAnnotation(),findAllMergedAnnotations()和findMergedRepeatableAnnotations()的所有變體都支持組合注解中帶有屬性覆蓋的元注解的方法。
查找與獲取語義
獲取語??義(Get semantics)僅限于搜索AnnotatedElement上存在的注解(即本地聲明或繼承)或在AnnotatedElement上方的注解層次結構中聲明的注解。
查找語義(Find semantics)更加詳盡,提供了語義加上對以下內容的支持:
- 如果帶注解的元素是類,則在接口上搜索
- 如果帶注解的元素是類,則在超類上搜索
- 解析橋接方法,如果帶注解的元素是方法
- 如果帶注解的元素是方法,則在接口中搜索方法
- 如果帶注解的元素是方法,則在超類中搜索方法
支持@Inherited
get語義之后的方法將遵循Java的@Inherited批注的約定,除了本地聲明的注解(包括自定義組合注解)將優于繼承注解。相反,查找語義之后的方法將完全忽略@Inherited的存在,因為查找搜索算法手動遍歷類型和方法層次結構,從而隱式支持注解繼承而不需要@Inherited。
四、準備兩個測試的注解
@Target({ElementType.METHOD, ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) @Documented @interface RequestMapping {String name() default "";@AliasFor("path")String[] value() default {};@AliasFor("value")String[] path() default {}; }@Target({ElementType.METHOD, ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) @Documented @RequestMapping @interface PostMapping {@AliasFor(annotation = RequestMapping.class)String name() default "";@AliasFor(annotation = RequestMapping.class)String[] value() default {};@AliasFor(annotation = RequestMapping.class)String[] path() default {}; }
五、測試例子
父類擁有注解@RequestMapping,子類沒有注解
public class AnnotationTest {public static void main(String[] args) {System.out.println("ParentController getAnnotation @RequestMapping: " + AnnotationUtils.getAnnotation(ParentController.class, RequestMapping.class));System.out.println("ChildController getAnnotation @RequestMapping: " +AnnotationUtils.getAnnotation(ChildController.class, RequestMapping.class));System.out.println();System.out.println("ParentController findAnnotation @RequestMapping: " + AnnotationUtils.findAnnotation(ParentController.class, RequestMapping.class));System.out.println("ParentController findAnnotation @RequestMapping: " + AnnotationUtils.findAnnotation(ChildController.class, RequestMapping.class));System.out.println();System.out.println("ParentController isAnnotated @RequestMapping: " + AnnotatedElementUtils.isAnnotated(ParentController.class, RequestMapping.class));System.out.println("ParentController getMergedAnnotation @RequestMapping: " + AnnotatedElementUtils.getMergedAnnotation(ParentController.class, RequestMapping.class));System.out.println("ChildController isAnnotated @RequestMapping: " + AnnotatedElementUtils.isAnnotated(ChildController.class, RequestMapping.class));System.out.println("ChildController getMergedAnnotation @RequestMapping: " + AnnotatedElementUtils.getMergedAnnotation(ChildController.class, RequestMapping.class));System.out.println();System.out.println("ParentController hasAnnotation @RequestMapping: " + AnnotatedElementUtils.hasAnnotation(ParentController.class, RequestMapping.class));System.out.println("ParentController findMergedAnnotation @RequestMapping: " + AnnotatedElementUtils.findMergedAnnotation(ParentController.class, RequestMapping.class));System.out.println("ChildController hasAnnotation @RequestMapping: " + AnnotatedElementUtils.hasAnnotation(ChildController.class, RequestMapping.class));System.out.println("ChildController findMergedAnnotation @RequestMapping: " + AnnotatedElementUtils.findMergedAnnotation(ChildController.class, RequestMapping.class));} }@RequestMapping(value = "parent/controller") class ParentController { }class ChildController extends ParentController { }
輸出結果如下。
ParentController getAnnotation @RequestMapping: @com.hjzgg.apigateway.test.service.main.RequestMapping(name=, value=[parent/controller], path=[parent/controller]) ChildController getAnnotation @RequestMapping: nullParentController findAnnotation @RequestMapping: @com.hjzgg.apigateway.test.service.main.RequestMapping(name=, value=[parent/controller], path=[parent/controller]) ChildController findAnnotation @RequestMapping: @com.hjzgg.apigateway.test.service.main.RequestMapping(name=, value=[parent/controller], path=[parent/controller])ParentController isAnnotated @RequestMapping: true ParentController getMergedAnnotation @RequestMapping: @com.hjzgg.apigateway.test.service.main.RequestMapping(name=, value=[parent/controller], path=[parent/controller]) ChildController isAnnotated @RequestMapping: false ChildController getMergedAnnotation @RequestMapping: nullParentController hasAnnotation @RequestMapping: true ParentController findMergedAnnotation @RequestMapping: @com.hjzgg.apigateway.test.service.main.RequestMapping(name=, value=[parent/controller], path=[parent/controller]) ChildController hasAnnotation @RequestMapping: true ChildController findMergedAnnotation @RequestMapping: @com.hjzgg.apigateway.test.service.main.RequestMapping(name=, value=[parent/controller], path=[parent/controller])
父類沒有注解,子類擁有注解@RequestMapping
輸出結果如下。
ParentController getAnnotation @RequestMapping: null ChildController getAnnotation @RequestMapping: @com.hjzgg.apigateway.test.service.main.RequestMapping(name=, value=[parent/controller], path=[parent/controller])ParentController findAnnotation @RequestMapping: null ChildController findAnnotation @RequestMapping: @com.hjzgg.apigateway.test.service.main.RequestMapping(name=, value=[parent/controller], path=[parent/controller])ParentController isAnnotated @RequestMapping: false ParentController getMergedAnnotation @RequestMapping: null ChildController isAnnotated @RequestMapping: true ChildController getMergedAnnotation @RequestMapping: @com.hjzgg.apigateway.test.service.main.RequestMapping(name=, value=[parent/controller], path=[parent/controller])ParentController hasAnnotation @RequestMapping: false ParentController findMergedAnnotation @RequestMapping: null ChildController hasAnnotation @RequestMapping: true ChildController findMergedAnnotation @RequestMapping: @com.hjzgg.apigateway.test.service.main.RequestMapping(name=, value=[parent/controller], path=[parent/controller])
父類擁有注解@PostMapping,子類沒有注解
public class AnnotationTest {public static void main(String[] args) {System.out.println("ParentController getAnnotation @RequestMapping: " + AnnotationUtils.getAnnotation(ParentController.class, RequestMapping.class));System.out.println("ChildController getAnnotation @RequestMapping: " +AnnotationUtils.getAnnotation(ChildController.class, RequestMapping.class));System.out.println("ParentController getAnnotation @PostMapping: " + AnnotationUtils.getAnnotation(ParentController.class, PostMapping.class));System.out.println("ChildController getAnnotation @PostMapping: " +AnnotationUtils.getAnnotation(ChildController.class, PostMapping.class));System.out.println();System.out.println("ParentController findAnnotation @RequestMapping: " + AnnotationUtils.findAnnotation(ParentController.class, RequestMapping.class));System.out.println("ParentController findAnnotation @RequestMapping: " + AnnotationUtils.findAnnotation(ChildController.class, RequestMapping.class));System.out.println("ParentController findAnnotation @PostMapping: " + AnnotationUtils.findAnnotation(ParentController.class, PostMapping.class));System.out.println("ParentController findAnnotation @PostMapping: " + AnnotationUtils.findAnnotation(ChildController.class, PostMapping.class));System.out.println();System.out.println("ParentController isAnnotated @RequestMapping: " + AnnotatedElementUtils.isAnnotated(ParentController.class, RequestMapping.class));System.out.println("ParentController getMergedAnnotation @RequestMapping: " + AnnotatedElementUtils.getMergedAnnotation(ParentController.class, RequestMapping.class));System.out.println("ParentController isAnnotated @PostMapping: " + AnnotatedElementUtils.isAnnotated(ParentController.class, PostMapping.class));System.out.println("ParentController getMergedAnnotation @PostMapping: " + AnnotatedElementUtils.getMergedAnnotation(ParentController.class, PostMapping.class));System.out.println("ChildController isAnnotated @RequestMapping: " + AnnotatedElementUtils.isAnnotated(ChildController.class, RequestMapping.class));System.out.println("ChildController getMergedAnnotation @RequestMapping: " + AnnotatedElementUtils.getMergedAnnotation(ChildController.class, RequestMapping.class));System.out.println("ChildController isAnnotated @PostMapping: " + AnnotatedElementUtils.isAnnotated(ChildController.class, PostMapping.class));System.out.println("ChildController getMergedAnnotation @PostMapping: " + AnnotatedElementUtils.getMergedAnnotation(ChildController.class, PostMapping.class));System.out.println();System.out.println("ParentController hasAnnotation @RequestMapping: " + AnnotatedElementUtils.hasAnnotation(ParentController.class, RequestMapping.class));System.out.println("ParentController findMergedAnnotation @RequestMapping: " + AnnotatedElementUtils.findMergedAnnotation(ParentController.class, RequestMapping.class));System.out.println("ParentController hasAnnotation @PostMapping: " + AnnotatedElementUtils.hasAnnotation(ParentController.class, PostMapping.class));System.out.println("ParentController findMergedAnnotation @PostMapping: " + AnnotatedElementUtils.findMergedAnnotation(ParentController.class, PostMapping.class));System.out.println("ChildController hasAnnotation @RequestMapping: " + AnnotatedElementUtils.hasAnnotation(ChildController.class, RequestMapping.class));System.out.println("ChildController findMergedAnnotation @RequestMapping: " + AnnotatedElementUtils.findMergedAnnotation(ChildController.class, RequestMapping.class));System.out.println("ChildController hasAnnotation @PostMapping: " + AnnotatedElementUtils.hasAnnotation(ChildController.class, PostMapping.class));System.out.println("ChildController findMergedAnnotation @PostMapping: " + AnnotatedElementUtils.findMergedAnnotation(ChildController.class, PostMapping.class));} }@PostMapping(value = "parent/controller") class ParentController { }class ChildController extends ParentController { }
輸出結果如下。
ParentController getAnnotation @RequestMapping: @com.hjzgg.apigateway.test.service.main.RequestMapping(name=, value=[], path=[]) ChildController getAnnotation @RequestMapping: null ParentController getAnnotation @PostMapping: @com.hjzgg.apigateway.test.service.main.PostMapping(name=, value=[parent/controller], path=[parent/controller]) ChildController getAnnotation @PostMapping: nullParentController findAnnotation @RequestMapping: @com.hjzgg.apigateway.test.service.main.RequestMapping(name=, value=[], path=[]) ChildController findAnnotation @RequestMapping: @com.hjzgg.apigateway.test.service.main.RequestMapping(name=, value=[], path=[]) ParentController findAnnotation @PostMapping: @com.hjzgg.apigateway.test.service.main.PostMapping(name=, value=[parent/controller], path=[parent/controller]) ChildController findAnnotation @PostMapping: @com.hjzgg.apigateway.test.service.main.PostMapping(name=, value=[parent/controller], path=[parent/controller])ParentController isAnnotated @RequestMapping: true ParentController getMergedAnnotation @RequestMapping: @com.hjzgg.apigateway.test.service.main.RequestMapping(name=, value=[parent/controller], path=[parent/controller]) ParentController isAnnotated @PostMapping: true ParentController getMergedAnnotation @PostMapping: @com.hjzgg.apigateway.test.service.main.PostMapping(name=, value=[parent/controller], path=[parent/controller]) ChildController isAnnotated @RequestMapping: false ChildController getMergedAnnotation @RequestMapping: null ChildController isAnnotated @PostMapping: false ChildController getMergedAnnotation @PostMapping: nullParentController hasAnnotation @RequestMapping: true ParentController findMergedAnnotation @RequestMapping: @com.hjzgg.apigateway.test.service.main.RequestMapping(name=, value=[parent/controller], path=[parent/controller]) ParentController hasAnnotation @PostMapping: true ParentController findMergedAnnotation @PostMapping: @com.hjzgg.apigateway.test.service.main.PostMapping(name=, value=[parent/controller], path=[parent/controller]) ChildController hasAnnotation @RequestMapping: true ChildController findMergedAnnotation @RequestMapping: @com.hjzgg.apigateway.test.service.main.RequestMapping(name=, value=[parent/controller], path=[parent/controller]) ChildController hasAnnotation @PostMapping: true ChildController findMergedAnnotation @PostMapping: @com.hjzgg.apigateway.test.service.main.PostMapping(name=, value=[parent/controller], path=[parent/controller])
父類沒有注解,子類擁有注解@PostMapping
輸出結果如下。
ParentController getAnnotation @RequestMapping: null ChildController getAnnotation @RequestMapping: @com.hjzgg.apigateway.test.service.main.RequestMapping(name=, value=[], path=[]) ParentController getAnnotation @PostMapping: null ChildController getAnnotation @PostMapping: @com.hjzgg.apigateway.test.service.main.PostMapping(name=, value=[parent/controller], path=[parent/controller])ParentController findAnnotation @RequestMapping: null ChildController findAnnotation @RequestMapping: @com.hjzgg.apigateway.test.service.main.RequestMapping(name=, value=[], path=[]) ParentController findAnnotation @PostMapping: null ChildController findAnnotation @PostMapping: @com.hjzgg.apigateway.test.service.main.PostMapping(name=, value=[parent/controller], path=[parent/controller])ParentController isAnnotated @RequestMapping: false ParentController getMergedAnnotation @RequestMapping: null ParentController isAnnotated @PostMapping: false ParentController getMergedAnnotation @PostMapping: null ChildController isAnnotated @RequestMapping: true ChildController getMergedAnnotation @RequestMapping: @com.hjzgg.apigateway.test.service.main.RequestMapping(name=, value=[parent/controller], path=[parent/controller]) ChildController isAnnotated @PostMapping: true ChildController getMergedAnnotation @PostMapping: @com.hjzgg.apigateway.test.service.main.PostMapping(name=, value=[parent/controller], path=[parent/controller])ParentController hasAnnotation @RequestMapping: false ParentController findMergedAnnotation @RequestMapping: null ParentController hasAnnotation @PostMapping: false ParentController findMergedAnnotation @PostMapping: null ChildController hasAnnotation @RequestMapping: true ChildController findMergedAnnotation @RequestMapping: @com.hjzgg.apigateway.test.service.main.RequestMapping(name=, value=[parent/controller], path=[parent/controller]) ChildController hasAnnotation @PostMapping: true ChildController findMergedAnnotation @PostMapping: @com.hjzgg.apigateway.test.service.main.PostMapping(name=, value=[parent/controller], path=[parent/controller])
@PostMapping 注有 @RequestMapping,AnnotationUtils和AnnotatedElementUtils區別
public class AnnotationTest {public static void main(String[] args) {System.out.println("ParentController getAnnotation @RequestMapping: " + AnnotationUtils.getAnnotation(ParentController.class, RequestMapping.class));System.out.println("ChildController getAnnotation @RequestMapping: " + AnnotationUtils.getAnnotation(ChildController.class, RequestMapping.class));System.out.println("ParentController findAnnotation @RequestMapping: " + AnnotationUtils.findAnnotation(ParentController.class, RequestMapping.class));System.out.println("ChildController findAnnotation @RequestMapping: " + AnnotationUtils.findAnnotation(ChildController.class, RequestMapping.class));System.out.println();System.out.println("ParentController getMergedAnnotation @RequestMapping: " + AnnotatedElementUtils.getMergedAnnotation(ParentController.class, RequestMapping.class));System.out.println("ChildController getMergedAnnotation @RequestMapping: " + AnnotatedElementUtils.getMergedAnnotation(ChildController.class, RequestMapping.class));System.out.println("ParentController findMergedAnnotation @RequestMapping: " + AnnotatedElementUtils.findMergedAnnotation(ParentController.class, RequestMapping.class));System.out.println("ChildController findMergedAnnotation @RequestMapping: " + AnnotatedElementUtils.findMergedAnnotation(ChildController.class, RequestMapping.class));} }@RequestMapping(name = "parent", path="parent/controller") class ParentController { }@PostMapping(name="child", value = "child/controller", consume = "application/json") class ChildController extends ParentController { }@Target({ElementType.METHOD, ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) @Documented @interface RequestMapping {String name() default "";@AliasFor("path")String[] value() default {};@AliasFor("value")String[] path() default {};String[] consume() default {}; }@Target({ElementType.METHOD, ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) @Documented @RequestMapping @interface PostMapping {@AliasFor(annotation = RequestMapping.class)String name() default "";@AliasFor(annotation = RequestMapping.class)String[] value() default {};@AliasFor(annotation = RequestMapping.class)String[] path() default {};@AliasFor(annotation = RequestMapping.class)String[] consume() default ""; }
輸出結果如下。
ParentController getAnnotation @RequestMapping: @com.hjzgg.apigateway.test.service.main.RequestMapping(name=parent, value=[parent/controller], path=[parent/controller], consume=[]) ChildController getAnnotation @RequestMapping: @com.hjzgg.apigateway.test.service.main.RequestMapping(name=, value=[], path=[], consume=[]) ParentController findAnnotation @RequestMapping: @com.hjzgg.apigateway.test.service.main.RequestMapping(name=parent, value=[parent/controller], path=[parent/controller], consume=[]) ChildController findAnnotation @RequestMapping: @com.hjzgg.apigateway.test.service.main.RequestMapping(name=, value=[], path=[], consume=[])ParentController getMergedAnnotation @RequestMapping: @com.hjzgg.apigateway.test.service.main.RequestMapping(name=parent, value=[parent/controller], path=[parent/controller], consume=[]) ChildController getMergedAnnotation @RequestMapping: @com.hjzgg.apigateway.test.service.main.RequestMapping(name=child, value=[child/controller], path=[child/controller], consume=[application/json]) ParentController findMergedAnnotation @RequestMapping: @com.hjzgg.apigateway.test.service.main.RequestMapping(name=parent, value=[parent/controller], path=[parent/controller], consume=[]) ChildController findMergedAnnotation @RequestMapping: @com.hjzgg.apigateway.test.service.main.RequestMapping(name=child, value=[child/controller], path=[child/controller], consume=[application/json])
@PostMapping 和 @RequestMapping各自獨立,AnnotationUtils和AnnotatedElementUtils區別
public class AnnotationTest {public static void main(String[] args) {System.out.println("ParentController getAnnotation @RequestMapping: " + AnnotationUtils.getAnnotation(ParentController.class, RequestMapping.class));System.out.println("ChildController getAnnotation @RequestMapping: " + AnnotationUtils.getAnnotation(ChildController.class, RequestMapping.class));System.out.println("ParentController findAnnotation @RequestMapping: " + AnnotationUtils.findAnnotation(ParentController.class, RequestMapping.class));System.out.println("ChildController findAnnotation @RequestMapping: " + AnnotationUtils.findAnnotation(ChildController.class, RequestMapping.class));System.out.println();System.out.println("ParentController getMergedAnnotation @RequestMapping: " + AnnotatedElementUtils.getMergedAnnotation(ParentController.class, RequestMapping.class));System.out.println("ChildController getMergedAnnotation @RequestMapping: " + AnnotatedElementUtils.getMergedAnnotation(ChildController.class, RequestMapping.class));System.out.println("ParentController findMergedAnnotation @RequestMapping: " + AnnotatedElementUtils.findMergedAnnotation(ParentController.class, RequestMapping.class));System.out.println("ChildController findMergedAnnotation @RequestMapping: " + AnnotatedElementUtils.findMergedAnnotation(ChildController.class, RequestMapping.class));} }@RequestMapping(name = "parent", path="parent/controller") class ParentController { }@PostMapping(name="child", value = "child/controller", consume = "application/json") class ChildController extends ParentController { }@Target({ElementType.METHOD, ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) @Documented @interface RequestMapping {String name() default "";@AliasFor("path")String[] value() default {};@AliasFor("value")String[] path() default {};String[] consume() default {}; }@Target({ElementType.METHOD, ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) @Documented @interface PostMapping {@AliasFor(annotation = RequestMapping.class)String name() default "";@AliasFor(annotation = RequestMapping.class)String[] value() default {};@AliasFor(annotation = RequestMapping.class)String[] path() default {};@AliasFor(annotation = RequestMapping.class)String[] consume() default ""; }
輸出結果如下。
ParentController getAnnotation @RequestMapping: @com.hjzgg.apigateway.test.service.main.RequestMapping(name=parent, value=[parent/controller], path=[parent/controller], consume=[]) ChildController getAnnotation @RequestMapping: null ParentController findAnnotation @RequestMapping: @com.hjzgg.apigateway.test.service.main.RequestMapping(name=parent, value=[parent/controller], path=[parent/controller], consume=[]) ChildController findAnnotation @RequestMapping: @com.hjzgg.apigateway.test.service.main.RequestMapping(name=parent, value=[parent/controller], path=[parent/controller], consume=[])ParentController getMergedAnnotation @RequestMapping: @com.hjzgg.apigateway.test.service.main.RequestMapping(name=parent, value=[parent/controller], path=[parent/controller], consume=[]) ChildController getMergedAnnotation @RequestMapping: null ParentController findMergedAnnotation @RequestMapping: @com.hjzgg.apigateway.test.service.main.RequestMapping(name=parent, value=[parent/controller], path=[parent/controller], consume=[]) ChildController findMergedAnnotation @RequestMapping: @com.hjzgg.apigateway.test.service.main.RequestMapping(name=parent, value=[parent/controller], path=[parent/controller], consume=[])
此時@PostMapping沒注有@RequestMapping,所以AnnotationUtis.getAnnotation()和AnnotatedElementUtils.getMergeAnnotation()方法獲取@RequestMapping信息為空,對應的find*()方法獲取到的都是父類@RequestMapping信息。
六、相關方法解釋
AnnotationUtils.getAnnotation
從提供的AnnotatedElement獲取annotationType的單個Annotation,其中注解在AnnotatedElement上存在或元存在。請注意,此方法僅支持單級元注解。要支持任意級別的元注解,請使用findAnnotation(AnnotatedElement,Class)。
AnnotationUtils.findAnnotation
在提供的AnnotatedElement上查找annotationType的單個Annotation。如果注解不直接出現在提供的元素上,則將搜索元注解。
AnnotatedElementUtils.isAnnotated
確定在提供的AnnotatedElement上或指定元素上方的注解層次結構中是否存在指定annotationType的注解。如果此方法返回true,則getMergedAnnotationAttributes方法將返回非null值。
AnnotatedElementUtils.hasAnnotation
確定指定的annotationType的注解是否在提供的AnnotatedElement上或在指定元素上方的注解層次結構中可用。如果此方法返回true,則findMergedAnnotationAttributes方法將返回非null值。
AnnotatedElementUtils.getMergedAnnotation
在提供的元素上方的注解層次結構中獲取指定注解類型的第一個注解,將注解的屬性與注解層次結構的較低級別中的注解的匹配屬性合并,并將結果合成回指定注解類型的注解。完全支持@AliasFor語義,包括單個注解和注解層次結構。此方法委托給getMergedAnnotationAttributes(AnnotatedElement,Class)和AnnotationUtils.synthesizeAnnotation(Map,Class,AnnotatedElement)。
AnnotatedElementUtils.findMergedAnnotation
在提供的元素上方的注解層次結構中查找指定注解類型的第一個注解,將注解的屬性與注解層次結構的較低級別中的注解的匹配屬性合并,并將結果合成回指定注解類型的注解。完全支持@AliasFor語義,包括單個注解和注解層次結構。