{"id":1244,"date":"2025-09-16T14:28:06","date_gmt":"2025-09-16T06:28:06","guid":{"rendered":"https:\/\/eve2333.top\/?p=1244"},"modified":"2025-09-16T14:28:07","modified_gmt":"2025-09-16T06:28:07","slug":"%e6%89%8b%e5%86%99%e5%8a%a8%e6%80%81%e4%bb%a3%e7%90%86","status":"publish","type":"post","link":"https:\/\/eve2333.top\/?p=1244","title":{"rendered":"\u624b\u5199\u52a8\u6001\u4ee3\u7406"},"content":{"rendered":"\n<p>\u8fd9\u662f\u4e00\u4e2a\u65b0\u7684\u5de5\u7a0b\u3002\u6211\u4eec\u6709\u4e00\u4e2a\u63a5\u53e3\u53eb `MyInterface`\uff0c\u91cc\u9762\u5b9a\u4e49\u4e86\u4e09\u4e2a\u65b9\u6cd5\uff1a`func1()`\u3001`func2()` \u548c `func3()`\u3002<\/p>\n\n\n\n<p>\u73b0\u5728\u6211\u4eec\u6709\u4e00\u4e2a\u9700\u6c42\uff1a\u6bcf\u4e2a\u65b9\u6cd5\u88ab\u8c03\u7528\u65f6\uff0c\u90fd\u8981\u6253\u5370\u51fa\u81ea\u5df1\u7684\u65b9\u6cd5\u540d\u3002\u6700\u7b80\u5355\u7684\u505a\u6cd5\u662f\u5199\u4e00\u4e2a\u5b9e\u73b0\u7c7b `NameImpl`\uff1a<\/p>\n\n\n\n<p>```java<br>public class NameImpl implements MyInterface {<br>&nbsp; &nbsp; @Override<br>&nbsp; &nbsp; public void func1() {<br>&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(\"func1\");<br>&nbsp; &nbsp; }<\/p>\n\n\n\n<p>&nbsp; &nbsp; @Override<br>&nbsp; &nbsp; public void func2() {<br>&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(\"func2\");<br>&nbsp; &nbsp; }<\/p>\n\n\n\n<p>&nbsp; &nbsp; @Override<br>&nbsp; &nbsp; public void func3() {<br>&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(\"func3\");<br>&nbsp; &nbsp; }<br>}<br>```<\/p>\n\n\n\n<p>\u4f46\u5f88\u5feb\u9700\u6c42\u53d8\u4e86\uff1a\u4e0d\u4ec5\u8981\u6253\u5370\u65b9\u6cd5\u540d\uff0c\u8fd8\u8981\u6253\u5370\u65b9\u6cd5\u540d\u7684\u957f\u5ea6\u3002\u4e8e\u662f\u6211\u4eec\u518d\u5199\u4e00\u4e2a\u65b0\u7c7b `NameAndLengthImpl`\uff0c\u540c\u6837\u5b9e\u73b0 `MyInterface`\uff0c\u4f46\u5185\u90e8\u903b\u8f91\u53d8\u6210\u4e86\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public class NameAndLengthImpl implements MyInterface {\n\n    @Override\n    public void func1() {\n        String methodName = \"func1\";\n        System.out.println(methodName);\n        System.out.println(methodName.length());\n    }\n\n    @Override\n    public void func2() {\n        String methodName = \"func2\";\n        System.out.println(methodName);\n        System.out.println(methodName.length());\n    }\n\n    @Override\n    public void func3() {\n        String methodName = \"func3\";\n        System.out.println(methodName);\n        System.out.println(methodName.length());\n    }\n}<\/code><\/pre>\n\n\n\n<p>\u95ee\u9898\u6765\u4e86\uff1a\u5982\u679c\u6211\u4eec\u9700\u8981\u4e3a\u4e0d\u540c\u7684\u884c\u4e3a\uff08\u6bd4\u5982\u6253\u5370\u65f6\u95f4\u3001\u52a0\u65e5\u5fd7\u3001\u8ba1\u6570\u7b49\uff09\u521b\u5efa\u591a\u4e2a\u5b9e\u73b0\u7c7b\uff0c\u4ee3\u7801\u4f1a\u5927\u91cf\u91cd\u590d\uff0c\u4e14\u65e0\u6cd5\u52a8\u6001\u6269\u5c55\u3002\u6211\u4eec\u80fd\u5426**\u5728\u8fd0\u884c\u65f6\u52a8\u6001\u751f\u6210\u7c7b**\uff1f<\/p>\n\n\n\n<p>\u7b54\u6848\u662f\u53ef\u4ee5\u7684\u3002Java \u5141\u8bb8\u6211\u4eec\u5728\u7a0b\u5e8f\u8fd0\u884c\u671f\u95f4\u751f\u6210 Java \u6e90\u7801\u6587\u4ef6\uff0c\u7f16\u8bd1\u6210 `.class` \u6587\u4ef6\uff0c\u5e76\u52a0\u8f7d\u5230 JVM \u4e2d\u4f7f\u7528\u3002<\/p>\n\n\n\n<p>\u4e3a\u6b64\uff0c\u6211\u4eec\u521b\u5efa\u4e00\u4e2a\u5de5\u5382\u7c7b `MyInterfaceFactory`\uff0c\u7528\u6765\u4e13\u95e8\u751f\u6210\u5b9e\u73b0\u4e86 `MyInterface` \u7684\u4ee3\u7406\u7c7b\u3002<\/p>\n\n\n\n<p>\u7b2c\u4e00\u6b65\uff1a\u751f\u6210 Java \u6e90\u6587\u4ef6<\/p>\n\n\n\n<p>\u6211\u4eec\u5199\u4e00\u4e2a\u65b9\u6cd5 `createJavaFile()`\uff0c\u5b83\u5c06\u62fc\u63a5\u51fa\u4e00\u4e2a\u5b8c\u6574\u7684 Java \u7c7b\u6e90\u7801\u5b57\u7b26\u4e32\uff0c\u5e76\u5199\u5165\u6587\u4ef6\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public class MyInterfaceFactory {\n\n    public static File createJavaFile() throws IOException {\n        String context = \"package tech.insight.proxy;\\n\" +\n            \"\\n\" +\n            \"public class NameImpl implements MyInterface {\\n\" +\n            \"    @Override\\n\" +\n            \"    public void func1() {\\n\" +\n            \"        System.out.println(\\\"func1\\\");\\n\" +\n            \"    }\\n\" +\n            \"\\n\" +\n            \"    @Override\\n\" +\n            \"    public void func2() {\\n\" +\n            \"        System.out.println(\\\"func2\\\");\\n\" +\n            \"    }\\n\" +\n            \"\\n\" +\n            \"    @Override\\n\" +\n            \"    public void func3() {\\n\" +\n            \"        System.out.println(\\\"func3\\\");\\n\" +\n            \"    }\\n\" +\n            \"}\\n\";\n\n        File javaFile = new File(\"NameImpl.java\");\n        Files.write(javaFile.toPath(), context);\n        return javaFile;\n    }\n\n    public static void main(String&#91;] args) throw Exception {\n        createJavaFile();\n    }\n}<\/code><\/pre>\n\n\n\n<p>\u6267\u884c\u540e\uff0c\u786e\u5b9e\u53e6\u5916\u751f\u6210\u4e86\u4e00\u4e2a `NameImpl.java\u7684\u6587\u4ef6\u3002java\u865a\u62df\u673a\u4e0d\u80fd\u76f4\u63a5\u5bf9\u5b8c\u6210<\/p>\n\n\n\n<p>\u7b2c\u4e8c\u6b65\uff1a\u7f16\u8bd1 Java \u6587\u4ef6<\/p>\n\n\n\n<p>Java \u63d0\u4f9b\u4e86 `JavaCompiler` \u5de5\u5177\uff0c\u53ef\u4ee5\u5728\u8fd0\u884c\u65f6\u52a8\u6001\u7f16\u8bd1 Java \u6587\u4ef6\uff1a\u4f60\u53ef\u4ee5\u770b\u770bCompiler\u6587\u4ef6<\/p>\n\n\n\n<p>```java<br>JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();<br>int result = compiler.run(null, null, null, javaFile.getPath());<br>```<\/p>\n\n\n\n<p>\u6211\u4eec\u5c06\u7f16\u8bd1\u540e\u7684 `.class` \u6587\u4ef6\u653e\u5728\u9879\u76ee\u7684 `target\/classes` \u76ee\u5f55\u4e0b\uff0c\u786e\u4fdd\u7c7b\u8def\u5f84\u53ef\u8bbf\u95ee\u3002<\/p>\n\n\n\n<p><strong>Compiler.compile(javaFile);<br>\u5f97\u5230\u4e86.class\u6587\u4ef6<\/strong><\/p>\n\n\n\n<p>\u63a5\u7740\u4f5c\u8005\u901a\u8fc7\u81ea\u8bbegetClassName\uff08\uff09{<\/p>\n\n\n\n<p>&nbsp; &nbsp; &nbsp; &nbsp; return \"MyInterface$proxy\" + count.incrementAndGet();<\/p>\n\n\n\n<p>}<\/p>\n\n\n\n<p>\u8fd9\u6837\u83b7\u5f97\u67d0\u4e9b\u5185\u5bb9\uff0c\u5982\u4f55\u8fdb\u4e00\u6b65\u52a8\u6001\u5462\uff1f<\/p>\n\n\n\n<p>functionBody(Stirng methodName){<\/p>\n\n\n\n<p>&nbsp; &nbsp; &nbsp; &nbsp; return \"System.out .println\" +methodName +\"\\\");\";<\/p>\n\n\n\n<p>}<\/p>\n\n\n\n<p>\u8fd9\u6837\u6211\u4eec\u53ef\u4ee5\u8fdb\u4e00\u6b65<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>String func1Body = functionBody(\"func1\");\nString func2Body = functionBody(\"func2\");\nString func3Body = functionBody(\"func3\");\nString context = \"package tech.insight.proxy;\\n\" +\n    \"\\n\" +\n    \"public class \" + className + \" implements MyInterface {\\n\" +\n    \"    @Override\\n\" +\n    \"    public void func1() {\\n\" +\n    \"        \" + func1Body + \"\\n\" +\n    \"    }\\n\" +\n    \"\\n\" +\n    \"    @Override\\n\" +\n    \"    public void func2() {\\n\" +\n    \"        \" + func2Body + \"\\n\" +\n    \"    }\\n\" +\n    \"\\n\" +\n    \"    @Override\\n\" +\n    \"    public void func3() {\\n\" +\n    \"        \" + func3Body + \"\\n\" +\n    \"    }\\n\" +\n    \"}\";<\/code><\/pre>\n\n\n\n<p>\u7b2c\u4e09\u6b65\uff1a\u52a0\u8f7d\u5e76\u5b9e\u4f8b\u5316\u7c7b<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>private static MyInterface newInstance(String className) throws Exception {\n    Class&lt;?&gt; aClass = MyInterfaceFactory.class.getClassLoader().loadClass(className);\n    Constructor&lt;?&gt; constructor = aClass.getConstructor();\n    MyInterface proxy = (MyInterface) constructor.newInstance();\n    return proxy;\n}\n\npublic static MyInterface createProxyObject() throws Exception {\n    String className = getClassName();\n    File javaFile = createJavaFile(className);\n    Compiler.compile(javaFile);\n    return newInstance(\"tech.insight.proxy.\" + className);\n}<\/code><\/pre>\n\n\n\n<p>\u521b\u5efa\u4e00\u4e2amain\u65b9\u6cd5\uff0c\u628a\u4e0a\u9762\u7684\u6240\u6709\u6682\u65f6\u7edf\u7edf\u79c1\u6709\uff0c\u521b\u5efa\u4e86\u672c\u6765\u5728\u865a\u62df\u673a\u4e2d\u7684\u6ca1\u6709\u7684\u7c7b\uff1b<\/p>\n\n\n\n<p>public class Main {<\/p>\n\n\n\n<p>&nbsp; &nbsp; public static void main(String[] args) throws Exception {<br>&nbsp; &nbsp; &nbsp; &nbsp; MyInterface proxyObject = MyInterfaceFactory.createProxyObject();<br>&nbsp; &nbsp; &nbsp; &nbsp; proxyObject.func1();<br>&nbsp; &nbsp; &nbsp; &nbsp; proxyObject.func2();<br>&nbsp; &nbsp; &nbsp; &nbsp; proxyObject.func3();<br>&nbsp; &nbsp; }<br>}\u4f7f\u7528\u7c7b\u52a0\u8f7d\u5668\u5c06\u7f16\u8bd1\u597d\u7684\u5b57\u8282\u7801\u52a0\u8f7d\u8fdb\u865a\u62df\u673a\uff1a<\/p>\n\n\n\n<p>```java<br>URLClassLoader classLoader = URLClassLoader.newInstance(new URL[]{new File(\"target\/classes\").toURI().toURL()});<br>Class&lt;?&gt; clazz = classLoader.loadClass(\"MyInterface$Proxy1\");<br>Constructor&lt;?&gt; constructor = clazz.getConstructor();<br>Object instance = constructor.newInstance();<br>MyInterface proxy = (MyInterface) instance;<br>```<\/p>\n\n\n\n<p>\u8fd9\u6837\u6211\u4eec\u5c31\u5f97\u5230\u4e86\u4e00\u4e2a\u5728\u8fd0\u884c\u65f6\u52a8\u6001\u521b\u5efa\u7684\u5bf9\u8c61\u3002<\/p>\n\n\n\n<p>\u7b2c\u56db\u6b65\uff1a\u8ba9\u7c7b\u540d\u548c\u65b9\u6cd5\u4f53\u52a8\u6001\u5316<\/p>\n\n\n\n<p>\u76ee\u524d\u7c7b\u540d\u548c\u65b9\u6cd5\u4f53\u90fd\u662f\u786c\u7f16\u7801\u7684\u3002\u6211\u4eec\u53ef\u4ee5\u4f18\u5316\uff1a<\/p>\n\n\n\n<p>- \u7c7b\u540d\u901a\u8fc7\u539f\u5b50\u8ba1\u6570\u5668\u81ea\u589e\uff0c\u907f\u514d\u51b2\u7a81\uff0c\u5982 `MyInterface$Proxy1`\u3001`MyInterface$Proxy2`\u3002<br>- \u65b9\u6cd5\u4f53\u4e0d\u518d\u56fa\u5b9a\uff0c\u800c\u662f\u7531\u5916\u90e8\u4f20\u5165\u4e00\u4e2a\u201c\u5904\u7406\u5668\u201d\u6765\u51b3\u5b9a\u751f\u6210\u4ec0\u4e48\u4ee3\u7801\u3002<\/p>\n\n\n\n<p>\u6211\u4eec\u5b9a\u4e49\u4e00\u4e2a\u63a5\u53e3 `MyHandler`\uff1a<\/p>\n\n\n\n<p>```java<br>public interface MyHandler {<br>&nbsp; &nbsp; String getMethodBody(String methodName);<br>}<br>```<\/p>\n\n\n\n<p>\u8fd9\u4e2a\u63a5\u53e3\u7684\u4f5c\u7528\u662f\uff1a\u7ed9\u5b9a\u65b9\u6cd5\u540d\uff0c\u8fd4\u56de\u8be5\u65b9\u6cd5\u4f53\u5185\u8981\u6267\u884c\u7684 Java \u4ee3\u7801\u5b57\u7b26\u4e32\u3002<\/p>\n\n\n\n<p>&nbsp;\u4f46\u662f\u5982\u4f55\u8fdb\u4e00\u6b65\u7684\u52a8\u6001\u5462\uff1f\u5c31\u662f\u628agetClassName\u6216\u8005 functionBody \u51fd\u6570\u4e5f\u52a8\u6001\uff0c\u6211\u4eec\u65b0\u5efa\u4e00\u4e2aMyHandler\u7684\u6587\u4ef6\uff0c\u83b7\u5f97\u4e00\u4e2a\u65b9\u6cd5\u540d\uff0c\u8fd4\u56de\u4e00\u4e2a\u51fd\u6570\u4f53\uff1a\u52a8\u6001\u4ee3\u7406\u7684\u4e09\u4e2a\u53c2\u6570\uff0c\u52a0\u8f7d\u5668\uff0c\u7c7b\uff0chandler\u90fd\u9f50\u4e86<\/p>\n\n\n\n<p>\u8fd9\u6837\u6211\u4eec\u4e3a\u539f\u5148\u7684\u4e24\u4e2a\u51fd\u6570\u914d\u7f6e\u4f20\u9012\u65b9\u6cd5\u4f53\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public class MyInterfaceFactory {\n\n    private static final AtomicInteger count = new AtomicInteger();\n\n    private static File createJavaFile(String className, MyHandler handler) throws IOException {\n        String func1Body = handler.functionBody(\"func1\");\n        String func2Body = handler.functionBody(\"func2\");\n        String func3Body = handler.functionBody(\"func3\");\n        String context = \"package tech.insight.proxy;\\n\" +\n            \"\\n\" +\n            \"public class \" + className + \" implements MyInterface {\\n\" +\n            \"    @Override\\n\" +\n            \"    public void func1() {\\n\" +\n\n\n\n...............................................<\/code><\/pre>\n\n\n\n<p>\u9009\u62e9\u5220\u6389\u6211\u4eec\u7684pxory\u4e86<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>private static MyInterface newInstance(String className) throws Exception {\n    Class&lt;?&gt; aClass = MyInterfaceFactory.class.getClassLoader().loadClass(className);\n    Constructor&lt;?&gt; constructor = aClass.getConstructor();\n    MyInterface proxy = (MyInterface) constructor.newInstance();\n    return proxy;\n}\n\npublic static MyInterface createProxyObject(MyHandler myHandler) throws Exception {\n    String className = getClassName();\n    File javaFile = createJavaFile(className, myHandler);\n    Compiler.compile(javaFile);\n    return newInstance(\"tech.insight.proxy.\" + className);\n}<\/code><\/pre>\n\n\n\n<p>\u7136\u540e\u5728\u751f\u6210\u7c7b\u65f6\uff0c\u628a `MyHandler` \u4f20\u8fdb\u53bb\uff0c\u7528\u5b83\u6765\u751f\u6210\u6bcf\u79cd\u65b9\u6cd5\u7684\u51fd\u6570\u4f53\uff1a<\/p>\n\n\n\n<p>```java<br>for (Method method : MyInterface.class.getDeclaredMethods()) {<br>&nbsp; &nbsp; String body = handler.getMethodBody(method.getName());<br>&nbsp; &nbsp; content.append(\" &nbsp; &nbsp;public void \").append(method.getName())<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.append(\"() { \").append(body).append(\" }\\n\");<br>}<br>```<\/p>\n\n\n\n<p>\u4f8b\u5982\uff0c\u6211\u4eec\u53ef\u4ee5\u5b9e\u73b0\u4e00\u4e2a\u53ea\u6253\u5370\u65b9\u6cd5\u540d\u7684\u5904\u7406\u5668\uff1a<\/p>\n\n\n\n<p>```java<br>public class PrintMethodNameHandler implements MyHandler {<br>&nbsp; &nbsp; @Override<br>&nbsp; &nbsp; public String getMethodBody(String methodName) {<br>&nbsp; &nbsp; &nbsp; &nbsp; return \"System.out.println(\\\"\" + methodName + \"\\\");\";<br>&nbsp; &nbsp; }<br>}<\/p>\n\n\n\n<p>\u4e5f\u53ef\u4ee5\u5b9e\u73b0\u4e00\u4e2a\u5148\u6253\u5370 `\"1\"` \u518d\u6253\u5370\u65b9\u6cd5\u540d\u7684\u5904\u7406\u5668\uff1a<\/p>\n\n\n\n<p>public class PrintOneThenNameHandler implements MyHandler {<br>&nbsp; &nbsp; @Override<br>&nbsp; &nbsp; public String getMethodBody(String methodName) {<br>&nbsp; &nbsp; &nbsp; &nbsp; StringBuilder sb = new StringBuilder();<br>&nbsp; &nbsp; &nbsp; &nbsp; sb.append(\"System.out.println(\\\"1\\\");\");<br>&nbsp; &nbsp; &nbsp; &nbsp; sb.append(\"System.out.println(\\\"\").append(methodName).append(\"\\\");\");<br>&nbsp; &nbsp; &nbsp; &nbsp; return sb.toString();<br>&nbsp; &nbsp; }<br>}<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>static class PrintFunctionName1 implements MyHandler {\n\n    @Override\n    public String functionBody(String methodName) {\n        StringBuilder stringBuilder = new StringBuilder();\n        stringBuilder.append(\"System.out.println();\")\n                   .append(\" System.out.println(\\\"\" + methodName + \"\\\");\");\n        return stringBuilder.toString();\n    }\n}<\/code><\/pre>\n\n\n\n<p>\u6240\u6709\u7684\u5bf9\u8c61\u90fd\u662f\u4ee3\u7406\u62ff\u5230\u7684<\/p>\n\n\n\n<p>P \u529f\u80fd\uff08\u524d\u7f6e\/\u540e\u7f6e\u589e\u5f3a\uff09<\/p>\n\n\n\n<p>\u6211\u4eec\u73b0\u5728\u60f3\u5b9e\u73b0\u7c7b\u4f3c Spring AOP \u7684\u529f\u80fd\uff1a\u5728\u65b9\u6cd5\u6267\u884c\u524d\u540e\u81ea\u52a8\u6dfb\u52a0\u65e5\u5fd7\u3002<\/p>\n\n\n\n<p>\u6211\u4eec\u9700\u8981\u751f\u6210\u8fd9\u6837\u7684\u4ee3\u7801\uff1a<\/p>\n\n\n\n<p>```java<br>public void func1() {<br>&nbsp; &nbsp; System.out.println(\"before\");<br>&nbsp; &nbsp; myInterface.func1();<br>&nbsp; &nbsp; System.out.println(\"after\");<br>}<br>```<\/p>\n\n\n\n<p>\u4f46\u8fd9\u91cc\u6709\u4e2a\u95ee\u9898\uff1a\u751f\u6210\u7684\u7c7b\u9700\u8981\u6301\u6709\u4e00\u4e2a\u771f\u5b9e\u7684 `MyInterface` \u5b9e\u4f8b\uff0c\u5e76\u8c03\u7528\u5b83\u7684\u65b9\u6cd5\u3002<\/p>\n\n\n\n<p>\u56e0\u6b64\uff0c`MyHandler` \u9700\u8981\u80fd\u8bbe\u7f6e\u76ee\u6807\u5bf9\u8c61\u3002\u6211\u4eec\u6269\u5c55\u63a5\u53e3\uff1a\u907f\u514d\u7a7a\u6307\u9488\uff0c\u8fd9\u6837<\/p>\n\n\n\n<p>```java<br>public interface MyHandler {<br>&nbsp; &nbsp; String getMethodBody(String methodName);<br>&nbsp; &nbsp;&nbsp;<br>&nbsp; &nbsp; default void setProxy(Object proxy) {}<br>}<br>```<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public interface MyHandler {\n    String functionBody(String methodName);\n\n    default void setProxy(MyInterface proxy) {\n    }\n}<\/code><\/pre>\n\n\n\n<p>\u9ed8\u8ba4\u65b9\u6cd5\u8868\u793a\u5927\u591a\u6570\u5904\u7406\u5668\u4e0d\u9700\u8981\u8bbe\u7f6e\u4ee3\u7406\u5bf9\u8c61\u3002<\/p>\n\n\n\n<p>\u53ea\u6709\u50cf `LogHandler` \u8fd9\u6837\u7684\u5904\u7406\u5668\u624d\u9700\u8981\uff1a<\/p>\n\n\n\n<p>```java<br>public class LogHandler implements MyHandler {<br>&nbsp; &nbsp; private final MyInterface target;<\/p>\n\n\n\n<p>&nbsp; &nbsp; public LogHandler(MyInterface target) {<br>&nbsp; &nbsp; &nbsp; &nbsp; this.target = target;<br>&nbsp; &nbsp; }<\/p>\n\n\n\n<p>&nbsp; &nbsp; @Override<br>&nbsp; &nbsp; public String getMethodBody(String methodName) {<br>&nbsp; &nbsp; &nbsp; &nbsp; return \"System.out.println(\\\"before\\\");\" +<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\"myInterface.\" + methodName + \"();\" +<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\"System.out.println(\\\"after\\\");\";<br>&nbsp; &nbsp; }<\/p>\n\n\n\n<p>&nbsp; &nbsp; @Override<br>&nbsp; &nbsp; public void setProxy(Object proxy) {<br>&nbsp; &nbsp; &nbsp; &nbsp; try {<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Field field = proxy.getClass().getDeclaredField(\"myInterface\");<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; field.setAccessible(true);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; field.set(proxy, target);<br>&nbsp; &nbsp; &nbsp; &nbsp; } catch (Exception e) {<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; throw new RuntimeException(e);<br>&nbsp; &nbsp; &nbsp; &nbsp; }<br>&nbsp; &nbsp; }<br>}<br>```<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>static class LogHandler implements MyHandler {\n\n    MyInterface myInterface;\n\n    public LogHandler(MyInterface myInterface) {\n        this.myInterface = myInterface;\n    }\n\n    @Override\n    public void setProxy(MyInterface proxy) {\n        Class&lt;?&gt; extends MyInterface aClass = proxy.getClass();\n        Field field = null;\n        try {\n            field = aClass.getDeclaredField(\"myInterface\");\n            field.setAccessible(true);\n            field.set(proxy, myInterface);\n        } catch (Exception e) {\n            throw new RuntimeException(e);\n        }\n    }<\/code><\/pre>\n\n\n\n<p>\u6211\u4eec\u5728\u751f\u6210\u7684\u7c7b\u4e2d\u6dfb\u52a0\u5b57\u6bb5\uff1a<\/p>\n\n\n\n<p>```java<br>content.append(\" &nbsp; &nbsp;private MyInterface myInterface;\\n\");<br>```<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>private static MyInterface newInstance(String className, MyHandler handler) throws Exception {\n    Class&lt;?&gt; aClass = MyInterfaceFactory.class.getClassLoader().loadClass(className);\n    Constructor&lt;?&gt; constructor = aClass.getConstructor();\n    MyInterface proxy = (MyInterface) constructor.newInstance();\n    handler.setProxy(proxy);\n    return proxy;\n}\n\npublic static MyInterface createProxyObject(MyHandler myHandler) throws Exception {\n    String className = getClassName();\n    File javaFile = createJavaFile(className, myHandler);\n    Compiler.compile(javaFile);\n    return newInstance(\"tech.insight.proxy.\" + className, myHandler);\n}<\/code><\/pre>\n\n\n\n<p>\u5e76\u5728 `newInstance()` \u65f6\u8c03\u7528 `handler.setProxy(proxy)` \u5b8c\u6210\u8d4b\u503c\u3002<\/p>\n\n\n\n<p><strong>proxyObject= MyInterfaceFactory.createProxyObject(new LogHandler(proxyObject));<\/strong><\/p>\n\n\n\n<p>\u6700\u7ec8\uff0c\u6211\u4eec\u6210\u529f\u5b9e\u73b0\u4e86\uff1a<\/p>\n\n\n\n<p>- \u52a8\u6001\u751f\u6210\u5b9e\u73b0\u4e86 `MyInterface` \u7684\u7c7b\uff1b<br>- \u65b9\u6cd5\u4f53\u5185\u5bb9\u7531 `MyHandler` \u63a7\u5236\uff1b<br>- \u652f\u6301\u5728\u65b9\u6cd5\u524d\u540e\u63d2\u5165\u903b\u8f91\uff0c\u5b9e\u73b0 AOP \u589e\u5f3a\u3002<\/p>\n\n\n\n<p>\u8fd9\u5c31\u662f\u6211\u4eec\u5199\u7684\u52a8\u6001\u4ee3\u7406 \u90a3\u4e48\u518d\u56de\u987e\u4e00\u4e0b \u6211\u4eec\u662f\u5982\u4f55\u5199\u6211\u4eec\u7684\u52a8\u6001\u4ee3\u7406\u7684\u5462 \u9996\u5148\u6211\u4eec\u751f\u6210\u4e86\u4e00\u4e2a\u6587\u5b57\u7248\u7684java\u7c7b \u7136\u540e\u6211\u4eec\u5728\u8fd0\u884c\u65f6\u628a\u5b83\u7f16\u8bd1 \u518d\u7528\u901a\u8fc7class loader\u53bb\u628a\u8fd9\u4e2a\u7f16\u8bd1\u51fa\u6765\u7684\u5b57\u8282\u7801 \u6587\u4ef6\u52a0\u8f7d\u5230\u865a\u62df\u673a\u4e2d \u7136\u540e\u518d\u901a\u8fc7\u53cd\u5c04\u53bb\u521b\u5efa\u8fd9\u4e2a\u7c7b \u7136\u540e\u518d\u8c03\u7528\u8fd9\u4e2a\u7c7b\u5bf9\u5e94\u7684\u65b9\u6cd5 \u8fd9\u5c31\u662f\u6211\u4eec\u5199\u7684\u52a8\u6001\u4ee3\u7406<\/p>\n\n\n\n<p>\u867d\u7136\u529f\u80fd\u5b8c\u6574\uff0c\u4f46\u5b83\u6709\u660e\u663e\u7f3a\u9677\uff1a<\/p>\n\n\n\n<p>1. \u53ea\u80fd\u4ee3\u7406 `MyInterface`\uff0c\u65e0\u6cd5\u6cdb\u5316\uff1b<br>2. \u4f9d\u8d56\u78c1\u76d8 IO \u548c\u7f16\u8bd1\u8fc7\u7a0b\uff0c\u6027\u80fd\u5dee\uff1b<br>3. \u751f\u6210\u7684\u662f Java \u6e90\u7801\uff0c\u800c\u4e0d\u662f\u76f4\u63a5\u64cd\u4f5c\u5b57\u8282\u7801\u3002<\/p>\n\n\n\n<p>\u800c JDK \u81ea\u5e26\u7684\u52a8\u6001\u4ee3\u7406\uff08`java.lang.reflect.Proxy`\uff09\u548c CGLIB \u5219\u66f4\u9ad8\u6548\uff0c\u5b83\u4eec\u53ef\u4ee5\u76f4\u63a5\u5728\u5185\u5b58\u4e2d\u751f\u6210\u5b57\u8282\u7801\uff0c\u65e0\u9700\u5199\u6587\u4ef6\u548c\u7f16\u8bd1\u3002<\/p>\n\n\n\n<p>Spring \u7684\u4e8b\u52a1\u7ba1\u7406\u5e95\u5c42\u5c31\u662f\u57fa\u4e8e\u52a8\u6001\u4ee3\u7406\u3002\u5f53\u4f60\u5728\u4e00\u4e2a\u52a0\u4e86 `@Transactional` \u7684\u65b9\u6cd5\u4e0a\u6267\u884c\u6570\u636e\u5e93\u64cd\u4f5c\u65f6\uff0cSpring \u4f1a\u751f\u6210\u4e00\u4e2a\u4ee3\u7406\u5bf9\u8c61\uff0c\u5728\u65b9\u6cd5\u6267\u884c\u524d\u540e\u81ea\u52a8\u5f00\u542f\u4e8b\u52a1\u3001\u63d0\u4ea4\u6216\u56de\u6eda\uff1a<\/p>\n\n\n\n<p>```java<br>try {<br>&nbsp; &nbsp; \u5f00\u542f\u4e8b\u52a1();<br>&nbsp; &nbsp; target.insert(); \/\/ \u771f\u5b9e\u4e1a\u52a1\u903b\u8f91<br>&nbsp; &nbsp; \u63d0\u4ea4\u4e8b\u52a1();<br>} catch (RuntimeException e) {<br>&nbsp; &nbsp; \u56de\u6eda\u4e8b\u52a1();<br>&nbsp; &nbsp; throw e;<br>}<br>```<\/p>\n\n\n\n<p>\u7406\u89e3\u8fd9\u4e00\u70b9\u540e\uff0c\u5c31\u80fd\u660e\u767d\u4e3a\u4ec0\u4e48\u6709\u4e9b\u60c5\u51b5\u4e0b\u4e8b\u52a1\u4f1a\u5931\u6548\uff1a<\/p>\n\n\n\n<p>- \u5982\u679c\u4f60\u6355\u83b7\u4e86\u5f02\u5e38\u5e76\u201c\u541e\u6389\u201d\uff0c\u4ee3\u7406\u5c42\u65e0\u6cd5\u611f\u77e5\u5f02\u5e38\uff0c\u5c31\u4e0d\u4f1a\u56de\u6eda\uff1b<br>- \u5982\u679c\u629b\u51fa\u7684\u662f checked exception\uff08\u975e `RuntimeException`\uff09\uff0c\u9ed8\u8ba4\u4e0d\u4f1a\u89e6\u53d1\u56de\u6eda\uff0c\u9664\u975e\u663e\u5f0f\u914d\u7f6e\uff1b<br>- \u5982\u679c\u81ea\u8c03\u7528\uff08this.method()\uff09\uff0c\u7ed5\u8fc7\u4e86\u4ee3\u7406\u5bf9\u8c61\uff0cAOP \u4e0d\u751f\u6548\u3002<\/p>\n\n\n\n<p>\u601d\u8003\u9898\uff1a &nbsp;<br>\u4f60\u80fd\u57fa\u4e8e\u4ee5\u4e0a\u601d\u8def\uff0c\u81ea\u5df1\u5b9e\u73b0\u4e00\u4e2a\u652f\u6301 `before`\u3001`after`\u3001`around` \u7684\u901a\u7528\u589e\u5f3a\u5904\u7406\u5668\u5417\uff1f \u200b<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u8fd9\u662f\u4e00\u4e2a\u65b0\u7684\u5de5\u7a0b\u3002\u6211\u4eec\u6709\u4e00\u4e2a\u63a5\u53e3\u53eb `MyInterface`\uff0c\u91cc\u9762\u5b9a\u4e49\u4e86\u4e09\u4e2a\u65b9\u6cd5\uff1a`func1()`\u3001`func2()` \u548c ` &#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"emotion":"","emotion_color":"","title_style":"","license":"","footnotes":""},"categories":[3,2],"tags":[33,34],"class_list":["post-1244","post","type-post","status-publish","format-standard","hentry","category-3","category-2","tag-jdk","tag-34"],"_links":{"self":[{"href":"https:\/\/eve2333.top\/index.php?rest_route=\/wp\/v2\/posts\/1244","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/eve2333.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/eve2333.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/eve2333.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/eve2333.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1244"}],"version-history":[{"count":1,"href":"https:\/\/eve2333.top\/index.php?rest_route=\/wp\/v2\/posts\/1244\/revisions"}],"predecessor-version":[{"id":1245,"href":"https:\/\/eve2333.top\/index.php?rest_route=\/wp\/v2\/posts\/1244\/revisions\/1245"}],"wp:attachment":[{"href":"https:\/\/eve2333.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1244"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/eve2333.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1244"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/eve2333.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1244"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}