{"id":10705,"date":"2023-11-28T21:41:54","date_gmt":"2023-11-28T13:41:54","guid":{"rendered":"https:\/\/wx.kaifamiao.info\/?p=10705"},"modified":"2023-12-07T14:46:03","modified_gmt":"2023-12-07T06:46:03","slug":"jvm-zhong-dong-tai-gai-bian-gou-zao-le-jie-ma","status":"publish","type":"post","link":"http:\/\/wx.kaifamiao.info\/index.php\/2023\/11\/28\/jvm-zhong-dong-tai-gai-bian-gou-zao-le-jie-ma\/","title":{"rendered":"JVM\u4e2d\u52a8\u6001\u6539\u53d8\u6784\u9020\u4e86\u89e3\u5417\uff1f"},"content":{"rendered":"<p>\u5728 Java \u865a\u62df\u673a\u4e2d\uff0c\u4e00\u822c\u6765\u8bf4\uff0c\u4e00\u65e6\u7c7b\u52a0\u8f7d\u5b8c\u6210\uff0c\u5176\u7ed3\u6784\uff08\u5305\u62ec\u65b9\u6cd5\u3001\u5b57\u6bb5\u3001\u7236\u7c7b\u7b49\uff09\u5c31\u57fa\u672c\u786e\u5b9a\uff0c\u662f\u4e0d\u53ef\u6539\u53d8\u7684\u3002\u7136\u800c\uff0c\u6709\u4e00\u4e9b\u6280\u672f\u548c\u6846\u67b6\u80fd\u591f\u5b9e\u73b0\u4e00\u5b9a\u7a0b\u5ea6\u4e0a\u7684\u52a8\u6001\u6539\u53d8\u7c7b\u7684\u7ed3\u6784\u3002<\/p>\n<p>\u4e00\u79cd\u5e38\u89c1\u7684\u5b9e\u73b0\u52a8\u6001\u7c7b\u6539\u53d8\u7684\u65b9\u5f0f\u662f\u4f7f\u7528 Java \u7684\u4ee3\u7406\u6280\u672f\u548c\u5b57\u8282\u7801\u64cd\u4f5c\u5e93\u3002\u8fd9\u5141\u8bb8\u4f60\u5728\u8fd0\u884c\u65f6\u52a8\u6001\u5730\u521b\u5efa\u548c\u4fee\u6539\u7c7b\u3002\u4ee5\u4e0b\u662f\u4e24\u79cd\u4e3b\u8981\u7684\u5b9e\u73b0\u65b9\u5f0f\uff1a<\/p>\n<ol>\n<li>\n<strong>\u4ee3\u7406\u6a21\u5f0f\uff08Proxy Pattern\uff09\uff1a<\/strong><\/p>\n<ul>\n<li>Java \u63d0\u4f9b\u4e86\u52a8\u6001\u4ee3\u7406\u7684\u673a\u5236\uff0c\u901a\u8fc7 <code>java.lang.reflect.Proxy<\/code> \u7c7b\u548c <code>InvocationHandler<\/code> \u63a5\u53e3\uff0c\u4f60\u53ef\u4ee5\u5728\u8fd0\u884c\u65f6\u751f\u6210\u4e00\u4e2a\u4ee3\u7406\u7c7b\uff0c\u7528\u4e8e\u66ff\u4ee3\u539f\u59cb\u7c7b\u7684\u5b9e\u4f8b\u3002\u4ee3\u7406\u7c7b\u4e2d\u7684\u65b9\u6cd5\u8c03\u7528\u4f1a\u88ab\u59d4\u6258\u7ed9\u5b9e\u73b0\u4e86 <code>InvocationHandler<\/code> \u63a5\u53e3\u7684\u5bf9\u8c61\uff0c\u4ece\u800c\u5b9e\u73b0\u4e86\u5bf9\u539f\u59cb\u7c7b\u7684\u589e\u5f3a\u3002<\/li>\n<\/ul>\n<pre><code class=\"language-java\">public interface MyInterface {\n    void myMethod();\n}\n\npublic class MyImplementation implements MyInterface {\n    public void myMethod() {\n        System.out.println(&quot;Original method&quot;);\n    }\n}\n\nInvocationHandler handler = (proxy, method, args) -&gt; {\n    System.out.println(&quot;Before method call&quot;);\n    Object result = method.invoke(new MyImplementation(), args);\n    System.out.println(&quot;After method call&quot;);\n    return result;\n};\n\nMyInterface proxyInstance = (MyInterface) Proxy.newProxyInstance(\n        getClass().getClassLoader(),\n        new Class[]{MyInterface.class},\n        handler);\n\nproxyInstance.myMethod();\n<\/code><\/pre>\n<\/li>\n<li>\n<strong>\u5b57\u8282\u7801\u64cd\u4f5c\u5e93\uff1a<\/strong><\/p>\n<ul>\n<li>\u4f7f\u7528\u5b57\u8282\u7801\u64cd\u4f5c\u5e93\uff0c\u5982 ASM \u6216 Byte Buddy\uff0c\u53ef\u4ee5\u76f4\u63a5\u64cd\u4f5c\u7c7b\u7684\u5b57\u8282\u7801\uff0c\u5b9e\u73b0\u5bf9\u7c7b\u7684\u4fee\u6539\u548c\u589e\u5f3a\u3002\u8fd9\u4f7f\u5f97\u4f60\u53ef\u4ee5\u5728\u8fd0\u884c\u65f6\u52a8\u6001\u5730\u521b\u5efa\u65b0\u7684\u7c7b\u6216\u4fee\u6539\u5df2\u6709\u7c7b\u7684\u5b57\u8282\u7801\u3002<\/li>\n<\/ul>\n<pre><code class=\"language-java\">public class MyClass {\n    public void myMethod() {\n        System.out.println(&quot;Original method&quot;);\n    }\n}\n\nClass&lt;?&gt; dynamicClass = new ByteBuddy()\n        .subclass(MyClass.class)\n        .method(named(&quot;myMethod&quot;))\n        .intercept(FixedValue.value(&quot;Hello from Byte Buddy!&quot;))\n        .make()\n        .load(getClass().getClassLoader(), ClassLoadingStrategy.Default.WRAPPER)\n        .getLoaded();\n\nObject instance = dynamicClass.getDeclaredConstructor().newInstance();\nMethod method = dynamicClass.getMethod(&quot;myMethod&quot;);\nObject result = method.invoke(instance);\n<\/code><\/pre>\n<\/li>\n<\/ol>\n<p>\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u8fd9\u4e9b\u52a8\u6001\u6539\u53d8\u7c7b\u7ed3\u6784\u7684\u6280\u672f\u901a\u5e38\u7528\u4e8e\u67d0\u4e9b\u7279\u6b8a\u7684\u6846\u67b6\u3001AOP\uff08\u9762\u5411\u5207\u9762\u7f16\u7a0b\uff09\u7b49\u573a\u666f\uff0c\u800c\u5728\u4e00\u822c\u7684\u5e94\u7528\u7a0b\u5e8f\u4e2d\uff0c\u4e0d\u5efa\u8bae\u9891\u7e41\u5730\u52a8\u6001\u6539\u53d8\u7c7b\u7ed3\u6784\uff0c\u56e0\u4e3a\u8fd9\u53ef\u80fd\u5f15\u5165\u590d\u6742\u6027\u548c\u4e0d\u6613\u8c03\u8bd5\u7684\u95ee\u9898\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 Java \u865a\u62df\u673a\u4e2d\uff0c\u4e00\u822c\u6765\u8bf4\uff0c\u4e00\u65e6\u7c7b\u52a0\u8f7d\u5b8c\u6210\uff0c\u5176\u7ed3\u6784\uff08\u5305\u62ec\u65b9\u6cd5\u3001\u5b57\u6bb5\u3001\u7236\u7c7b\u7b49\uff09\u5c31\u57fa\u672c\u786e\u5b9a\uff0c\u662f\u4e0d\u53ef\u6539\u53d8\u7684\u3002\u7136 [&hellip;]<\/p>\n","protected":false},"author":9,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[119],"tags":[],"class_list":["post-10705","post","type-post","status-publish","format-standard","hentry","category-jvm"],"_links":{"self":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/10705","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/users\/9"}],"replies":[{"embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/comments?post=10705"}],"version-history":[{"count":2,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/10705\/revisions"}],"predecessor-version":[{"id":16882,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/10705\/revisions\/16882"}],"wp:attachment":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/media?parent=10705"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/categories?post=10705"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/tags?post=10705"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}