美文网首页
AOP导致自定义注解失效

AOP导致自定义注解失效

作者: 毛于晏 | 来源:发表于2020-05-19 12:15 被阅读0次

    1.问题复现

    //从spring上线文获取实现TestService接口的实现类
    Map<String, TestService> beansOfType = ContextUtil.getApplicationContext().getBeansOfType(TestService.class);
    //通过isAnnotationPresent(class) 方法过滤出想要的注解
    List<Method> collect = methodsList.stream().filter(method -> method.isAnnotationPresent(Test.class)).collect(Collectors.toList());
    //结果List == null
    

    2.解决方法

    //使用AnnotationUtils.findAnnotation()方法 获取原本被代理的原对象
    List<Method> methodsList = methodsList.stream().filter(method -> AnnotationUtils.findAnnotation(method, Test.class) != null).collect(Collectors.toList());
    

    3.问题原因

    aop实现代理类, 获取的是代理类中的注解, 而代理类中注解直接没有生成,导致失败
    如图:


    图片.png

    相关文章

      网友评论

          本文标题:AOP导致自定义注解失效

          本文链接:https://www.haomeiwen.com/subject/twavohtx.html