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: 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信息。
六、相关方法解释
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语义,包括单个注解和注解层次结构。
spring注解工具类AnnotatedElementUtils和AnnotationUtils的更多相关文章
- Spring 注解(二)注解工具类 AnnotationUtils 和 AnnotatedElementUtils
Spring 注解(二)注解工具类 AnnotationUtils 和 AnnotatedElementUtils Spring 系列目录(https://www.cnblogs.com/binary ...
- Spring 注解(二)注解工具类
本文转载自Spring 注解(二)注解工具类 导语 首先回顾一下 AnnotationUtils 和 AnnotatedElementUtils 这两个注解工具类的用法: @Test @GetMapp ...
- 2015第30周三Spring常用工具类
文件资源操作 文件资源的操作是应用程序中常见的功能,如当上传一个文件后将其保存在特定目录下,从指定地址加载一个配置文件等等.我们一般使用 JDK 的 I/O 处理类完成这些操作,但对于一般的应用程序来 ...
- Spring常用工具类
Spring框架下自带了丰富的工具类,在我们开发时可以简化很多工作: 1.Resource访问文件资源: 具体有: ResourceUtils.getFile(url); FileSystemReso ...
- Spring boot 工具类静态属性注入及多环境配置
由于需要访问MongoDB,但是本地开发环境不能直接连接MongoDB,需要通过SecureCRT使用127.0.0.2本地IP代理.但是程序部署到线上生产环境后,是可以直接访问MongoDB的,因此 ...
- Spring web 工具类 WebApplicationContextUtils
概述 Spring web 的工具类 WebApplicationContextUtils 位于包 org.springframework.web.context.support 是访问一个Servl ...
- Spring的工具类StringUtils使用
我们经常会对字符串进行操作,spring已经实现了常用的处理功能.我们可以使用org.springframework.util.StringUtils 工具类帮我们处理字符串. 工具类整理如下: ...
- Spring 常用工具类
1) 请求工具类 org.springframework.web.bind.ServletRequestUtils //取请求参数的整数值: public static Integer getIntP ...
- Spring常用工具类(ApplicationContextAware、DisposableBean、InitializingBean)
原创作品,出自 "晓风残月xj" 博客,欢迎转载,转载时请务必注明出处(http://blog.csdn.net/xiaofengcanyuexj). 由于各种原因,可能存在诸多不 ...
随机推荐
- python - 发送html格式的邮件
import smtplibfrom email.mime.multipart import MIMEMultipartfrom email.mime.text import MIMETextfrom ...
- Nginx详解二十五:Nginx架构篇之Nginx常见的问题
Nginx常见的问题 1.相同server_name多个虚拟主机优先级访问,是按读取文件的优先级来排序 在/opt/app/下准备3个code文件夹,下面放入3个html文件,里面的内容分别是code ...
- MySQL5.7.20报错Access denied for user 'root'@'localhost' (using password: NO)
在centos6.8上源码安装了MySQL5.7.20,进入mysql的时候报错如下: 解决办法如下: 在mysql的配置文件内加入: vim /etc/my.cnf skip-grant-tabl ...
- 爬虫(猫眼电影+校花网+github+今日头条+拉钩)
Requests+正则表达式爬取猫眼TOP100榜电影信息 MARK:将信息写入文件解决乱码方法,开启进程池秒爬. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 ...
- WARN util.NativeCodeLoader: Unable to load native-hadoop library for your platform... using builtin-java classes where applicable Exception in thread "main" java.io.IOException: No FileSystem for sc F
1.执行脚本程序报如下所示的错误: [hadoop@slaver1 script_hadoop]$ hadoop jar web_click_mr_hive.jar com.bie.hive.mr.C ...
- 关于yum网络版仓库(本地yum仓库的安装配置,如果没网了,做一个局域网内的yum仓库)
2017-11-13 22:49:48 1:两种方式: a.每一台机器都配一个本地文件系统上的yum仓库 file:///packege/path/ b.在局域网内部配置一台节点(server-b ...
- openresty用naxsi防xss、SQL注入
下载naxsi wget https://github.com/nbs-system/naxsi/archive/untagged-afabfc163946baa8036f.tar.gz tar zx ...
- Json对象处理.将对象处理成dic数组.
var parser = new JsonConfigurationFileParser();var dict = parser.Parse("json"); using Newt ...
- (原创)C# 压缩解压那些事儿
吐槽: 搜狗推广API的报告服务太坑爹了!!! 搜狗推广API的报告服务太坑爹了!!! 搜狗推广API的报告服务太坑爹了!!! 搜狗的太垃圾了,获取下来的压缩包使用正常方式无法解压!!没有专门的API ...
- Pytorch学习笔记
非线性回归问题的参数求解,反向求导基本流程.Variable 计算时, 它在后台一步步默默地搭建着一个庞大的系统, 叫做计算图, computational graph. 这个图将所有的计算步骤 (节 ...