spring注解工具類AnnotatedElementUtils和AnnotationUtils

一、前言

  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語義,包括單個注解和注解層次結構。

轉載于:https://www.cnblogs.com/hujunzheng/p/9790588.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/531217.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/531217.shtml
英文地址,請注明出處:http://en.pswp.cn/news/531217.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

windows系統nexus3安裝和配置

一、前言 為什么要在本地開發機器上安裝nexus?首先聲明公司內部是有自己的nexus倉庫,但是對上傳jar包做了限制,不能暢快的上傳自己測試包依賴。于是就自己在本地搭建了一個nexus私服,即可以使用公司nexus私服倉庫中的依賴&#xf…

Springmvc借助SimpleUrlHandlerMapping實現接口開關功能

一、接口開關功能 1、可配置化,依賴配置中心 2、接口訪問權限可控 3、springmvc不會掃描到,即不會直接的將接口暴露出去 二、接口開關使用場景 和業務沒什么關系,主要方便查詢系統中的一些狀態信息。比如系統的配置信息,中間件的狀…

log4j平穩升級到log4j2

一、前言 公司中的項目雖然已經用了很多的新技術了,但是日志的底層框架還是log4j,個人還是不喜歡用這個的。最近項目再生產環境上由于log4j引起了一場血案,于是決定升級到log4j2。 二、現象 雖然生產環境有多個結點分散高并發帶來的壓力&…

Springboot集成ES啟動報錯

報錯內容 None of the configured nodes are available elasticsearch.yml配置 cluster.name: ftest node.name: node-72 node.master: true node.data: true network.host: 112.122.245.212 http.port: 39200 transport.tcp.port: 39300 discovery.zen.ping.unicast.hosts: [&…

高效使用hibernate-validator校驗框架

一、前言 高效、合理的使用hibernate-validator校驗框架可以提高程序的可讀性,以及減少不必要的代碼邏輯。接下來會介紹一下常用一些使用方式。 二、常用注解說明 限制說明Null限制只能為nullNotNull限制必須不為nullAssertFalse限制必須為falseAssertTrue限制必須為…

kafka-manager配置和使用

kafka-manager配置 最主要配置就是用于kafka管理器狀態的zookeeper主機。這可以在conf目錄中的application.conf文件中找到。 kafka-manager.zkhosts"my.zookeeper.host.com:2181" 當然也可以聲明為zookeeper集群。 kafka-manager.zkhosts"my.zookeeper.host.co…

kafka告警簡單方案

一、前言 為什么要設計kafka告警方案?現成的監控項目百度一下一大堆,KafkaOffsetMonitor、KafkaManager、 Burrow等,具體參考:kafka的消息擠壓監控。由于本小組的項目使用的kafka集群并沒有被公司的kafka-manager管理,…

RedisCacheManager設置Value序列化器技巧

CacheManager基本配置 請參考博文:springboot2.0 redis EnableCaching的配置和使用 RedisCacheManager構造函數 /*** Construct a {link RedisCacheManager}.* * param redisOperations*/ SuppressWarnings("rawtypes") public RedisCacheManager(RedisOp…

Nginx配置以及域名轉發

工程中的nginx配置 #user nobody; worker_processes 24; error_log /home/xxx/opt/nginx/logs/error.log; pid /home/xxx/opt/nginx/run/nginx.pid;events {use epoll;worker_connections 102400; }http {include /home/xxx/opt/nginx/conf.d/mime.types;default_…

java接口簽名(Signature)實現方案續

一、前言 由于之前寫過的一片文章 (java接口簽名(Signature)實現方案 )收獲了很多好評,此次來說一下另一種簡單粗暴的簽名方案。相對于之前的簽名方案,對body、paramenter、path variable的獲取都做了簡化的處理。也就是說這種方式…

支付寶敏感信息解密

支付寶官方解密文檔&#xff1a;https://docs.alipay.com/mini/introduce/aes String response "小程序前端提交的";//1. 獲取驗簽和解密所需要的參數 Map<String, String> openapiResult JSON.parseObject(response,new TypeReference<Map<String, St…

HashMap 源碼閱讀

前言 之前讀過一些類的源碼&#xff0c;近來發現都忘了&#xff0c;再讀一遍整理記錄一下。這次讀的是 JDK 11 的代碼&#xff0c;貼上來的源碼會去掉大部分的注釋, 也會加上一些自己的理解。 Map 接口 這里提一下 Map 接口與1.8相比 Map接口又新增了幾個方法&#xff1a;   …

SpringMvc接口中轉設計(策略+模板方法)

一、前言 最近帶著兩個兄弟做支付寶小程序后端相關的開發&#xff0c;小程序首頁涉及到很多查詢的服務。小程序后端服務在我司屬于互聯網域&#xff0c;相關的查詢服務已經在核心域存在了&#xff0c;查詢這塊所要做的工作就是做接口中轉。參考了微信小程序的代碼&#xff0c;發…

SpringSecurity整合JWT

一、前言 最近負責支付寶小程序后端項目設計&#xff0c;這里主要分享一下用戶會話、接口鑒權的設計。參考過微信小程序后端的設計&#xff0c;會話需要依靠redis。相關的開發人員和我說依靠Redis并不是很靠譜&#xff0c;redis在業務高峰期不穩定&#xff0c;容易出現問題&…

Springboot定時任務原理及如何動態創建定時任務

一、前言 上周工作遇到了一個需求&#xff0c;同步多個省份銷號數據&#xff0c;解綁微信粉絲。分省定時將銷號數據放到SFTP服務器上&#xff0c;我需要開發定時任務去解析文件。因為是多省份&#xff0c;服務器、文件名規則、數據規則都不一定&#xff0c;所以要做成可配置是有…

轉載:ThreadPoolExecutor 源碼閱讀

前言 之前研究了一下如何使用ScheduledThreadPoolExecutor動態創建定時任務(Springboot定時任務原理及如何動態創建定時任務)&#xff0c;簡單了解了ScheduledThreadPoolExecutor相關源碼。今天看了同學寫的ThreadPoolExecutor 的源碼解讀&#xff0c;甚是NB&#xff0c;必須轉…

Spring BPP中優雅的創建動態代理Bean

一、前言 本文章所講并沒有基于Aspectj&#xff0c;而是直接通過Cglib以及ProxyFactoryBean去創建代理Bean。通過下面的例子&#xff0c;可以看出Cglib方式創建的代理Bean和ProxyFactoryBean創建的代理Bean的區別。 二、基本測試代碼 測試實體類&#xff0c;在BPP中創建BppTest…

使用pdfBox實現pdf轉圖片,解決中文方塊亂碼等問題

一、引入依賴 <dependency><groupId>org.apache.pdfbox</groupId><artifactId>fontbox</artifactId><version>2.0.13</version> </dependency> <dependency><groupId>org.apache.pdfbox</groupId><artif…

Spring異步調用原理及SpringAop攔截器鏈原理

一、Spring異步調用底層原理 開啟異步調用只需一個注解EnableAsync Target(ElementType.TYPE) Retention(RetentionPolicy.RUNTIME) Documented Import(AsyncConfigurationSelector.class) public interface EnableAsync {/*** Indicate the async annotation type to be detec…

線程池優化之充分利用線程池資源

一、前言 最近做了電子發票的需求&#xff0c;分省開票接口和發票下載接口都有一定的延遲。為了完成開票后自動將發票插入用戶微信卡包&#xff0c;目前的解決方案是利用線程池&#xff0c;將開票后插入卡包的任務&#xff08;輪詢分省發票接口&#xff0c;直到獲取到發票相關信…