V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
sunzy
V2EX  ›  Java

如何扫描项目中所有 FeignClient 的注解属性信息

  •  
  •   sunzy · 2020-10-28 11:59:20 +08:00 · 1606 次点击
    这是一个创建于 1273 天前的主题,其中的信息可能已经有所发展或是发生改变。

    @FeignClient 不是有一些如 name 、url 等的属性信息么 通过applicationContext.getBeansWithAnnotation(FeignClient.class)可以拿到 @FeignClient 的所有 bean 但是由于 @FeignClient 都是 interface,通过getAnnotations()拿不到注解的属性信息, 代码如下,求大神指点一下

            Map<String,Object> beans = applicationContext.getBeansWithAnnotation(FeignClient.class);
            if (beans.size() > 0){
                for (Map.Entry<String, Object> entry : beans.entrySet()){
                    String beanName = entry.getKey();
                    Annotation[] annotations = entry.getValue().getClass().getAnnotations(); //空的
                    AnnotatedType[] annotatedTypes = entry.getValue().getClass().getAnnotatedInterfaces();
                    for(AnnotatedType annotationType : annotatedTypes){
                        Type type = annotationType.getType();
                        Annotation[] typeAnnotations = type.getClass().getAnnotations(); //空的
                        Annotation[] annotationTypeAnnotations = annotationType.getAnnotations(); //空的
                        Annotation[] declaredAnnotations = annotationType.getDeclaredAnnotations(); //空的
                        System.out.println(annotationTypeAnnotations);
                        System.out.println(declaredAnnotations);
                    }
                }
            }
    
    8 条回复    2020-10-28 15:24:15 +08:00
    madworks
        1
    madworks  
       2020-10-28 12:22:11 +08:00
    你这样试一下,entry.getValue().getClass().getAnnotation(FeignClient.class)
    sunzy
        2
    sunzy  
    OP
       2020-10-28 14:12:29 +08:00
    @madworks 试了,是 null
    leejoker
        3
    leejoker  
       2020-10-28 14:33:28 +08:00
    beans.forEach((k, v) -> {
    try {
    if (Proxy.isProxyClass(v.getClass())) {
    InvocationHandler ih = Proxy.getInvocationHandler(v);
    Field targetField = ih.getClass().getDeclaredField("target");
    targetField.setAccessible(true);

    Target target = (Target) targetField.get(ih);
    Field nameField = target.getClass().getDeclaredField("name");
    nameField.setAccessible(true);

    String clientName = (String) nameField.get(target);

    Field urlField = target.getClass().getDeclaredField("url");
    urlField.setAccessible(true);
    String url = (String) urlField.get(target);

    System.out.println("clientName: " + clientName);
    System.out.println("url: " + url);
    }
    } catch (Exception e) {
    log.error("失败,exception={}", ExceptionUtils.getStackTrace(e));
    }
    });
    leejoker
        4
    leejoker  
       2020-10-28 14:40:17 +08:00
    大概是这样,spring 里的 bean 是通过代理的方式注册的,需要用代理的方式去获取。
    代码是从我写的工具里截取的: https://github.com/leejoker/feign-dev-tool-starter
    sunzy
        5
    sunzy  
    OP
       2020-10-28 14:45:45 +08:00
    @leejoker 多谢大佬
    DreamSpace
        6
    DreamSpace  
       2020-10-28 15:16:57 +08:00
    @leejoker 老哥发的链接好像是个 private repository,看不到具体的代码实现
    leejoker
        7
    leejoker  
       2020-10-28 15:21:39 +08:00
    是的 XD,已经改了
    leejoker
        8
    leejoker  
       2020-10-28 15:24:15 +08:00
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3569 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 44ms · UTC 00:46 · PVG 08:46 · LAX 17:46 · JFK 20:46
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.