method.isAnnotationPresent(Xxx.class)一直为null

java 复制代码
​​​​package com.dj.springtest.aspect;

import com.dj.springtest.annotation.RequireRoles;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.web.bind.annotation.RestController;

import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Map;

/**
 * User: ldj
 * Date: 2023/10/22
 * Time: 4:25
 * Description: No Description
 */
@Slf4j
@RunWith(SpringRunner.class)
@SpringBootTest
public class PermissionControl {

    @Autowired
    private ApplicationContext applicationContext;

    @Test
    public void test() {
        Map<String, Object> beans = applicationContext.getBeansWithAnnotation(RestController.class);
        for (Object bean : beans.values()) {
            //反射获取字节码对象
            Class clazz = bean.getClass();
            Method[] methods = clazz.getSuperclass().getDeclaredMethods();
            for (Method method : methods) {
                boolean isPresent = method.isAnnotationPresent(RequireRoles.class);
                log.info("是否存在@RequireRoles注解:{}", isPresent);
                if (isPresent) {
                    RequireRoles annotation = method.getAnnotation(RequireRoles.class);
                    log.info("roles:{}",Arrays.toString(annotation.roles()));
                }
            }
        }
    }

}
相关推荐
扶苏10022 分钟前
详解Vue3的自定义 Hooks
前端·javascript·vue.js
前路不黑暗@4 分钟前
Java项目:Java脚手架项目的地图服务(十)
java·数据库·spring boot·笔记·学习·spring cloud·maven
二级小助手10 分钟前
26年计算机二级web考试介绍【内附真题】
前端·计算机二级·全国计算机二级·web二级·二级web·前端二级·全国计算机web二级
014-code33 分钟前
Redisson 常用技巧
java·redis
java干货34 分钟前
明明删了数据,磁盘却满了?
java
之歆34 分钟前
HA 高可用集群指南
java·开发语言
lsx2024061 小时前
电子商务网站主机:选择与维护指南
开发语言
专注VB编程开发20年1 小时前
WebView2 处理跨域访问限制,Frame脚本执行,难度比CEF大10倍
前端·javascript·.net
wangluoqi1 小时前
c++ 逆元 小总结
开发语言·c++
BackCatK Chen1 小时前
第十五章 吃透C语言结构与数据形式:struct/union/typedef全解析
c语言·开发语言·数据结构·typedef·结构体·函数指针·联合体