{"id":202768,"date":"2025-05-22T08:53:04","date_gmt":"2025-05-22T00:53:04","guid":{"rendered":"https:\/\/server.hk\/cnblog\/202768\/"},"modified":"2025-05-22T08:53:04","modified_gmt":"2025-05-22T00:53:04","slug":"%e5%a6%82%e4%bd%95%e5%9c%a8-java-%e4%b8%ad%e4%bd%bf%e7%94%a8%e5%8f%8d%e5%b0%84%e5%a4%84%e7%90%86%e5%bc%82%e5%b8%b8%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/202768\/","title":{"rendered":"\u5982\u4f55\u5728 Java \u4e2d\u4f7f\u7528\u53cd\u5c04\u5904\u7406\u5f02\u5e38\uff1f"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>\u5982\u4f55\u5728 Java \u4e2d\u4f7f\u7528\u53cd\u5c04\u5904\u7406\u5f02\u5e38\uff1f<\/h1>\n<p>\u5c0f\u4f19\u4f34\u4eec\u6709\u6ca1\u6709\u89c9\u5f97\u5b66\u4e60<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\u5f88\u6709\u610f\u601d\uff1f\u6709\u610f\u601d\u5c31\u5bf9\u4e86\uff01\u4eca\u5929\u5c31\u7ed9\u5927\u5bb6\u5e26\u6765<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u5982\u4f55\u5728 Java \u4e2d\u4f7f\u7528\u53cd\u5c04\u5904\u7406\u5f02\u5e38\uff1f\u300b<\/span>\uff0c\u4ee5\u4e0b\u5185\u5bb9\u5c06\u4f1a\u6d89\u53ca\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\uff0c\u82e5\u662f\u5728\u5b66\u4e60\u4e2d\u5bf9\u5176\u4e2d\u90e8\u5206\u77e5\u8bc6\u70b9\u6709\u7591\u95ee\uff0c\u6216\u8bb8\u770b\u4e86\u672c\u6587\u5c31\u80fd\u5e2e\u5230\u4f60\uff01<\/p>\n<p>\u662f\u7684\uff0cJava \u4e2d\u53ef\u4ee5\u4f7f\u7528\u53cd\u5c04\u5904\u7406\u5f02\u5e38\u3002\u5177\u4f53\u6b65\u9aa4\u5982\u4e0b\uff1a\u83b7\u53d6\u5f85\u5904\u7406\u65b9\u6cd5\u3002\u521b\u5efa\u4e00\u4e2a\u5b9e\u73b0 InvocationHandler \u63a5\u53e3\u7684\u81ea\u5b9a\u4e49\u5904\u7406\u7a0b\u5e8f\u3002\u521b\u5efa\u4e00\u4e2a\u52a8\u6001\u4ee3\u7406\uff0c\u8be5\u4ee3\u7406\u4f7f\u7528\u81ea\u5b9a\u4e49\u5904\u7406\u7a0b\u5e8f\u5904\u7406\u65b9\u6cd5\u8c03\u7528\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241026\/1729939935671cc9df6f740.jpg\" class=\"aligncenter\"><\/p>\n<h2>\u5982\u4f55\u5728 Java \u4e2d\u4f7f\u7528\u53cd\u5c04\u5904\u7406\u5f02\u5e38<\/h2>\n<p><strong>\u7b80\u4ecb<\/strong><\/p>\n<p>\u53cd\u5c04\u662f Java \u4e2d\u4e00\u9879\u5f3a\u5927\u7684\u529f\u80fd\uff0c\u5b83\u5141\u8bb8\u6211\u4eec\u68c0\u67e5\u548c\u4fee\u6539\u7c7b\u7684\u8fd0\u884c\u65f6\u884c\u4e3a\u3002\u5b83\u8fd8\u53ef\u4ee5\u7528\u6765\u5904\u7406\u5f02\u5e38\uff0c\u4ece\u800c\u4e3a\u6211\u4eec\u7684\u5e94\u7528\u7a0b\u5e8f\u63d0\u4f9b\u66f4\u5927\u7684\u7075\u6d3b\u6027\u3002<\/p>\n<p><strong>\u4f7f\u7528\u53cd\u5c04\u5904\u7406\u5f02\u5e38<\/strong><\/p>\n<p>\u8981\u4f7f\u7528\u53cd\u5c04\u5904\u7406\u5f02\u5e38\uff0c\u6211\u4eec\u9700\u8981\u4f7f\u7528 <code>java.lang.reflect<\/code> \u5305\u4e2d\u7684\u4e24\u4e2a\u4e3b\u8981\u7684\u7c7b\uff1a<code>Method<\/code> \u548c <code>InvocationHandler<\/code>\u3002<\/p>\n<ol>\n<li><strong>\u83b7\u53d6\u5f85\u5904\u7406\u7684\u65b9\u6cd5\uff1a<\/strong> \u4f7f\u7528 <code>Method<\/code> \u7c7b\u83b7\u53d6\u8981\u5904\u7406\u5f02\u5e38\u7684\u65b9\u6cd5\u3002\u4f8b\u5982\uff1a<\/li>\n<\/ol>\n<pre>Method method = object.getClass().getMethod(\"methodName\");<\/pre>\n<ol>\n<li><strong>\u521b\u5efa InvocationHandler\uff1a<\/strong> \u5b9e\u73b0 <code>InvocationHandler<\/code> \u63a5\u53e3\u521b\u5efa\u4e00\u4e2a\u81ea\u5b9a\u4e49\u5904\u7406\u7a0b\u5e8f\u3002\u8be5\u5904\u7406\u7a0b\u5e8f\u5c06\u8d1f\u8d23\u5904\u7406\u65b9\u6cd5\u8c03\u7528\u4e2d\u7684\u5f02\u5e38\u3002\u4f8b\u5982\uff1a<\/li>\n<\/ol>\n<pre>InvocationHandler handler = new InvocationHandler() {\n    @Override\n    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {\n        try {\n            return method.invoke(object, args);\n        } catch (Throwable t) {\n            \/\/ \u5904\u7406\u5f02\u5e38\n            return null;\n        }\n    }\n};<\/pre>\n<ol>\n<li><strong>\u521b\u5efa\u52a8\u6001\u4ee3\u7406\uff1a<\/strong> \u4f7f\u7528 <code>Proxy<\/code> \u7c7b\u521b\u5efa\u4e00\u4e2a\u52a8\u6001\u4ee3\u7406\uff0c\u8be5\u4ee3\u7406\u5c06\u4f7f\u7528\u6211\u4eec\u7684 <code>InvocationHandler<\/code> \u6765\u5904\u7406\u65b9\u6cd5\u8c03\u7528\u3002\u4f8b\u5982\uff1a<\/li>\n<\/ol>\n<pre>Object proxy = Proxy.newProxyInstance(ClassLoader, new Class[] { method.getDeclaringClass() }, handler);<\/pre>\n<p><strong>\u5b9e\u6218\u6848\u4f8b<\/strong><\/p>\n<p>\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u65b9\u6cd5 <code>doSomething()<\/code> \u53ef\u80fd\u629b\u51fa <code>NullPointerException<\/code>\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u53cd\u5c04\u6765\u5904\u7406\u8fd9\u4e2a\u5f02\u5e38\u5e76\u8fd4\u56de\u4e00\u4e2a\u9ed8\u8ba4\u503c\uff1a<\/p>\n<pre>Object proxy = Proxy.newProxyInstance(ClassLoader, new Class[] { SomeClass.class }, new InvocationHandler() {\n    @Override\n    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {\n        try {\n            return method.invoke(someObject, args);\n        } catch (NullPointerException e) {\n            return \"Default value\";\n        }\n    }\n});\n\nObject result = proxy.doSomething();<\/pre>\n<p>\u5728\u4ee5\u4e0a\u793a\u4f8b\u4e2d\uff0c\u5f53 <code>doSomething()<\/code> \u65b9\u6cd5\u88ab\u8c03\u7528\u65f6\uff0c\u5b83\u5c06\u7531\u6211\u4eec\u7684 <code>InvocationHandler<\/code> \u62e6\u622a\u3002\u5982\u679c\u8be5\u65b9\u6cd5\u629b\u51fa <code>NullPointerException<\/code>\uff0c\u5219\u6211\u4eec\u7684\u5904\u7406\u7a0b\u5e8f\u5c06\u8fd4\u56de\u4e00\u4e2a\u9ed8\u8ba4\u503c &#8220;Default value&#8221;\u3002<\/p>\n<p><strong>\u7ed3\u8bba<\/strong><\/p>\n<p>\u901a\u8fc7\u4f7f\u7528\u53cd\u5c04\uff0c\u6211\u4eec\u53ef\u4ee5\u5904\u7406 Java \u4e2d\u7684\u5f02\u5e38\u5e76\u4e3a\u6211\u4eec\u7684\u5e94\u7528\u7a0b\u5e8f\u63d0\u4f9b\u66f4\u5927\u7684\u7075\u6d3b\u6027\u3002\u6211\u4eec\u53ef\u4ee5\u52a8\u6001\u5730\u62e6\u622a\u65b9\u6cd5\u8c03\u7528\u5e76\u6839\u636e\u9700\u8981\u5904\u7406\u5f02\u5e38\u3002<\/p>\n<p>\u7406\u8bba\u8981\u638c\u63e1\uff0c\u5b9e\u64cd\u4e0d\u80fd\u843d\uff01\u4ee5\u4e0a\u5173\u4e8e\u300a\u5982\u4f55\u5728 Java \u4e2d\u4f7f\u7528\u53cd\u5c04\u5904\u7406\u5f02\u5e38\uff1f\u300b\u7684\u8be6\u7ec6\u4ecb\u7ecd\uff0c\u5927\u5bb6\u90fd\u638c\u63e1\u4e86\u5427\uff01\u5982\u679c\u60f3\u8981\u7ee7\u7eed\u63d0\u5347\u81ea\u5df1\u7684\u80fd\u529b\uff0c\u90a3\u4e48\u5c31\u6765\u5173\u6ce8\u516c\u4f17\u53f7\u5427\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u4f55\u5728 Java \u4e2d\u4f7f\u7528\u53cd\u5c04\u5904\u7406&#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-202768","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/202768","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=202768"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/202768\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=202768"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=202768"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=202768"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}