spring註解工具類AnnotatedElementUtils和AnnotationUtils

1、前言

  spring爲開發人員提供了兩個搜索註解的工具類,分別是AnnotatedElementUtils和AnnotationUtils。在使用的時候,老是傻傻分不清,什麼狀況下使用哪個。因而我作了以下的整理和總結。java

2、AnnotationUtils官方解釋

  功能

  用於處理註解,處理元註解,橋接方法(編譯器爲通用聲明生成)以及超級方法(用於可選註解繼承)的常規實用程序方法。請注意,JDK的內省工具自己並不提供此類的大多數功能。做爲運行時保留註解的通常規則(例如,用於事務控制,受權或服務公開),始終在此類上使用查找方法(例如,findAnnotation(Method,Class),getAnnotation(Method,Class)和getAnnotations(方法))而不是JDK中的普通註解查找方法。您仍然能夠在給定類級別的get查找(getAnnotation(Method,Class))和給定方法的整個繼承層次結構中的查找查找(findAnnotation(Method,Class))之間明確選擇。算法

  術語

  直接呈現,間接呈現和呈現的術語與AnnotatedElement的類級別javadoc中定義的含義相同(在Java 8中)。若是註解被聲明爲元素上存在的其餘註解上的元註解,則註解在元素上是元存在的。若是A在另外一個註解上直接存在或元存在,則註解A在另外一個註解上存在元。spring

  元註解支持

  大多數find *()方法和此類中的一些get *()方法都支持查找用做元註解的註解。有關詳細信息,請參閱此類中每一個方法的javadoc。對於在組合註解中使用屬性覆蓋的元註解的細粒度支持,請考慮使用AnnotatedElementUtils的更具體的方法。編程

  屬性別名

  此類中返回註解,註解數組或AnnotationAttributes的全部公共方法都透明地支持經過@AliasFor配置的屬性別名。有關詳細信息,請參閱各類synthesizeAnnotation *(..)方法。json

  搜索範圍

  一旦找到指定類型的第一個註解,此類中的方法使用的搜索算法將中止搜索註解。所以,將默默忽略指定類型的其餘註解。api

3、AnnotatedElementUtils官方解釋

  功能

   用於在AnnotatedElements上查找註解,元註解和可重複註解的常規實用程序方法。AnnotatedElementUtils爲Spring的元註解編程模型定義了公共API,並支持註解屬性覆蓋。若是您不須要支持註解屬性覆蓋,請考慮使用AnnotationUtils。請注意,JDK的內省工具自己不提供此類的功能。數組

  註解屬性覆蓋

  getMergedAnnotationAttributes(),getMergedAnnotation(),getAllMergedAnnotations(),getMergedRepeatableAnnotations(),findMergedAnnotationAttributes(),findMergedAnnotation(),findAllMergedAnnotations()和findMergedRepeatableAnnotations()的全部變體都支持組合註解中帶有屬性覆蓋的元註解的方法。app

  查找與獲取語義

  獲取語​​義(Get semantics)僅限於搜索AnnotatedElement上存在的註解(即本地聲明或繼承)或在AnnotatedElement上方的註解層次結構中聲明的註解。
  查找語義(Find semantics更加詳盡,提供了語義加上對如下內容的支持:工具

  • 若是帶註解的元素是類,則在接口上搜索
  • 若是帶註解的元素是類,則在超類上搜索
  • 解析橋接方法,若是帶註解的元素是方法
  • 若是帶註解的元素是方法,則在接口中搜索方法
  • 若是帶註解的元素是方法,則在超類中搜索方法

  支持@Inherited

  get語義以後的方法將遵循Java的@Inherited批註的約定,除了本地聲明的註解(包括自定義組合註解)將優於繼承註解。相反,查找語義以後的方法將徹底忽略@Inherited的存在,由於查找搜索算法手動遍歷類型和方法層次結構,從而隱式支持註解繼承而不須要@Inherited。測試

4、準備兩個測試的註解

@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 {};
}

5、測試例子

  父類擁有註解@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: null

ParentController 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: null

ParentController 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: null

ParentController 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: null

ParentController 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信息。

6、相關方法解釋

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

相關文章
相關標籤/搜索