Spring工具类--AnnotatedElementUtils的使用

原文网址:Spring工具类--AnnotatedElementUtils的使用_IT利刃出鞘的博客-CSDN博客

简介

本文介绍Spring的AnnotatedElementUtils工具类的使用。

AnnotatedElementUtils与AnnotationUtils的区别:(AnnotatedElementUtils更强大)

|--------|-------------------------------------------------------|-------------------------|
| 特性 | AnnotatedElementUtils | AnnotationUtils |
| 功能 | 处理类、方法、字段等元素上的多个注解。 | 处理单个注解。 |
| 范围 | 可以处理多个注解,且能够处理多个注解之间相互关联的情况。 | 只能处理指定注解,无法处理多个注解之间的关系。 |
| 注解继承关系 | 能识别注解的继承关系。比如: 1.用的是子注解,但可以通过父注解类来判断。 2.通过父注解获得子注解上的值 | 不能识别注解的继承关系 |
| 注解搜索范围 | 搜索注解时会递归查找元素的父类和接口上的注解。 | 只搜索传入元素本身的注解。 |

用法

注解

java 复制代码
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 "";
}
java 复制代码
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 "";
}

控制器

java 复制代码
package com.example.controller;

import com.example.annotation.MyAnnotation;
import com.example.annotation.SubMyAnnotation;
import org.springframework.core.annotation.AnnotatedElementUtils;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/hello")
public class HelloController {
    @SubMyAnnotation(location = "location(my)")
    @RequestMapping("/test")
    public String test() {
        SubMyAnnotation subMyAnnotation = null;
        MyAnnotation myAnnotation = null;
        MyAnnotation myAnnotation1 = null;

        try {
            subMyAnnotation = AnnotationUtils.getAnnotation(this.getClass().getMethod("test"), SubMyAnnotation.class);
            myAnnotation = AnnotationUtils.getAnnotation(this.getClass().getMethod("test"), MyAnnotation.class);
            myAnnotation1 = AnnotatedElementUtils.getMergedAnnotation(this.getClass().getMethod("test"), MyAnnotation.class);
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }

        return "loation(sub):" + subMyAnnotation.location() + "\n" +
                "location:" + myAnnotation.location() + "\n" +
                "location:" + myAnnotation1.location();
    }
}

测试

前端访问:http://localhost:8080/hello/test

结果

复制代码
loation(sub):location(my)
location:
location:location(my)
相关推荐
孤廖3 分钟前
吃透 C++ 栈和队列:stack/queue/priority_queue 用法 + 模拟 + STL 标准实现对比
java·开发语言·数据结构·c++·人工智能·深度学习·算法
我命由我123455 分钟前
Android 对话框 - 对话框全屏显示(设置 Window 属性、使用自定义样式、继承 DialogFragment 实现、继承 Dialog 实现)
android·java·java-ee·android studio·android jetpack·android-studio·android runtime
驰羽11 分钟前
[GO]GORM中的Tag映射规则
开发语言·golang
Full Stack Developme15 分钟前
java.net 包详解
java·python·.net
非凡的世界32 分钟前
深入理解 PHP 框架里的设计模式
开发语言·设计模式·php
小龙报33 分钟前
《算法通关指南---C++编程篇(3)》
开发语言·c++·算法·visualstudio·学习方法·visual studio
一叶飘零_sweeeet34 分钟前
深入 Spring 内核:解密 15 种设计模式的实战应用与底层实现
java·spring·设计模式
凤山老林38 分钟前
排序算法:详解插入排序
java·开发语言·后端·算法·排序算法
彦楠42 分钟前
IDEA实用快捷键
java·ide·intellij-idea
豆沙沙包?1 小时前
2025年--Lc197-077. 排序链表(链表,尾插法)--Java版
java·数据结构·链表