{"id":203259,"date":"2025-05-22T11:49:33","date_gmt":"2025-05-22T03:49:33","guid":{"rendered":"https:\/\/server.hk\/cnblog\/203259\/"},"modified":"2025-05-22T11:49:33","modified_gmt":"2025-05-22T03:49:33","slug":"%e4%bd%bf%e7%94%a8%e9%9d%99%e6%80%81-invocationhandler-%e7%9a%84%e6%bd%9c%e5%9c%a8%e9%9a%90%e6%82%a3%ef%bc%9a%e4%b8%ba%e4%bb%80%e4%b9%88%e5%9c%a8%e5%8a%a8%e6%80%81%e4%bb%a3%e7%90%86%e4%b8%ad%e5%bb%ba","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/203259\/","title":{"rendered":"\u4f7f\u7528\u9759\u6001 InvocationHandler \u7684\u6f5c\u5728\u9690\u60a3\uff1a\u4e3a\u4ec0\u4e48\u5728\u52a8\u6001\u4ee3\u7406\u4e2d\u5efa\u8bae\u4f7f\u7528\u533f\u540d\u5185\u90e8\u7c7b\uff1f"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>\u4f7f\u7528\u9759\u6001 InvocationHandler \u7684\u6f5c\u5728\u9690\u60a3\uff1a\u4e3a\u4ec0\u4e48\u5728\u52a8\u6001\u4ee3\u7406\u4e2d\u5efa\u8bae\u4f7f\u7528\u533f\u540d\u5185\u90e8\u7c7b\uff1f<\/h1>\n<p>\u672c\u7bc7\u6587\u7ae0\u5411\u5927\u5bb6\u4ecb\u7ecd\u300a\u4f7f\u7528\u9759\u6001 InvocationHandler \u7684\u6f5c\u5728\u9690\u60a3\uff1a\u4e3a\u4ec0\u4e48\u5728\u52a8\u6001\u4ee3\u7406\u4e2d\u5efa\u8bae\u4f7f\u7528\u533f\u540d\u5185\u90e8\u7c7b\uff1f\u300b\uff0c\u4e3b\u8981\u5305\u62ec\uff0c\u5177\u6709\u4e00\u5b9a\u7684\u53c2\u8003\u4ef7\u503c\uff0c\u9700\u8981\u7684\u670b\u53cb\u53ef\u4ee5\u53c2\u8003\u4e00\u4e0b\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241103\/17306107516727063fbd54c.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>\u52a8\u6001\u4ee3\u7406\u4e2d\u7684\u9759\u6001 invocationhandler<\/strong><\/p>\n<p>\u5728\u4f7f\u7528 jdk \u52a8\u6001\u4ee3\u7406\u65f6\uff0c\u67d0\u4e9b\u573a\u666f\u9700\u8981\u5728 invocationhandler \u4e2d\u4f7f\u7528\u9759\u6001\u65b9\u6cd5\u3002\u7136\u800c\uff0c\u8fd9\u79cd\u505a\u6cd5\u53ef\u80fd\u4f1a\u5e26\u6765\u6f5c\u5728\u7684\u9690\u60a3\u3002<\/p>\n<p>\u4ee5\u4e0b\u662f\u4f7f\u7528\u9759\u6001\u65b9\u6cd5\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<pre>class myinvocationhandler implements invocationhandler {\n    private static service targetservice;\n\n    @override\n    public object invoke(object proxy, method method, object[] args) throws throwable {\n        return null;\n    }\n\n    public static service getproxyservice(service target) {\n        targetservice = target;\n        classloader contextclassloader = thread.currentthread().getcontextclassloader();\n        class&lt;?&gt;[] interfaces = targetservice.getclass().getinterfaces();\n        return (service) proxy.newproxyinstance(contextclassloader, interfaces, new myinvocationhandler());\n    }\n}<\/pre>\n<p>\u4f7f\u7528\u9759\u6001\u65b9\u6cd5\u7684\u6f5c\u5728\u9690\u60a3\u5728\u4e8e\uff1a<\/p>\n<ul>\n<li><strong>\u7ebf\u7a0b\u5b89\u5168\u95ee\u9898\uff1a<\/strong>\u9759\u6001\u65b9\u6cd5\u662f\u5168\u5c40\u53ef\u8bbf\u95ee\u7684\uff0c\u65e0\u6cd5\u786e\u4fdd\u5728\u591a\u7ebf\u7a0b\u73af\u5883\u4e0b\u7684\u7ebf\u7a0b\u5b89\u5168\u3002<\/li>\n<li><strong>\u8d44\u6e90\u6cc4\u6f0f\uff1a<\/strong>\u9759\u6001\u5f15\u7528\u4f1a\u4e00\u76f4\u6301\u6709 targetservice \u5bf9\u8c61\uff0c\u5373\u4f7f\u4ee3\u7406\u5bf9\u8c61\u5df2\u5931\u6548\uff0ctargetservice \u4e5f\u4f1a\u4e00\u76f4\u5b58\u5728\u5185\u5b58\u4e2d\uff0c\u53ef\u80fd\u5bfc\u81f4\u5185\u5b58\u6cc4\u6f0f\u3002<\/li>\n<li><strong>\u4e0d\u7075\u6d3b\uff1a<\/strong>\u9759\u6001\u65b9\u6cd5\u4e0d\u80fd\u52a8\u6001\u4fee\u6539 targetservice \u5bf9\u8c61\uff0c\u9650\u5236\u4e86\u52a8\u6001\u4ee3\u7406\u7684\u7075\u6d3b\u6027\u3002<\/li>\n<\/ul>\n<p>\u56e0\u6b64\uff0c\u63a8\u8350\u4f7f\u7528\u533f\u540d\u5185\u90e8\u7c7b\u6765\u5b9e\u73b0 invocationhandler\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<pre>Service proxyInstance = (Service) Proxy.newProxyInstance(target.getClass().getClassLoader(),\n        target.getClass().getInterfaces(), new InvocationHandler() {\n            @Override\n            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {\n                if (\"add\".equals(method.getName())) {\n                    System.out.println(\"HELLOIDEA\");\n                }\n                return null;\n            }\n        });<\/pre>\n<p>\u4eca\u5929\u5173\u4e8e\u300a\u4f7f\u7528\u9759\u6001 InvocationHandler \u7684\u6f5c\u5728\u9690\u60a3\uff1a\u4e3a\u4ec0\u4e48\u5728\u52a8\u6001\u4ee3\u7406\u4e2d\u5efa\u8bae\u4f7f\u7528\u533f\u540d\u5185\u90e8\u7c7b\uff1f\u300b\u7684\u5185\u5bb9\u4ecb\u7ecd\u5c31\u5230\u6b64\u7ed3\u675f\uff0c\u5982\u679c\u6709\u4ec0\u4e48\u7591\u95ee\u6216\u8005\u5efa\u8bae\uff0c\u53ef\u4ee5\u5728\u516c\u4f17\u53f7\u4e0b\u591a\u591a\u56de\u590d\u4ea4\u6d41\uff1b\u6587\u4e2d\u82e5\u6709\u4e0d\u6b63\u4e4b\u5904\uff0c\u4e5f\u5e0c\u671b\u56de\u590d\u7559\u8a00\u4ee5\u544a\u77e5\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4f7f\u7528\u9759\u6001 InvocationH&#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-203259","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/203259","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=203259"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/203259\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=203259"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=203259"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=203259"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}