{"id":180122,"date":"2020-10-29T15:25:53","date_gmt":"2020-10-29T07:25:53","guid":{"rendered":"http:\/\/4563.org\/?p=180122"},"modified":"2020-10-29T15:25:53","modified_gmt":"2020-10-29T07:25:53","slug":"%e5%a6%82%e4%bd%95%e6%89%ab%e6%8f%8f%e9%a1%b9%e7%9b%ae%e4%b8%ad%e6%89%80%e6%9c%89-feignclient-%e7%9a%84%e6%b3%a8%e8%a7%a3%e5%b1%9e%e6%80%a7%e4%bf%a1%e6%81%af","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=180122","title":{"rendered":"\u5982\u4f55\u626b\u63cf\u9879\u76ee\u4e2d\u6240\u6709 FeignClient \u7684\u6ce8\u89e3\u5c5e\u6027\u4fe1\u606f"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  \u5982\u4f55\u626b\u63cf\u9879\u76ee\u4e2d\u6240\u6709 FeignClient \u7684\u6ce8\u89e3\u5c5e\u6027\u4fe1\u606f               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : sunzy <\/span>  <span><i><\/i> 1<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<p>@FeignClient \u4e0d\u662f\u6709\u4e00\u4e9b\u5982 name \u3001url \u7b49\u7684\u5c5e\u6027\u4fe1\u606f\u4e48 \u901a\u8fc7<code>applicationContext.getBeansWithAnnotation(FeignClient.class)<\/code>\u53ef\u4ee5\u62ff\u5230 @FeignClient \u7684\u6240\u6709 bean \u4f46\u662f\u7531\u4e8e @FeignClient \u90fd\u662f interface\uff0c\u901a\u8fc7<code>getAnnotations()<\/code>\u62ff\u4e0d\u5230\u6ce8\u89e3\u7684\u5c5e\u6027\u4fe1\u606f\uff0c \u4ee3\u7801\u5982\u4e0b\uff0c\u6c42\u5927\u795e\u6307\u70b9\u4e00\u4e0b<\/p>\n<pre><code>        Map&lt;String,Object&gt; beans = applicationContext.getBeansWithAnnotation(FeignClient.class);         if (beans.size() &gt; 0){             for (Map.Entry&lt;String, Object&gt; entry : beans.entrySet()){                 String beanName = entry.getKey();                 Annotation[] annotations = entry.getValue().getClass().getAnnotations(); \/\/\u7a7a\u7684                 AnnotatedType[] annotatedTypes = entry.getValue().getClass().getAnnotatedInterfaces();                 for(AnnotatedType annotationType : annotatedTypes){                     Type type = annotationType.getType();                     Annotation[] typeAnnotations = type.getClass().getAnnotations();\u3000\/\/\u7a7a\u7684                     Annotation[] annotationTypeAnnotations = annotationType.getAnnotations(); \/\/\u7a7a\u7684                     Annotation[] declaredAnnotations = annotationType.getDeclaredAnnotations();\u3000\/\/\u7a7a\u7684                     System.out.println(annotationTypeAnnotations);                     System.out.println(declaredAnnotations);                 }             }         } <\/code><\/pre>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>8<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"3941566\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : madworks <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u4f60\u8fd9\u6837\u8bd5\u4e00\u4e0b\uff0centry.getValue().getClass().getAnnotation(FeignClient.class)                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3941567\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : sunzy <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @madworks \u8bd5\u4e86\uff0c\u662f null                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3941568\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : leejoker <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             beans.forEach((k, v) -&gt; {<br \/> try {<br \/> if (Proxy.isProxyClass(v.getClass())) {<br \/> InvocationHandler ih = Proxy.getInvocationHandler(v);<br \/> Field targetField = ih.getClass().getDeclaredField(&#8220;target&#8221;);<br \/> targetField.setAccessible(true);<\/p>\n<p> Target target = (Target) targetField.get(ih);<br \/> Field nameField = target.getClass().getDeclaredField(&#8220;name&#8221;);<br \/> nameField.setAccessible(true);<\/p>\n<p> String clientName = (String) nameField.get(target);<\/p>\n<p> Field urlField = target.getClass().getDeclaredField(&#8220;url&#8221;);<br \/> urlField.setAccessible(true);<br \/> String url = (String) urlField.get(target);<\/p>\n<p> System.out.println(&#8220;clientName: &#8221; + clientName);<br \/> System.out.println(&#8220;url: &#8221; + url);<br \/> }<br \/> } catch (Exception e) {<br \/> log.error(&#8220;\u5931\u8d25,exception={}&#8221;, ExceptionUtils.getStackTrace(e));<br \/> }<br \/> });                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3941569\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : leejoker <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u5927\u6982\u662f\u8fd9\u6837\uff0cspring \u91cc\u7684 bean \u662f\u901a\u8fc7\u4ee3\u7406\u7684\u65b9\u5f0f\u6ce8\u518c\u7684\uff0c\u9700\u8981\u7528\u4ee3\u7406\u7684\u65b9\u5f0f\u53bb\u83b7\u53d6\u3002<br \/>\u4ee3\u7801\u662f\u4ece\u6211\u5199\u7684\u5de5\u5177\u91cc\u622a\u53d6\u7684\uff1a https:\/\/github.com\/leejoker\/feign-dev-tool-starter                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3941570\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : sunzy <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @leejoker \u591a\u8c22\u5927\u4f6c                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3941571\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : DreamSpace <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @leejoker \u8001\u54e5\u53d1\u7684\u94fe\u63a5\u597d\u50cf\u662f\u4e2a private repository,\u770b\u4e0d\u5230\u5177\u4f53\u7684\u4ee3\u7801\u5b9e\u73b0                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3941572\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : leejoker <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u662f\u7684 XD\uff0c\u5df2\u7ecf\u6539\u4e86                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"3941573\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : leejoker <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @DreamSpace                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u4f55\u626b\u63cf\u9879\u76ee\u4e2d\u6240\u6709 FeignCl&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/180122"}],"collection":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=180122"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/180122\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=180122"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=180122"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=180122"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}