原文网址:Spring工具类--AnnotatedElementUtils的使用_IT利刃出鞘的博客-CSDN博客
本文介绍Spring的AnnotatedElementUtils工具类的使用。
AnnotatedElementUtils与AnnotationUtils的区别:(AnnotatedElementUtils更强大)
| 特性 | AnnotatedElementUtils | AnnotationUtils |
| 功能 | 处理类、方法、字段等元素上的多个注解。 | 处理单个注解。 |
| 范围 | 可以处理多个注解,且能够处理多个注解之间相互关联的情况。 | 只能处理指定注解,无法处理多个注解之间的关系。 |
| 注解继承关系 | 能识别注解的继承关系。比如: 1.用的是子注解,但可以通过父注解类来判断。 2.通过父注解获得子注解上的值 | 不能识别注解的继承关系 |
| 注解搜索范围 | 搜索注解时会递归查找元素的父类和接口上的注解。 | 只搜索传入元素本身的注解。 |
注解
- package com.example.annotation;
-
- import org.springframework.core.annotation.AliasFor;
-
- import java.lang.annotation.*;
-
- @Retention(RetentionPolicy.RUNTIME)
- @Target({ElementType.METHOD, ElementType.TYPE})
- @Documented
- @Inherited
- public @interface MyAnnotation {
- @AliasFor(attribute = "location")
- String value() default "";
-
- @AliasFor(attribute = "value")
- String location() default "";
- }
- package com.example.annotation;
-
- import org.springframework.core.annotation.AliasFor;
-
- import java.lang.annotation.*;
-
- @Retention(RetentionPolicy.RUNTIME)
- @Target({ElementType.METHOD, ElementType.TYPE})
- @Documented
- @Inherited
- @MyAnnotation
- public @interface SubMyAnnotation {
- @AliasFor(annotation = MyAnnotation.class)
- String location() default "";
-
- // 这个不能写,只能有一个与父属性名同名的属性,否则报错
- // @AliasFor(annotation = MyAnnotation.class)
- // String value() default "";
- }
控制器
上边是文章的部分内容,为便于维护,全文已转移到此网址:Spring工具类-AnnotatedElementUtils的使用 - 自学精灵