{"id":203286,"date":"2025-05-22T13:25:16","date_gmt":"2025-05-22T05:25:16","guid":{"rendered":"https:\/\/server.hk\/cnblog\/203286\/"},"modified":"2025-05-22T13:25:16","modified_gmt":"2025-05-22T05:25:16","slug":"jdk-%e5%8a%a8%e6%80%81%e4%bb%a3%e7%90%86%e4%b8%ad%ef%bc%8cinvocationhandler-%e4%bd%bf%e7%94%a8%e9%9d%99%e6%80%81%e6%96%b9%e6%b3%95%e5%ad%98%e5%9c%a8%e4%bb%80%e4%b9%88%e9%9a%90%e6%82%a3%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/203286\/","title":{"rendered":"JDK \u52a8\u6001\u4ee3\u7406\u4e2d\uff0cInvocationHandler \u4f7f\u7528\u9759\u6001\u65b9\u6cd5\u5b58\u5728\u4ec0\u4e48\u9690\u60a3\uff1f"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>JDK \u52a8\u6001\u4ee3\u7406\u4e2d\uff0cInvocationHandler \u4f7f\u7528\u9759\u6001\u65b9\u6cd5\u5b58\u5728\u4ec0\u4e48\u9690\u60a3\uff1f<\/h1>\n<p><span style=\"font-size: 15px\">\u6709\u5fd7\u8005\uff0c\u4e8b\u7adf\u6210\uff01\u5982\u679c\u4f60\u5728\u5b66\u4e60<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\uff0c\u90a3\u4e48\u672c\u6587<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300aJDK \u52a8\u6001\u4ee3\u7406\u4e2d\uff0cInvocationHandler \u4f7f\u7528\u9759\u6001\u65b9\u6cd5\u5b58\u5728\u4ec0\u4e48\u9690\u60a3\uff1f\u300b<\/span>\uff0c\u5c31\u5f88\u9002\u5408\u4f60\uff01\u6587\u7ae0\u8bb2\u89e3\u7684\u77e5\u8bc6\u70b9\u4e3b\u8981\u5305\u62ec<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\uff0c\u82e5\u662f\u4f60\u5bf9\u672c\u6587\u611f\u5174\u8da3\uff0c\u6216\u8005\u662f\u60f3\u641e\u61c2\u5176\u4e2d\u67d0\u4e2a\u77e5\u8bc6\u70b9\uff0c\u5c31\u8bf7\u4f60\u7ee7\u7eed\u5f80\u4e0b\u770b\u5427~<\/span><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241104\/1730678982672810c6729c7.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>\u52a8\u6001\u4ee3\u7406\u4e2d\u7684 invocationhandler \u9759\u6001\u65b9\u6cd5\u9690\u60a3<\/strong><\/p>\n<p>\u5728 jdk \u52a8\u6001\u4ee3\u7406\u4e2d\uff0cinvocationhandler \u662f\u4ee3\u7406\u65b9\u6cd5\u8c03\u7528\u7684\u6838\u5fc3\u3002\u4f46\u662f\uff0c\u5f53 invocationhandler \u4e2d\u4f7f\u7528\u9759\u6001\u65b9\u6cd5\u65f6\uff0c\u53ef\u80fd\u5b58\u5728\u9690\u60a3\u3002<\/p>\n<p>\u4ee5\u4e0b\u4ee3\u7801\u793a\u4f8b\u4e2d\uff0cmyinvocationhandler \u4f7f\u7528\u9759\u6001\u53d8\u91cf targetservice\uff1a<\/p>\n<pre>class myinvocationhandler implements invocationhandler{\n    private static service targetservice;\n    ...\n}<\/pre>\n<p>\u4f7f\u7528\u9759\u6001\u53d8\u91cf\u7684\u597d\u5904\u662f\uff0c\u53ef\u4ee5\u5728\u6240\u6709\u4ee3\u7406\u5bf9\u8c61\u4e4b\u95f4\u5171\u4eab\u72b6\u6001\u3002\u4f46\u662f\uff0c\u5982\u679c\u60a8\u5728 invocationhandler \u7684\u9759\u6001\u65b9\u6cd5\u4e2d\u4fee\u6539 targetservice\uff0c\u5219\u6240\u6709\u4f7f\u7528\u8be5 invocationhandler \u7684\u4ee3\u7406\u5bf9\u8c61\u90fd\u5c06\u53d7\u5230\u5f71\u54cd\u3002\u8fd9\u53ef\u80fd\u4f1a\u5bfc\u81f4\u5e76\u53d1\u95ee\u9898\uff0c\u56e0\u4e3a\u591a\u4e2a\u7ebf\u7a0b\u53ef\u80fd\u4f1a\u540c\u65f6\u8bbf\u95ee targetservice\u3002<\/p>\n<p>\u4e3a\u4e86\u907f\u514d\u8fd9\u4e2a\u95ee\u9898\uff0c\u5efa\u8bae\u4e0d\u8981\u5728 invocationhandler \u7684\u9759\u6001\u65b9\u6cd5\u4e2d\u4f7f\u7528\u9759\u6001\u53d8\u91cf\u3002\u76f8\u53cd\uff0c\u53ef\u4ee5\u4f7f\u7528\u533f\u540d\u5185\u90e8\u7c7b\u6216\u5b9e\u4f8b\u65b9\u6cd5\u6765\u5b58\u50a8\u548c\u4fee\u6539\u72b6\u6001\uff1a<\/p>\n<pre>Service proxyInstance = (Service) Proxy.newProxyInstance(target.getClass().getClassLoader(),\n        target.getClass().getInterfaces(), new InvocationHandler() {\n            ...\n        });<\/pre>\n<p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u6cd5\uff0c\u6bcf\u4e2a\u4ee3\u7406\u5bf9\u8c61\u90fd\u6709\u81ea\u5df1\u72ec\u7acb\u7684\u72b6\u6001\uff0c\u4ece\u800c\u907f\u514d\u4e86\u5e76\u53d1\u95ee\u9898\u3002<\/p>\n<p>\u672c\u7bc7\u5173\u4e8e\u300aJDK \u52a8\u6001\u4ee3\u7406\u4e2d\uff0cInvocationHandler \u4f7f\u7528\u9759\u6001\u65b9\u6cd5\u5b58\u5728\u4ec0\u4e48\u9690\u60a3\uff1f\u300b\u7684\u4ecb\u7ecd\u5c31\u5230\u6b64\u7ed3\u675f\u5566\uff0c\u4f46\u662f\u5b66\u65e0\u6b62\u5883\uff0c\u60f3\u8981\u4e86\u89e3\u5b66\u4e60\u66f4\u591a\u5173\u4e8e\u6587\u7ae0\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u8bf7\u5173\u6ce8\u516c\u4f17\u53f7\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>JDK \u52a8\u6001\u4ee3\u7406\u4e2d\uff0cInvoca&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-203286","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/203286","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=203286"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/203286\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=203286"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=203286"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=203286"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}