当前位置:   article > 正文

method.isAnnotationPresent(Xxx.class)一直为null_method.isannotationpresent(passtoken.class)等于null

method.isannotationpresent(passtoken.class)等于null

  1. ​​​​package com.dj.springtest.aspect;
  2. import com.dj.springtest.annotation.RequireRoles;
  3. import lombok.extern.slf4j.Slf4j;
  4. import org.junit.Test;
  5. import org.junit.runner.RunWith;
  6. import org.springframework.beans.factory.annotation.Autowired;
  7. import org.springframework.boot.test.context.SpringBootTest;
  8. import org.springframework.context.ApplicationContext;
  9. import org.springframework.stereotype.Component;
  10. import org.springframework.test.context.junit4.SpringRunner;
  11. import org.springframework.web.bind.annotation.RestController;
  12. import java.lang.reflect.Method;
  13. import java.util.Arrays;
  14. import java.util.Map;
  15. /**
  16. * User: ldj
  17. * Date: 2023/10/22
  18. * Time: 4:25
  19. * Description: No Description
  20. */
  21. @Slf4j
  22. @RunWith(SpringRunner.class)
  23. @SpringBootTest
  24. public class PermissionControl {
  25. @Autowired
  26. private ApplicationContext applicationContext;
  27. @Test
  28. public void test() {
  29. Map<String, Object> beans = applicationContext.getBeansWithAnnotation(RestController.class);
  30. for (Object bean : beans.values()) {
  31. //反射获取字节码对象
  32. Class clazz = bean.getClass();
  33. Method[] methods = clazz.getSuperclass().getDeclaredMethods();
  34. for (Method method : methods) {
  35. boolean isPresent = method.isAnnotationPresent(RequireRoles.class);
  36. log.info("是否存在@RequireRoles注解:{}", isPresent);
  37. if (isPresent) {
  38. RequireRoles annotation = method.getAnnotation(RequireRoles.class);
  39. log.info("roles:{}",Arrays.toString(annotation.roles()));
  40. }
  41. }
  42. }
  43. }
  44. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/菜鸟追梦旅行/article/detail/240236
推荐阅读
相关标签
  

闽ICP备14008679号