{"id":208381,"date":"2025-08-26T10:23:00","date_gmt":"2025-08-26T02:23:00","guid":{"rendered":"https:\/\/www.server.hk\/blog\/?p=208381"},"modified":"2025-08-26T10:23:00","modified_gmt":"2025-08-26T02:23:00","slug":"%e6%b7%b1%e5%85%a5%e7%90%86%e8%a7%a3java%e5%8f%8d%e5%b0%84%e4%b8%8e%e5%8a%a8%e6%80%81%e4%bb%a3%e7%90%86%e7%9a%84%e9%ab%98%e7%ba%a7%e7%bc%96%e7%a8%8b","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/208381\/","title":{"rendered":"\u6df1\u5165\u7406\u89e3Java\u53cd\u5c04\u4e0e\u52a8\u6001\u4ee3\u7406\u7684\u9ad8\u7ea7\u7f16\u7a0b"},"content":{"rendered":"<h2 dir=\"auto\">Java\u53cd\u5c04\u7b80\u4ecb<\/h2>\n<p dir=\"auto\">Java\u53cd\u5c04\u662fJava\u8bed\u8a00\u7684\u4e00\u9879\u5f3a\u5927\u529f\u80fd\uff0c\u5141\u8bb8\u8fd0\u884c\u4e2d\u7684\u7a0b\u5e8f\u68c0\u67e5\u548c\u64cd\u4f5c\u81ea\u8eab\u7684\u7ed3\u6784\uff0c\u5305\u62ec\u7c7b\u3001\u65b9\u6cd5\u3001\u5b57\u6bb5\u548c\u6784\u9020\u65b9\u6cd5\u3002\u8fd9\u79cd\u80fd\u529b\u5bf9\u4e8e\u6784\u5efa\u7075\u6d3b\u4e14\u52a8\u6001\u7684\u5e94\u7528\u7a0b\u5e8f\uff0c\u7279\u522b\u662f\u5728\u6846\u67b6\u548c\u5e93\u4e2d\u81f3\u5173\u91cd\u8981\u3002<\/p>\n<h3 dir=\"auto\">\u4ec0\u4e48\u662f\u53cd\u5c04\uff1f<\/h3>\n<p dir=\"auto\">\u53cd\u5c04\u4f7fJava\u7a0b\u5e8f\u80fd\u591f\u5728\u8fd0\u884c\u65f6\u52a8\u6001\u68c0\u67e5\u548c\u4fee\u6539\u7c7b\u53ca\u5bf9\u8c61\u7684\u5c5e\u6027\u3001\u65b9\u6cd5\u548c\u6784\u9020\u65b9\u6cd5\u3002\u901a\u8fc7\u8bbf\u95eejava.lang.Class\u5bf9\u8c61\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\u83b7\u53d6\u7c7b\u7684\u5143\u6570\u636e\uff0c\u5e76\u6267\u884c\u8bf8\u5982\u8c03\u7528\u65b9\u6cd5\u6216\u8bbf\u95ee\u5b57\u6bb5\u7b49\u64cd\u4f5c\u3002<\/p>\n<h3 dir=\"auto\">\u53cd\u5c04\u7684\u4e3b\u8981\u5e94\u7528\u573a\u666f<\/h3>\n<p dir=\"auto\">\u53cd\u5c04\u5728\u4ee5\u4e0b\u573a\u666f\u4e2d\u5e7f\u6cdb\u5e94\u7528\uff1a<\/p>\n<ul dir=\"auto\">\n<li><strong>\u6846\u67b6\u5f00\u53d1<\/strong>\uff1a\u5982Spring\u548cHibernate\u7b49\u6846\u67b6\u5229\u7528\u53cd\u5c04\u6839\u636e\u914d\u7f6e\u6587\u4ef6\uff08\u5982XML\u6216\u6ce8\u89e3\uff09\u52a8\u6001\u52a0\u8f7d\u548c\u914d\u7f6e\u7c7b\u3002<\/li>\n<li><strong>\u52a8\u6001\u4ee3\u7406\u521b\u5efa<\/strong>\uff1a\u53cd\u5c04\u662f\u5b9e\u73b0\u52a8\u6001\u4ee3\u7406\u7684\u5173\u952e\uff0c\u652f\u6301\u9762\u5411\u5207\u9762\u7f16\u7a0b\uff08AOP\uff09\u901a\u8fc7\u62e6\u622a\u65b9\u6cd5\u8c03\u7528\u3002<\/li>\n<li><strong>\u6ce8\u89e3\u5904\u7406<\/strong>\uff1a\u53cd\u5c04\u4f7f\u6846\u67b6\u80fd\u591f\u68c0\u67e5\u6ce8\u89e3\u5e76\u6267\u884c\u76f8\u5e94\u7684\u903b\u8f91\uff0c\u4f8b\u5982\u9a8c\u8bc1\u8f93\u5165\u6216\u6620\u5c04\u5bf9\u8c61\u3002<\/li>\n<li><strong>\u6269\u5c55\u6027<\/strong>\uff1a\u5e94\u7528\u7a0b\u5e8f\u53ef\u4ee5\u901a\u8fc7\u5b8c\u5168\u9650\u5b9a\u540d\u52a8\u6001\u52a0\u8f7d\u7528\u6237\u5b9a\u4e49\u7684\u7c7b\uff0c\u589e\u5f3a\u6a21\u5757\u5316\u3002<\/li>\n<\/ul>\n<h3 dir=\"auto\">\u53cd\u5c04\u7684\u4f18\u70b9\u4e0e\u7f3a\u70b9<\/h3>\n<h4 dir=\"auto\">\u4f18\u70b9<\/h4>\n<ul dir=\"auto\">\n<li><strong>\u7075\u6d3b\u6027<\/strong>\uff1a\u652f\u6301\u52a8\u6001\u884c\u4e3a\uff0c\u9002\u5408\u9700\u8981\u8fd0\u884c\u65f6\u9002\u914d\u7684\u6846\u67b6\u548c\u5de5\u5177\u3002<\/li>\n<li><strong>\u6269\u5c55\u6027<\/strong>\uff1a\u652f\u6301\u52a0\u8f7d\u548c\u96c6\u6210\u5916\u90e8\u7c7b\uff0c\u65e0\u9700\u786c\u7f16\u7801\u3002<\/li>\n<\/ul>\n<h4 dir=\"auto\">\u7f3a\u70b9<\/h4>\n<ul dir=\"auto\">\n<li><strong>\u6027\u80fd\u5f00\u9500<\/strong>\uff1a\u53cd\u5c04\u6d89\u53ca\u8fd0\u884c\u65f6\u7c7b\u578b\u89e3\u6790\uff0c\u65e0\u6cd5\u5229\u7528\u67d0\u4e9bJVM\u4f18\u5316\uff0c\u6027\u80fd\u4f4e\u4e8e\u76f4\u63a5\u8c03\u7528\u3002<\/li>\n<li><strong>\u5b89\u5168\u98ce\u9669<\/strong>\uff1a\u53cd\u5c04\u53ef\u4ee5\u7ed5\u8fc7\u8bbf\u95ee\u4fee\u9970\u7b26\uff08\u5982\u79c1\u6709\u5b57\u6bb5\uff09\uff0c\u82e5\u4f7f\u7528\u4e0d\u5f53\u53ef\u80fd\u5bfc\u81f4\u5b89\u5168\u6f0f\u6d1e\u3002<\/li>\n<li><strong>\u4ee3\u7801\u590d\u6742\u6027<\/strong>\uff1a\u4e0d\u5f53\u4f7f\u7528\u53cd\u5c04\u53ef\u80fd\u5bfc\u81f4\u610f\u5916\u526f\u4f5c\u7528\uff0c\u964d\u4f4e\u4ee3\u7801\u53ef\u7ef4\u62a4\u6027\u548c\u53ef\u79fb\u690d\u6027\u3002<\/li>\n<\/ul>\n<h2 dir=\"auto\">\u53cd\u5c04\u7684\u5de5\u4f5c\u539f\u7406<\/h2>\n<h3 dir=\"auto\">\u7c7b\u52a0\u8f7d\u8fc7\u7a0b<\/h3>\n<p dir=\"auto\">Java\u865a\u62df\u673a\uff08JVM\uff09\u901a\u8fc7\u4ee5\u4e0b\u6b65\u9aa4\u52a0\u8f7d\u7c7b\uff1a<\/p>\n<ol dir=\"auto\">\n<li><strong>\u7f16\u8bd1<\/strong>\uff1aJava\u7f16\u8bd1\u5668\uff08javac\uff09\u5c06.java\u6e90\u6587\u4ef6\u8f6c\u6362\u4e3a.class\u5b57\u8282\u7801\u6587\u4ef6\u3002<\/li>\n<li><strong>\u52a0\u8f7d<\/strong>\uff1a\u7c7b\u52a0\u8f7d\u5668\u8bfb\u53d6\u5b57\u8282\u7801\u5e76\u52a0\u8f7d\u5230\u5185\u5b58\u4e2d\uff0c\u521b\u5efa\u4ee3\u8868\u8be5\u7c7b\u7684java.lang.Class\u5bf9\u8c61\u3002<\/li>\n<li><strong>\u94fe\u63a5<\/strong>\uff1aJVM\u9a8c\u8bc1\u5b57\u8282\u7801\uff0c\u4e3a\u9759\u6001\u5b57\u6bb5\u5206\u914d\u5185\u5b58\uff0c\u5e76\u89e3\u6790\u5f15\u7528\u3002<\/li>\n<li><strong>\u521d\u59cb\u5316<\/strong>\uff1a\u521d\u59cb\u5316\u9759\u6001\u53d8\u91cf\u548c\u9759\u6001\u5757\uff0c\u51c6\u5907\u7c7b\u4ee5\u4f9b\u4f7f\u7528\u3002<\/li>\n<\/ol>\n<p dir=\"auto\">Class\u5bf9\u8c61\u662f\u53cd\u5c04\u7684\u5165\u53e3\uff0c\u63d0\u4f9b\u5bf9\u7c7b\u7ed3\u6784\uff08\u5305\u62ec\u5b57\u6bb5\u3001\u65b9\u6cd5\u548c\u6784\u9020\u65b9\u6cd5\uff09\u7684\u8bbf\u95ee\u3002<\/p>\n<h3 dir=\"auto\">\u83b7\u53d6Class\u5bf9\u8c61<\/h3>\n<p dir=\"auto\">\u83b7\u53d6Class\u5bf9\u8c61\u7684\u4e09\u79cd\u4e3b\u8981\u65b9\u5f0f\uff1a<\/p>\n<ol dir=\"auto\">\n<li><strong>\u4f7f\u7528Class.forName<\/strong>\uff1a\u901a\u8fc7\u5b8c\u5168\u9650\u5b9a\u540d\u52a8\u6001\u52a0\u8f7d\u7c7b\u3002\n<div dir=\"auto\">\n<div>\n<div>java<\/div>\n<div>\n<div>\n<div><\/div>\n<\/div>\n<\/div>\n<div><code><\/code><\/p>\n<div>\n<div>\n<div>Class&lt;?&gt; clazz = Class.forName(&#8220;java.lang.String&#8221;);<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div><\/div>\n<div><\/div>\n<\/div>\n<\/div>\n<\/li>\n<li><strong>\u4f7f\u7528.class\u8bed\u6cd5<\/strong>\uff1a\u76f4\u63a5\u901a\u8fc7\u7c7b\u540d\u5f15\u7528\u7c7b\u3002\n<div dir=\"auto\">\n<div>\n<div>java<\/div>\n<div>\n<div>\n<div><\/div>\n<\/div>\n<\/div>\n<div><code><\/code><\/p>\n<div>\n<div>\n<div>Class&lt;?&gt; clazz = String.class;<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div><\/div>\n<div><\/div>\n<\/div>\n<\/div>\n<\/li>\n<li><strong>\u4f7f\u7528getClass()<\/strong>\uff1a\u4ece\u5b9e\u4f8b\u4e2d\u83b7\u53d6Class\u5bf9\u8c61\u3002\n<div dir=\"auto\">\n<div>\n<div>java<\/div>\n<div>\n<div>\n<div><\/div>\n<\/div>\n<\/div>\n<div><code><\/code><\/p>\n<div>\n<div>\n<div>String str = &#8220;example&#8221;;<\/div>\n<div>Class&lt;?&gt; clazz = str.getClass();<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div><\/div>\n<div><\/div>\n<\/div>\n<\/div>\n<\/li>\n<\/ol>\n<h3 dir=\"auto\">\u8bbf\u95ee\u7c7b\u7ec4\u4ef6<\/h3>\n<p dir=\"auto\">java.lang.reflect\u5305\u63d0\u4f9b\u4e86Field\u3001Method\u548cConstructor\u7c7b\u6765\u64cd\u4f5c\u7c7b\u7ec4\u4ef6\uff1a<\/p>\n<ul dir=\"auto\">\n<li><strong>\u5b57\u6bb5<\/strong>\uff1a\u4f7f\u7528getField\u6216getDeclaredField\u5206\u522b\u8bbf\u95ee\u516c\u5171\u5b57\u6bb5\u6216\u6240\u6709\u5b57\u6bb5\u3002<\/li>\n<li><strong>\u65b9\u6cd5<\/strong>\uff1a\u4f7f\u7528getMethod\u6216getDeclaredMethod\u5206\u522b\u83b7\u53d6\u516c\u5171\u65b9\u6cd5\u6216\u6240\u6709\u65b9\u6cd5\u3002<\/li>\n<li><strong>\u6784\u9020\u65b9\u6cd5<\/strong>\uff1a\u4f7f\u7528getConstructor\u6216getDeclaredConstructor\u5206\u522b\u8bbf\u95ee\u516c\u5171\u6784\u9020\u65b9\u6cd5\u6216\u6240\u6709\u6784\u9020\u65b9\u6cd5\u3002<\/li>\n<\/ul>\n<h3 dir=\"auto\">\u7ed5\u8fc7\u8bbf\u95ee\u9650\u5236<\/h3>\n<p dir=\"auto\">\u901a\u8fc7\u5728Field\u3001Method\u6216Constructor\u5bf9\u8c61\u4e0a\u8bbe\u7f6esetAccessible(true)\uff0c\u53cd\u5c04\u53ef\u4ee5\u7ed5\u8fc7Java\u7684\u8bbf\u95ee\u63a7\u5236\uff0c\u8bbf\u95ee\u79c1\u6709\u6210\u5458\u3002\u4f46\u5e94\u8c28\u614e\u4f7f\u7528\uff0c\u4ee5\u514d\u7834\u574f\u5c01\u88c5\u6027\u3002<\/p>\n<h2 dir=\"auto\">\u53cd\u5c04\u7684\u6027\u80fd\u8003\u8651<\/h2>\n<p dir=\"auto\">\u53cd\u5c04\u5e26\u6765\u6027\u80fd\u5f00\u9500\u7684\u539f\u56e0\u5305\u62ec\uff1a<\/p>\n<ul dir=\"auto\">\n<li><strong>\u52a8\u6001\u89e3\u6790<\/strong>\uff1a\u53cd\u5c04\u5728\u8fd0\u884c\u65f6\u89e3\u6790\u7c7b\u578b\uff0c\u65e0\u6cd5\u5229\u7528JVM\u4f18\u5316\uff08\u5982\u65b9\u6cd5\u5185\u8054\uff09\u3002<\/li>\n<li><strong>\u5bf9\u8c61\u6570\u7ec4\u521b\u5efa<\/strong>\uff1a\u5982Method.invoke\u4f7f\u7528\u53d8\u957f\u53c2\u6570\u6570\u7ec4\uff0c\u5bfc\u81f4\u5bf9\u8c61\u521b\u5efa\u5f00\u9500\u3002<\/li>\n<li><strong>\u81ea\u52a8\u88c5\u7bb1<\/strong>\uff1a\u5c06\u57fa\u672c\u7c7b\u578b\u4f20\u9012\u7ed9\u53cd\u5c04\u8c03\u7528\u9700\u8981\u81ea\u52a8\u88c5\u7bb1\uff0c\u589e\u52a0\u5185\u5b58\u4f7f\u7528\u3002<\/li>\n<\/ul>\n<p dir=\"auto\">\u7f13\u89e3\u6027\u80fd\u95ee\u9898\u7684\u65b9\u6cd5\uff1a<\/p>\n<ul dir=\"auto\">\n<li><strong>\u7f13\u5b58\u7ed3\u679c<\/strong>\uff1a\u5b58\u50a8Class\u548cMethod\u5bf9\u8c61\uff0c\u907f\u514d\u91cd\u590d\u67e5\u627e\u3002<\/li>\n<li><strong>\u9650\u5236\u4f7f\u7528<\/strong>\uff1a\u907f\u514d\u5728\u6027\u80fd\u654f\u611f\u7684\u4ee3\u7801\u8def\u5f84\u4e2d\u4f7f\u7528\u53cd\u5c04\u3002<\/li>\n<li><strong>\u4f7f\u7528\u66ff\u4ee3\u65b9\u6848<\/strong>\uff1a\u8003\u8651\u76f4\u63a5\u65b9\u6cd5\u8c03\u7528\u6216\u7f16\u8bd1\u5b57\u8282\u7801\u4ee5\u4f18\u5316\u9891\u7e41\u6267\u884c\u7684\u64cd\u4f5c\u3002<\/li>\n<\/ul>\n<h3 dir=\"auto\">\u793a\u4f8b\uff1a\u53cd\u5c04\u65b9\u6cd5\u8c03\u7528<\/h3>\n<p dir=\"auto\">\u4ee5\u4e0b\u793a\u4f8b\u5c55\u793a\u4e86\u5982\u4f55\u4f7f\u7528\u53cd\u5c04\u8c03\u7528\u65b9\u6cd5\uff1a<\/p>\n<div dir=\"auto\">\n<div>\n<div>java<\/div>\n<div>\n<div>\n<div><\/div>\n<\/div>\n<\/div>\n<div><code><\/code><\/p>\n<div>\n<div>\n<div>import java.lang.reflect.Method;<\/div>\n<div><\/div>\n<div>public class ReflectionExample {<\/div>\n<div>public static void printMessage(String message) {<\/div>\n<div>System.out.println(&#8220;\u6d88\u606f\uff1a&#8221; + message);<\/div>\n<div>}<\/div>\n<div><\/div>\n<div>public static void main(String[] args) throws Exception {<\/div>\n<div>Class&lt;?&gt; clazz = Class.forName(&#8220;ReflectionExample&#8221;);<\/div>\n<div>Method method = clazz.getMethod(&#8220;printMessage&#8221;, String.class);<\/div>\n<div>method.invoke(null, &#8220;\u4f60\u597d\uff0c\u53cd\u5c04\uff01&#8221;);<\/div>\n<div>}<\/div>\n<div>}<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div><\/div>\n<div><\/div>\n<\/div>\n<\/div>\n<p dir=\"auto\"><strong>\u8f93\u51fa<\/strong>\uff1a<\/p>\n<div dir=\"auto\">\n<div>\n<div>text<\/div>\n<div>\n<div>\n<div><\/div>\n<\/div>\n<\/div>\n<div><code><\/code><\/p>\n<div>\n<div>\n<div>\u6d88\u606f\uff1a\u4f60\u597d\uff0c\u53cd\u5c04\uff01<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div><\/div>\n<div><\/div>\n<\/div>\n<\/div>\n<p dir=\"auto\">\u6b64\u793a\u4f8b\u5c55\u793a\u5982\u4f55\u901a\u8fc7\u53cd\u5c04\u52a8\u6001\u8c03\u7528printMessage\u65b9\u6cd5\u3002<\/p>\n<h2 dir=\"auto\">Java\u4e2d\u7684\u52a8\u6001\u4ee3\u7406<\/h2>\n<p dir=\"auto\">\u52a8\u6001\u4ee3\u7406\u63d0\u4f9b\u4e86\u4e00\u79cd\u5728\u8fd0\u884c\u65f6\u521b\u5efa\u4ee3\u7406\u5bf9\u8c61\u4ee5\u62e6\u622a\u65b9\u6cd5\u8c03\u7528\u5e76\u589e\u5f3a\u529f\u80fd\u7684\u673a\u5236\uff0c\u4f8b\u5982\u65e5\u5fd7\u8bb0\u5f55\u6216\u4e8b\u52a1\u7ba1\u7406\u3002<\/p>\n<h3 dir=\"auto\">\u9759\u6001\u4ee3\u7406\u4e0e\u52a8\u6001\u4ee3\u7406<\/h3>\n<ul dir=\"auto\">\n<li><strong>\u9759\u6001\u4ee3\u7406<\/strong>\uff1a\u5728\u7f16\u8bd1\u65f6\u521b\u5efa\u4ee3\u7406\u7c7b\uff0c\u53ef\u80fd\u5bfc\u81f4\u4ee3\u7801\u91cd\u590d\u548c\u7ef4\u62a4\u56f0\u96be\u3002<\/li>\n<li><strong>\u52a8\u6001\u4ee3\u7406<\/strong>\uff1a\u5728\u8fd0\u884c\u65f6\u751f\u6210\u4ee3\u7406\u7c7b\uff0c\u63d0\u4f9b\u66f4\u5927\u7684\u7075\u6d3b\u6027\u5e76\u51cf\u5c11\u4ee3\u7801\u5197\u4f59\u3002<\/li>\n<\/ul>\n<h3 dir=\"auto\">JDK\u52a8\u6001\u4ee3\u7406<\/h3>\n<p dir=\"auto\">JDK\u52a8\u6001\u4ee3\u7406\u4f9d\u8d56\u4e8ejava.lang.reflect.Proxy\u7c7b\u548cInvocationHandler\u63a5\u53e3\uff0c\u901a\u8fc7\u52a8\u6001\u5b9e\u73b0\u63a5\u53e3\u6765\u5de5\u4f5c\u3002<\/p>\n<h4 dir=\"auto\">\u5173\u952e\u7ec4\u4ef6<\/h4>\n<ul dir=\"auto\">\n<li><strong>InvocationHandler<\/strong>\uff1a\u5b9a\u4e49invoke\u65b9\u6cd5\u4ee5\u5904\u7406\u4ee3\u7406\u5bf9\u8c61\u4e0a\u7684\u65b9\u6cd5\u8c03\u7528\u3002<\/li>\n<li><strong>Proxy<\/strong>\uff1a\u63d0\u4f9bnewProxyInstance\u65b9\u6cd5\u4ee5\u521b\u5efa\u4ee3\u7406\u5bf9\u8c61\u3002<\/li>\n<\/ul>\n<h4 dir=\"auto\">\u521b\u5efaJDK\u52a8\u6001\u4ee3\u7406\u7684\u6b65\u9aa4<\/h4>\n<ol dir=\"auto\">\n<li>\u5b9a\u4e49\u63a5\u53e3\u53ca\u5176\u5b9e\u73b0\uff08\u771f\u5b9e\u4e3b\u9898\uff09\u3002<\/li>\n<li>\u521b\u5efaInvocationHandler\u6765\u5904\u7406\u65b9\u6cd5\u8c03\u7528\u3002<\/li>\n<li>\u4f7f\u7528Proxy.newProxyInstance\u751f\u6210\u4ee3\u7406\u5bf9\u8c61\u3002<\/li>\n<\/ol>\n<h4 dir=\"auto\">\u793a\u4f8b\uff1aJDK\u52a8\u6001\u4ee3\u7406<\/h4>\n<div dir=\"auto\">\n<div>\n<div>java<\/div>\n<div>\n<div>\n<div><\/div>\n<\/div>\n<\/div>\n<div><code><\/code><\/p>\n<div>\n<div>\n<div>import java.lang.reflect.InvocationHandler;<\/div>\n<div>import java.lang.reflect.Method;<\/div>\n<div>import java.lang.reflect.Proxy;<\/div>\n<div><\/div>\n<div>interface Service {<\/div>\n<div>void performTask(String task);<\/div>\n<div>}<\/div>\n<div><\/div>\n<div>class RealService implements Service {<\/div>\n<div>public void performTask(String task) {<\/div>\n<div>System.out.println(&#8220;\u6267\u884c\u4efb\u52a1\uff1a&#8221; + task);<\/div>\n<div>}<\/div>\n<div>}<\/div>\n<div><\/div>\n<div>class ServiceHandler implements InvocationHandler {<\/div>\n<div>private final Object target;<\/div>\n<div><\/div>\n<div>public ServiceHandler(Object target) {<\/div>\n<div>this.target = target;<\/div>\n<div>}<\/div>\n<div><\/div>\n<div>@Override<\/div>\n<div>public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {<\/div>\n<div>System.out.println(&#8220;\u8c03\u7528&#8221; + method.getName() + &#8220;\u4e4b\u524d&#8221;);<\/div>\n<div>Object result = method.invoke(target, args);<\/div>\n<div>System.out.println(&#8220;\u8c03\u7528&#8221; + method.getName() + &#8220;\u4e4b\u540e&#8221;);<\/div>\n<div>return result;<\/div>\n<div>}<\/div>\n<div>}<\/div>\n<div><\/div>\n<div>public class DynamicProxyExample {<\/div>\n<div>public static void main(String[] args) {<\/div>\n<div>Service realService = new RealService();<\/div>\n<div>InvocationHandler handler = new ServiceHandler(realService);<\/div>\n<div>Service proxy = (Service) Proxy.newProxyInstance(<\/div>\n<div>realService.getClass().getClassLoader(),<\/div>\n<div>realService.getClass().getInterfaces(),<\/div>\n<div>handler<\/div>\n<div>);<\/div>\n<div>proxy.performTask(&#8220;\u5904\u7406\u6570\u636e&#8221;);<\/div>\n<div>}<\/div>\n<div>}<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div><\/div>\n<div><\/div>\n<\/div>\n<\/div>\n<p dir=\"auto\"><strong>\u8f93\u51fa<\/strong>\uff1a<\/p>\n<div dir=\"auto\">\n<div>\n<div>text<\/div>\n<div>\n<div>\n<div><\/div>\n<\/div>\n<\/div>\n<div><code><\/code><\/p>\n<div>\n<div>\n<div>\u8c03\u7528performTask\u4e4b\u524d<\/div>\n<div>\u6267\u884c\u4efb\u52a1\uff1a\u5904\u7406\u6570\u636e<\/div>\n<div>\u8c03\u7528performTask\u4e4b\u540e<\/div>\n<\/div>\n<\/div>\n<\/div>\n<div><\/div>\n<div><\/div>\n<\/div>\n<\/div>\n<p dir=\"auto\">\u6b64\u793a\u4f8b\u5c55\u793a\u4ee3\u7406\u5982\u4f55\u62e6\u622a\u65b9\u6cd5\u8c03\u7528\u5e76\u6dfb\u52a0\u524d\u540e\u5904\u7406\u903b\u8f91\u3002<\/p>\n<h3 dir=\"auto\">CGLIB\u52a8\u6001\u4ee3\u7406<\/h3>\n<p dir=\"auto\">CGLIB\uff08Code Generation Library\uff09\u662f\u4e00\u79cd\u66ff\u4ee3JDK\u52a8\u6001\u4ee3\u7406\u7684\u65b9\u6848\uff0c\u901a\u8fc7\u5b57\u8282\u7801\u64cd\u4f5c\u751f\u6210\u76ee\u6807\u7c7b\u7684\u5b50\u7c7b\u3002<\/p>\n<h4 dir=\"auto\">\u5173\u952e\u7279\u6027<\/h4>\n<ul dir=\"auto\">\n<li><strong>\u65e0\u9700\u63a5\u53e3<\/strong>\uff1a\u4e0eJDK\u4ee3\u7406\u4e0d\u540c\uff0cCGLIB\u53ef\u4ee5\u4ee3\u7406\u65e0\u9700\u5b9e\u73b0\u63a5\u53e3\u7684\u7c7b\u3002<\/li>\n<li><strong>\u6027\u80fd<\/strong>\uff1a\u7531\u4e8e\u4f7f\u7528\u5b57\u8282\u7801\u751f\u6210\uff0cCGLIB\u901a\u5e38\u6bd4JDK\u4ee3\u7406\u66f4\u5feb\u3002<\/li>\n<li><strong>\u9650\u5236<\/strong>\uff1a\u65e0\u6cd5\u4ee3\u7406final\u7c7b\u6216\u65b9\u6cd5\u3002<\/li>\n<\/ul>\n<h4 dir=\"auto\">CGLIB\u4e0eJDK\u52a8\u6001\u4ee3\u7406\u5bf9\u6bd4<\/h4>\n<div dir=\"auto\">\n<div>\n<div><\/div>\n<\/div>\n<table dir=\"auto\" data-wide=\"false\">\n<thead>\n<tr>\n<th data-col-size=\"xs\">\u7279\u6027<\/th>\n<th data-col-size=\"lg\">JDK\u52a8\u6001\u4ee3\u7406<\/th>\n<th data-col-size=\"md\">CGLIB\u52a8\u6001\u4ee3\u7406<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td data-col-size=\"xs\"><strong>\u673a\u5236<\/strong><\/td>\n<td data-col-size=\"lg\">\u57fa\u4e8e\u63a5\u53e3<\/td>\n<td data-col-size=\"md\">\u57fa\u4e8e\u5b50\u7c7b<\/td>\n<\/tr>\n<tr>\n<td data-col-size=\"xs\"><strong>\u6027\u80fd<\/strong><\/td>\n<td data-col-size=\"lg\">\u56e0\u53cd\u5c04\u8f83\u6162<\/td>\n<td data-col-size=\"md\">\u56e0\u5b57\u8282\u7801\u751f\u6210\u8f83\u5feb<\/td>\n<\/tr>\n<tr>\n<td data-col-size=\"xs\"><strong>\u63a5\u53e3\u8981\u6c42<\/strong><\/td>\n<td data-col-size=\"lg\">\u5fc5\u987b\u5b9e\u73b0\u63a5\u53e3<\/td>\n<td data-col-size=\"md\">\u53ef\u9009<\/td>\n<\/tr>\n<tr>\n<td data-col-size=\"xs\"><strong>Final\u7c7b\/\u65b9\u6cd5<\/strong><\/td>\n<td data-col-size=\"lg\">\u652f\u6301<\/td>\n<td data-col-size=\"md\">\u4e0d\u652f\u6301<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<h3 dir=\"auto\">\u52a8\u6001\u4ee3\u7406\u7684\u5b9e\u9645\u8003\u8651<\/h3>\n<ul dir=\"auto\">\n<li><strong>\u4f7f\u7528\u573a\u666f<\/strong>\uff1a\u52a8\u6001\u4ee3\u7406\u9002\u7528\u4e8eAOP\u3001\u65e5\u5fd7\u8bb0\u5f55\u548c\u4e8b\u52a1\u7ba1\u7406\uff0c\u5982\u5728Spring\u6846\u67b6\u4e2d\u3002<\/li>\n<li><strong>\u6027\u80fd<\/strong>\uff1aJDK\u4ee3\u7406\u5b9e\u73b0\u7b80\u5355\u4f46\u8f83\u6162\uff1bCGLIB\u66f4\u5feb\u4f46\u9700\u8981\u989d\u5916\u4f9d\u8d56\u3002<\/li>\n<li><strong>\u6700\u4f73\u5b9e\u8df5<\/strong>\uff1a\u57fa\u4e8e\u63a5\u53e3\u7684\u8bbe\u8ba1\u4f7f\u7528JDK\u4ee3\u7406\uff0c\u65e0\u63a5\u53e3\u7c7b\u4f7f\u7528CGLIB\u3002<\/li>\n<\/ul>\n<h2 dir=\"auto\">\u7ed3\u8bba<\/h2>\n<p dir=\"auto\">Java\u53cd\u5c04\u548c\u52a8\u6001\u4ee3\u7406\u662f\u6784\u5efa\u7075\u6d3b\u3001\u6269\u5c55\u6027\u548c\u52a8\u6001\u5e94\u7528\u7a0b\u5e8f\u4e0d\u53ef\u6216\u7f3a\u7684\u5de5\u5177\u3002\u53cd\u5c04\u652f\u6301\u8fd0\u884c\u65f6\u68c0\u67e5\u548c\u64cd\u4f5c\u7c7b\u7ed3\u6784\uff0c\u800c\u52a8\u6001\u4ee3\u7406\u901a\u8fc7\u65b9\u6cd5\u62e6\u622a\u5b9e\u73b0\u589e\u5f3a\u529f\u80fd\u3002\u901a\u8fc7\u7406\u89e3\u5176\u673a\u5236\u3001\u5e94\u7528\u573a\u666f\u548c\u6027\u80fd\u5f71\u54cd\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\u5728\u6846\u67b6\u3001\u5de5\u5177\u548c\u9ad8\u7ea7\u7f16\u7a0b\u573a\u666f\u4e2d\u6709\u6548\u5229\u7528\u8fd9\u4e9b\u7279\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Java\u53cd\u5c04\u7b80\u4ecb Java\u53cd\u5c04\u662f&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-208381","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/208381","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=208381"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/208381\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=208381"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=208381"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=208381"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}