{"id":73667,"date":"2026-02-08T06:41:39","date_gmt":"2026-02-07T22:41:39","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/73667.html"},"modified":"2026-02-08T06:41:39","modified_gmt":"2026-02-07T22:41:39","slug":"springaop%e5%8a%a8%e6%80%81%e4%bb%a3%e7%90%86%ef%bc%9ajdk%e4%b8%8ecglib%e6%b7%b1%e5%ba%a6%e8%a7%a3%e6%9e%90","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/73667.html","title":{"rendered":"SpringAOP\u52a8\u6001\u4ee3\u7406\uff1aJDK\u4e0eCGLib\u6df1\u5ea6\u89e3\u6790"},"content":{"rendered":"<p>\u597d\u7684&#xff0c;\u6211\u4eec\u6765\u6df1\u5165\u89e3\u6790 Spring AOP \u7684\u6838\u5fc3\u539f\u7406\u4e4b\u4e00&#xff1a;\u52a8\u6001\u4ee3\u7406&#xff0c;\u5e76\u91cd\u70b9\u5bf9\u6bd4 JDK \u52a8\u6001\u4ee3\u7406 \u548c CGLib \u52a8\u6001\u4ee3\u7406 \u7684\u5b9e\u73b0\u65b9\u5f0f\u3001\u533a\u522b\u4ee5\u53ca\u5b9e\u6218\u5e94\u7528\u3002<\/p>\n<p>\u6838\u5fc3\u6982\u5ff5&#xff1a;\u52a8\u6001\u4ee3\u7406\u4e0e AOP<\/p>\n<p>Spring AOP (Aspect-Oriented Programming) \u7684\u6838\u5fc3\u76ee\u6807\u662f\u5c06\u6a2a\u5207\u5173\u6ce8\u70b9&#xff08;\u5982\u65e5\u5fd7\u8bb0\u5f55\u3001\u4e8b\u52a1\u7ba1\u7406\u3001\u5b89\u5168\u68c0\u67e5\u7b49&#xff09;\u4ece\u6838\u5fc3\u4e1a\u52a1\u903b\u8f91\u4e2d\u5206\u79bb\u51fa\u6765\u3002\u4e3a\u4e86\u5b9e\u73b0\u8fd9\u79cd\u975e\u4fb5\u5165\u5f0f\u7684\u589e\u5f3a&#xff0c;Spring \u4e3b\u8981\u4f9d\u8d56\u4e8e \u52a8\u6001\u4ee3\u7406 \u6280\u672f\u3002<\/p>\n<ul>\n<li>\u52a8\u6001\u4ee3\u7406&#xff1a;\u5728\u7a0b\u5e8f\u8fd0\u884c\u65f6\u52a8\u6001\u5730\u521b\u5efa\u4e00\u4e2a\u5b9e\u73b0\u7279\u5b9a\u63a5\u53e3\u6216\u7ee7\u627f\u7279\u5b9a\u7c7b\u7684\u4ee3\u7406\u5bf9\u8c61\u3002\u5f53\u901a\u8fc7\u8fd9\u4e2a\u4ee3\u7406\u5bf9\u8c61\u8c03\u7528\u76ee\u6807\u65b9\u6cd5\u65f6&#xff0c;\u4ee3\u7406\u5bf9\u8c61\u53ef\u4ee5\u5728\u8c03\u7528\u76ee\u6807\u65b9\u6cd5\u7684\u524d\u540e\u63d2\u5165\u989d\u5916\u7684\u903b\u8f91&#xff08;Advice &#8211; \u589e\u5f3a&#xff09;\u3002<\/li>\n<li>AOP \u4e2d\u7684\u89d2\u8272&#xff1a;\n<ul>\n<li>\u76ee\u6807\u5bf9\u8c61 (Target Object)&#xff1a;\u5305\u542b\u6838\u5fc3\u4e1a\u52a1\u903b\u8f91\u7684\u9700\u8981\u88ab\u589e\u5f3a\u7684\u5bf9\u8c61\u3002<\/li>\n<li>\u4ee3\u7406\u5bf9\u8c61 (Proxy Object)&#xff1a;\u7531 AOP \u6846\u67b6\u521b\u5efa\u7684\u5bf9\u8c61&#xff0c;\u5b83\u5305\u88f9\u4e86\u76ee\u6807\u5bf9\u8c61&#xff0c;\u5e76\u8d1f\u8d23\u5728\u8c03\u7528\u76ee\u6807\u65b9\u6cd5\u524d\u540e\u6267\u884c\u589e\u5f3a\u903b\u8f91\u3002<\/li>\n<li>\u8fde\u63a5\u70b9 (Joinpoint)&#xff1a;\u7a0b\u5e8f\u6267\u884c\u8fc7\u7a0b\u4e2d\u7684\u4e00\u4e2a\u70b9&#xff0c;\u4f8b\u5982\u65b9\u6cd5\u8c03\u7528\u6216\u5f02\u5e38\u629b\u51fa\u3002\u5728 Spring AOP \u4e2d&#xff0c;\u4e3b\u8981\u6307\u65b9\u6cd5\u8c03\u7528\u3002<\/li>\n<li>\u5207\u70b9 (Pointcut)&#xff1a;\u4e00\u4e2a\u8868\u8fbe\u5f0f&#xff0c;\u7528\u4e8e\u5339\u914d\u54ea\u4e9b\u8fde\u63a5\u70b9\u9700\u8981\u88ab\u589e\u5f3a\u3002<\/li>\n<li>\u589e\u5f3a (Advice)&#xff1a;\u5728\u7279\u5b9a\u8fde\u63a5\u70b9\u6267\u884c\u7684\u52a8\u4f5c&#xff08;\u5982\u524d\u7f6e\u901a\u77e5\u3001\u540e\u7f6e\u901a\u77e5\u3001\u73af\u7ed5\u901a\u77e5\u7b49&#xff09;\u3002<\/li>\n<li>\u5207\u9762 (Aspect)&#xff1a;\u5207\u70b9\u548c\u589e\u5f3a\u7684\u7ed3\u5408\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p>Spring AOP \u5728\u5e95\u5c42\u4f7f\u7528\u4e24\u79cd\u52a8\u6001\u4ee3\u7406\u6280\u672f\u6765\u521b\u5efa\u8fd9\u4e9b\u4ee3\u7406\u5bf9\u8c61&#xff1a;JDK \u52a8\u6001\u4ee3\u7406 \u548c CGLib \u52a8\u6001\u4ee3\u7406\u3002<\/p>\n<hr \/>\n<p>1. JDK \u52a8\u6001\u4ee3\u7406<\/p>\n<p>\u539f\u7406&#xff1a; JDK \u52a8\u6001\u4ee3\u7406\u662f Java \u6807\u51c6\u5e93 (java.lang.reflect \u5305) \u63d0\u4f9b\u7684\u529f\u80fd\u3002\u5b83\u8981\u6c42\u76ee\u6807\u5bf9\u8c61\u5fc5\u987b\u5b9e\u73b0\u81f3\u5c11\u4e00\u4e2a\u63a5\u53e3\u3002\u4ee3\u7406\u5bf9\u8c61\u5728\u8fd0\u884c\u65f6\u52a8\u6001\u751f\u6210&#xff0c;\u5b83\u4f1a\u5b9e\u73b0\u76ee\u6807\u5bf9\u8c61\u6240\u5b9e\u73b0\u7684\u63a5\u53e3\u3002<\/p>\n<p>\u5b9e\u73b0\u673a\u5236&#xff1a;<\/p>\n<ul>\n<li>\u6838\u5fc3\u7c7b\u662f java.lang.reflect.Proxy \u548c java.lang.reflect.InvocationHandler\u3002<\/li>\n<li>Proxy.newProxyInstance(ClassLoader loader, Class&lt;?&gt;[] interfaces, InvocationHandler h) \u65b9\u6cd5\u7528\u4e8e\u521b\u5efa\u4ee3\u7406\u5bf9\u8c61\u3002\n<ul>\n<li>loader: \u7c7b\u52a0\u8f7d\u5668\u3002<\/li>\n<li>interfaces: \u76ee\u6807\u5bf9\u8c61\u5b9e\u73b0\u7684\u63a5\u53e3\u5217\u8868\u3002<\/li>\n<li>h: \u5b9e\u73b0\u4e86 InvocationHandler \u63a5\u53e3\u7684\u5bf9\u8c61&#xff0c;\u5b83\u5b9a\u4e49\u4e86\u4ee3\u7406\u5bf9\u8c61\u62e6\u622a\u65b9\u6cd5\u8c03\u7528\u7684\u903b\u8f91\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\u5f53\u901a\u8fc7\u4ee3\u7406\u5bf9\u8c61\u8c03\u7528\u63a5\u53e3\u65b9\u6cd5\u65f6&#xff0c;\u8fd9\u4e2a\u8c03\u7528\u4f1a\u88ab\u8f6c\u53d1\u7ed9 InvocationHandler \u7684 invoke \u65b9\u6cd5&#xff1a; public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {<br \/>\n    \/\/ \u524d\u7f6e\u589e\u5f3a\u903b\u8f91 (Before Advice)<br \/>\n    Object result &#061; method.invoke(target, args); \/\/ \u8c03\u7528\u76ee\u6807\u5bf9\u8c61\u7684\u65b9\u6cd5<br \/>\n    \/\/ \u540e\u7f6e\u589e\u5f3a\u903b\u8f91 (After Advice)<br \/>\n    return result;<br \/>\n}<\/p>\n<p>http:\/\/my.tv.sohu.com\/us\/442338213\/699359606.shtml https:\/\/tv.sohu.com\/v\/dXMvNDQyMzM4MjEzLzY5OTM1OTYwNi5zaHRtbA&#061;&#061;.html http:\/\/my.tv.sohu.com\/us\/442338213\/699359615.shtml https:\/\/tv.sohu.com\/v\/dXMvNDQyMzM4MjEzLzY5OTM1OTYxNS5zaHRtbA&#061;&#061;.html http:\/\/my.tv.sohu.com\/us\/442338213\/699359712.shtml https:\/\/tv.sohu.com\/v\/dXMvNDQyMzM4MjEzLzY5OTM1OTcxMi5zaHRtbA&#061;&#061;.html http:\/\/my.tv.sohu.com\/us\/442338213\/699359631.shtml https:\/\/tv.sohu.com\/v\/dXMvNDQyMzM4MjEzLzY5OTM1OTYzMS5zaHRtbA&#061;&#061;.html http:\/\/my.tv.sohu.com\/us\/442338213\/699359809.shtml https:\/\/tv.sohu.com\/v\/dXMvNDQyMzM4MjEzLzY5OTM1OTgwOS5zaHRtbA&#061;&#061;.html http:\/\/my.tv.sohu.com\/us\/442338213\/699359725.shtml https:\/\/tv.sohu.com\/v\/dXMvNDQyMzM4MjEzLzY5OTM1OTcyNS5zaHRtbA&#061;&#061;.html http:\/\/my.tv.sohu.com\/us\/442338213\/699359820.shtml https:\/\/tv.sohu.com\/v\/dXMvNDQyMzM4MjEzLzY5OTM1OTgyMC5zaHRtbA&#061;&#061;.html http:\/\/my.tv.sohu.com\/us\/442338213\/699359824.shtml https:\/\/tv.sohu.com\/v\/dXMvNDQyMzM4MjEzLzY5OTM1OTgyNC5zaHRtbA&#061;&#061;.html http:\/\/my.tv.sohu.com\/us\/442338213\/699359921.shtml https:\/\/tv.sohu.com\/v\/dXMvNDQyMzM4MjEzLzY5OTM1OTkyMS5zaHRtbA&#061;&#061;.html http:\/\/my.tv.sohu.com\/us\/442338213\/699359923.shtml https:\/\/tv.sohu.com\/v\/dXMvNDQyMzM4MjEzLzY5OTM1OTkyMy5zaHRtbA&#061;&#061;.html http:\/\/my.tv.sohu.com\/us\/442338213\/699359832.shtml https:\/\/tv.sohu.com\/v\/dXMvNDQyMzM4MjEzLzY5OTM1OTgzMi5zaHRtbA&#061;&#061;.html http:\/\/my.tv.sohu.com\/us\/442338213\/699359663.shtml https:\/\/tv.sohu.com\/v\/dXMvNDQyMzM4MjEzLzY5OTM1OTY2My5zaHRtbA&#061;&#061;.html http:\/\/my.tv.sohu.com\/us\/442338213\/699359845.shtml https:\/\/tv.sohu.com\/v\/dXMvNDQyMzM4MjEzLzY5OTM1OTg0NS5zaHRtbA&#061;&#061;.html http:\/\/my.tv.sohu.com\/us\/442338213\/699359752.shtml https:\/\/tv.sohu.com\/v\/dXMvNDQyMzM4MjEzLzY5OTM1OTc1Mi5zaHRtbA&#061;&#061;.html http:\/\/my.tv.sohu.com\/us\/442338213\/699359849.shtml https:\/\/tv.sohu.com\/v\/dXMvNDQyMzM4MjEzLzY5OTM1OTg0OS5zaHRtbA&#061;&#061;.html http:\/\/my.tv.sohu.com\/us\/442338213\/699359939.shtml https:\/\/tv.sohu.com\/v\/dXMvNDQyMzM4MjEzLzY5OTM1OTkzOS5zaHRtbA&#061;&#061;.html http:\/\/my.tv.sohu.com\/us\/442338213\/699359676.shtml https:\/\/tv.sohu.com\/v\/dXMvNDQyMzM4MjEzLzY5OTM1OTY3Ni5zaHRtbA&#061;&#061;.html http:\/\/my.tv.sohu.com\/us\/442338213\/699359859.shtml https:\/\/tv.sohu.com\/v\/dXMvNDQyMzM4MjEzLzY5OTM1OTg1OS5zaHRtbA&#061;&#061;.html http:\/\/my.tv.sohu.com\/us\/442338213\/699359683.shtml https:\/\/tv.sohu.com\/v\/dXMvNDQyMzM4MjEzLzY5OTM1OTY4My5zaHRtbA&#061;&#061;.html http:\/\/my.tv.sohu.com\/us\/442338213\/699359775.shtml https:\/\/tv.sohu.com\/v\/dXMvNDQyMzM4MjEzLzY5OTM1OTc3NS5zaHRtbA&#061;&#061;.html http:\/\/my.tv.sohu.com\/us\/442338213\/699359973.shtml https:\/\/tv.sohu.com\/v\/dXMvNDQyMzM4MjEzLzY5OTM1OTk3My5zaHRtbA&#061;&#061;.html http:\/\/my.tv.sohu.com\/us\/442338213\/699359790.shtml https:\/\/tv.sohu.com\/v\/dXMvNDQyMzM4MjEzLzY5OTM1OTc5MC5zaHRtbA&#061;&#061;.html http:\/\/my.tv.sohu.com\/us\/442338213\/699360205.shtml https:\/\/tv.sohu.com\/v\/dXMvNDQyMzM4MjEzLzY5OTM2MDIwNS5zaHRtbA&#061;&#061;.html http:\/\/my.tv.sohu.com\/us\/442338213\/699360103.shtml https:\/\/tv.sohu.com\/v\/dXMvNDQyMzM4MjEzLzY5OTM2MDEwMy5zaHRtbA&#061;&#061;.html http:\/\/my.tv.sohu.com\/us\/442338213\/699360026.shtml https:\/\/tv.sohu.com\/v\/dXMvNDQyMzM4MjEzLzY5OTM2MDAyNi5zaHRtbA&#061;&#061;.html http:\/\/my.tv.sohu.com\/us\/442338213\/699360000.shtml https:\/\/tv.sohu.com\/v\/dXMvNDQyMzM4MjEzLzY5OTM2MDAwMC5zaHRtbA&#061;&#061;.html http:\/\/my.tv.sohu.com\/us\/442338213\/699360035.shtml https:\/\/tv.sohu.com\/v\/dXMvNDQyMzM4MjEzLzY5OTM2MDAzNS5zaHRtbA&#061;&#061;.html http:\/\/my.tv.sohu.com\/us\/442338213\/699360125.shtml https:\/\/tv.sohu.com\/v\/dXMvNDQyMzM4MjEzLzY5OTM2MDEyNS5zaHRtbA&#061;&#061;.html http:\/\/my.tv.sohu.com\/us\/442338213\/699360042.shtml https:\/\/tv.sohu.com\/v\/dXMvNDQyMzM4MjEzLzY5OTM2MDA0Mi5zaHRtbA&#061;&#061;.html http:\/\/my.tv.sohu.com\/us\/442338213\/699360136.shtml https:\/\/tv.sohu.com\/v\/dXMvNDQyMzM4MjEzLzY5OTM2MDEzNi5zaHRtbA&#061;&#061;.html http:\/\/my.tv.sohu.com\/us\/442338213\/699360142.shtml https:\/\/tv.sohu.com\/v\/dXMvNDQyMzM4MjEzLzY5OTM2MDE0Mi5zaHRtbA&#061;&#061;.html http:\/\/my.tv.sohu.com\/us\/442338213\/699360056.shtml https:\/\/tv.sohu.com\/v\/dXMvNDQyMzM4MjEzLzY5OTM2MDA1Ni5zaHRtbA&#061;&#061;.html http:\/\/my.tv.sohu.com\/us\/442338213\/699360060.shtml https:\/\/tv.sohu.com\/v\/dXMvNDQyMzM4MjEzLzY5OTM2MDA2MC5zaHRtbA&#061;&#061;.html http:\/\/my.tv.sohu.com\/us\/442338213\/699360065.shtml https:\/\/tv.sohu.com\/v\/dXMvNDQyMzM4MjEzLzY5OTM2MDA2NS5zaHRtbA&#061;&#061;.html http:\/\/my.tv.sohu.com\/us\/442338213\/699360162.shtml https:\/\/tv.sohu.com\/v\/dXMvNDQyMzM4MjEzLzY5OTM2MDE2Mi5zaHRtbA&#061;&#061;.html http:\/\/my.tv.sohu.com\/us\/442338213\/699360353.shtml https:\/\/tv.sohu.com\/v\/dXMvNDQyMzM4MjEzLzY5OTM2MDM1My5zaHRtbA&#061;&#061;.html http:\/\/my.tv.sohu.com\/us\/442338213\/699360272.shtml https:\/\/tv.sohu.com\/v\/dXMvNDQyMzM4MjEzLzY5OTM2MDI3Mi5zaHRtbA&#061;&#061;.html http:\/\/my.tv.sohu.com\/us\/442338213\/699360179.shtml https:\/\/tv.sohu.com\/v\/dXMvNDQyMzM4MjEzLzY5OTM2MDE3OS5zaHRtbA&#061;&#061;.html http:\/\/my.tv.sohu.com\/us\/442338213\/699360275.shtml https:\/\/tv.sohu.com\/v\/dXMvNDQyMzM4MjEzLzY5OTM2MDI3NS5zaHRtbA&#061;&#061;.html http:\/\/my.tv.sohu.com\/us\/442338213\/699360279.shtml https:\/\/tv.sohu.com\/v\/dXMvNDQyMzM4MjEzLzY5OTM2MDI3OS5zaHRtbA&#061;&#061;.html http:\/\/my.tv.sohu.com\/us\/442338213\/699360185.shtml https:\/\/tv.sohu.com\/v\/dXMvNDQyMzM4MjEzLzY5OTM2MDE4NS5zaHRtbA&#061;&#061;.html http:\/\/my.tv.sohu.com\/us\/442338213\/699360187.shtml https:\/\/tv.sohu.com\/v\/dXMvNDQyMzM4MjEzLzY5OTM2MDE4Ny5zaHRtbA&#061;&#061;.html http:\/\/my.tv.sohu.com\/us\/442338213\/699360190.shtml https:\/\/tv.sohu.com\/v\/dXMvNDQyMzM4MjEzLzY5OTM2MDE5MC5zaHRtbA&#061;&#061;.html http:\/\/my.tv.sohu.com\/us\/442338213\/699360375.shtml https:\/\/tv.sohu.com\/v\/dXMvNDQyMzM4MjEzLzY5OTM2MDM3NS5zaHRtbA&#061;&#061;.html http:\/\/my.tv.sohu.com\/us\/442338213\/699360378.shtml https:\/\/tv.sohu.com\/v\/dXMvNDQyMzM4MjEzLzY5OTM2MDM3OC5zaHRtbA&#061;&#061;.html http:\/\/my.tv.sohu.com\/us\/442338213\/699360293.shtml https:\/\/tv.sohu.com\/v\/dXMvNDQyMzM4MjEzLzY5OTM2MDI5My5zaHRtbA&#061;&#061;.html http:\/\/my.tv.sohu.com\/us\/442338213\/699360295.shtml https:\/\/tv.sohu.com\/v\/dXMvNDQyMzM4MjEzLzY5OTM2MDI5NS5zaHRtbA&#061;&#061;.html http:\/\/my.tv.sohu.com\/us\/442338213\/699360410.shtml https:\/\/tv.sohu.com\/v\/dXMvNDQyMzM4MjEzLzY5OTM2MDQxMC5zaHRtbA&#061;&#061;.html http:\/\/my.tv.sohu.com\/us\/442338213\/699360611.shtml https:\/\/tv.sohu.com\/v\/dXMvNDQyMzM4MjEzLzY5OTM2MDYxMS5zaHRtbA&#061;&#061;.html http:\/\/my.tv.sohu.com\/us\/442338213\/699360421.shtml https:\/\/tv.sohu.com\/v\/dXMvNDQyMzM4MjEzLzY5OTM2MDQyMS5zaHRtbA&#061;&#061;.html \u00a0<\/p>\n<ul>\n<li>proxy: \u4ee3\u7406\u5bf9\u8c61\u672c\u8eab&#xff08;\u901a\u5e38\u7528\u4e0d\u5230&#xff09;\u3002<\/li>\n<li>method: \u88ab\u8c03\u7528\u7684\u76ee\u6807\u65b9\u6cd5\u3002<\/li>\n<li>args: \u65b9\u6cd5\u53c2\u6570\u3002<\/li>\n<li>target: \u76ee\u6807\u5bf9\u8c61&#xff08;\u9700\u8981\u5728\u81ea\u5b9a\u4e49\u7684 InvocationHandler \u5b9e\u73b0\u4e2d\u6301\u6709\u5176\u5f15\u7528&#xff09;\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p>\u7279\u70b9&#xff1a;<\/p>\n<ul>\n<li>\u4f18\u70b9&#xff1a;\u57fa\u4e8e Java \u6807\u51c6\u5e93&#xff0c;\u65e0\u9700\u989d\u5916\u4f9d\u8d56\u3002<\/li>\n<li>\u4f18\u70b9&#xff1a;\u751f\u6210\u7684\u4ee3\u7406\u5bf9\u8c61\u662f\u63a5\u53e3\u7c7b\u578b&#xff0c;\u7b26\u5408\u9762\u5411\u63a5\u53e3\u7f16\u7a0b\u7684\u539f\u5219\u3002<\/li>\n<li>\u7f3a\u70b9&#xff1a;\u53ea\u80fd\u4ee3\u7406\u5b9e\u73b0\u4e86\u63a5\u53e3\u7684\u7c7b\u3002<\/li>\n<li>\u7f3a\u70b9&#xff1a;\u901a\u8fc7\u53cd\u5c04\u8c03\u7528\u65b9\u6cd5&#xff0c;\u6027\u80fd\u7565\u4f4e\u4e8e CGLib&#xff08;\u4f46\u901a\u5e38\u53ef\u63a5\u53d7&#xff09;\u3002<\/li>\n<\/ul>\n<p>\u5b9e\u6218\u793a\u4f8b&#xff1a;<\/p>\n<p>\/\/ 1. \u5b9a\u4e49\u4e1a\u52a1\u63a5\u53e3<br \/>\npublic interface UserService {<br \/>\n    void saveUser(User user);<br \/>\n    User getUserById(int id);<br \/>\n}<\/p>\n<p>\/\/ 2. \u5b9e\u73b0\u4e1a\u52a1\u63a5\u53e3 (\u76ee\u6807\u5bf9\u8c61)<br \/>\npublic class UserServiceImpl implements UserService {<br \/>\n    &#064;Override<br \/>\n    public void saveUser(User user) { \/* \u4e1a\u52a1\u903b\u8f91 *\/ }<br \/>\n    &#064;Override<br \/>\n    public User getUserById(int id) { \/* \u4e1a\u52a1\u903b\u8f91 *\/ return new User(); }<br \/>\n}<\/p>\n<p>\/\/ 3. \u5b9e\u73b0 InvocationHandler (\u5b9a\u4e49\u589e\u5f3a\u903b\u8f91)<br \/>\npublic class LoggingHandler implements InvocationHandler {<br \/>\n    private final Object target; \/\/ \u76ee\u6807\u5bf9\u8c61<\/p>\n<p>    public LoggingHandler(Object target) {<br \/>\n        this.target &#061; target;<br \/>\n    }<\/p>\n<p>    &#064;Override<br \/>\n    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {<br \/>\n        System.out.println(&#034;Before method: &#034; &#043; method.getName());<br \/>\n        Object result &#061; method.invoke(target, args); \/\/ \u8c03\u7528\u76ee\u6807\u65b9\u6cd5<br \/>\n        System.out.println(&#034;After method: &#034; &#043; method.getName());<br \/>\n        return result;<br \/>\n    }<br \/>\n}<\/p>\n<p>\/\/ 4. \u521b\u5efa\u4ee3\u7406\u5bf9\u8c61\u5e76\u4f7f\u7528<br \/>\npublic class Main {<br \/>\n    public static void main(String[] args) {<br \/>\n        UserService target &#061; new UserServiceImpl(); \/\/ \u76ee\u6807\u5bf9\u8c61<br \/>\n        \/\/ \u521b\u5efa\u4ee3\u7406\u5bf9\u8c61<br \/>\n        UserService proxy &#061; (UserService) Proxy.newProxyInstance(<br \/>\n                target.getClass().getClassLoader(),<br \/>\n                target.getClass().getInterfaces(),<br \/>\n                new LoggingHandler(target)<br \/>\n        );<br \/>\n        \/\/ \u901a\u8fc7\u4ee3\u7406\u8c03\u7528\u65b9\u6cd5&#xff0c;\u589e\u5f3a\u903b\u8f91\u4f1a\u88ab\u6267\u884c<br \/>\n        proxy.saveUser(new User());<br \/>\n        proxy.getUserById(1);<br \/>\n    }<br \/>\n}<\/p>\n<hr \/>\n<p>2. CGLib \u52a8\u6001\u4ee3\u7406<\/p>\n<p>\u539f\u7406&#xff1a; CGLib (Code Generation Library) \u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u9ad8\u6027\u80fd\u4ee3\u7801\u751f\u6210\u5e93\u3002\u5b83\u901a\u8fc7\u5728\u8fd0\u884c\u65f6\u52a8\u6001\u751f\u6210\u76ee\u6807\u7c7b\u7684\u5b50\u7c7b\u6765\u5b9e\u73b0\u4ee3\u7406\u3002\u56e0\u6b64&#xff0c;\u5b83\u4e0d\u9700\u8981\u76ee\u6807\u7c7b\u5b9e\u73b0\u4efb\u4f55\u63a5\u53e3\u3002<\/p>\n<p>\u5b9e\u73b0\u673a\u5236&#xff1a;<\/p>\n<ul>\n<li>\u6838\u5fc3\u7c7b\u662f net.sf.cglib.proxy.Enhancer \u548c net.sf.cglib.proxy.MethodInterceptor\u3002<\/li>\n<li>Enhancer \u7528\u4e8e\u521b\u5efa\u4ee3\u7406\u5bf9\u8c61&#xff1a; Enhancer enhancer &#061; new Enhancer();<br \/>\nenhancer.setSuperclass(target.getClass()); \/\/ \u8bbe\u7f6e\u76ee\u6807\u7c7b\u4e3a\u7236\u7c7b<br \/>\nenhancer.setCallback(new MyMethodInterceptor()); \/\/ \u8bbe\u7f6e\u56de\u8c03(\u62e6\u622a\u5668)<br \/>\nObject proxy &#061; enhancer.create(); \/\/ \u521b\u5efa\u4ee3\u7406\u5bf9\u8c61<\/p>\n<p>http:\/\/my.tv.sohu.com\/us\/442338213\/699359606.shtml https:\/\/tv.sohu.com\/v\/dXMvNDQyMzM4MjEzLzY5OTM1OTYwNi5zaHRtbA&#061;&#061;.html http:\/\/my.tv.sohu.com\/us\/442338213\/699359615.shtml https:\/\/tv.sohu.com\/v\/dXMvNDQyMzM4MjEzLzY5OTM1OTYxNS5zaHRtbA&#061;&#061;.html http:\/\/my.tv.sohu.com\/us\/442338213\/699359712.shtml https:\/\/tv.sohu.com\/v\/dXMvNDQyMzM4MjEzLzY5OTM1OTcxMi5zaHRtbA&#061;&#061;.html http:\/\/my.tv.sohu.com\/us\/442338213\/699359631.shtml https:\/\/tv.sohu.com\/v\/dXMvNDQyMzM4MjEzLzY5OTM1OTYzMS5zaHRtbA&#061;&#061;.html http:\/\/my.tv.sohu.com\/us\/442338213\/699359809.shtml https:\/\/tv.sohu.com\/v\/dXMvNDQyMzM4MjEzLzY5OTM1OTgwOS5zaHRtbA&#061;&#061;.html http:\/\/my.tv.sohu.com\/us\/442338213\/699359725.shtml https:\/\/tv.sohu.com\/v\/dXMvNDQyMzM4MjEzLzY5OTM1OTcyNS5zaHRtbA&#061;&#061;.html http:\/\/my.tv.sohu.com\/us\/442338213\/699359820.shtml https:\/\/tv.sohu.com\/v\/dXMvNDQyMzM4MjEzLzY5OTM1OTgyMC5zaHRtbA&#061;&#061;.html http:\/\/my.tv.sohu.com\/us\/442338213\/699359824.shtml https:\/\/tv.sohu.com\/v\/dXMvNDQyMzM4MjEzLzY5OTM1OTgyNC5zaHRtbA&#061;&#061;.html http:\/\/my.tv.sohu.com\/us\/442338213\/699359921.shtml https:\/\/tv.sohu.com\/v\/dXMvNDQyMzM4MjEzLzY5OTM1OTkyMS5zaHRtbA&#061;&#061;.html http:\/\/my.tv.sohu.com\/us\/442338213\/699359923.shtml https:\/\/tv.sohu.com\/v\/dXMvNDQyMzM4MjEzLzY5OTM1OTkyMy5zaHRtbA&#061;&#061;.html http:\/\/my.tv.sohu.com\/us\/442338213\/699359832.shtml https:\/\/tv.sohu.com\/v\/dXMvNDQyMzM4MjEzLzY5OTM1OTgzMi5zaHRtbA&#061;&#061;.html http:\/\/my.tv.sohu.com\/us\/442338213\/699359663.shtml https:\/\/tv.sohu.com\/v\/dXMvNDQyMzM4MjEzLzY5OTM1OTY2My5zaHRtbA&#061;&#061;.html http:\/\/my.tv.sohu.com\/us\/442338213\/699359845.shtml https:\/\/tv.sohu.com\/v\/dXMvNDQyMzM4MjEzLzY5OTM1OTg0NS5zaHRtbA&#061;&#061;.html http:\/\/my.tv.sohu.com\/us\/442338213\/699359752.shtml https:\/\/tv.sohu.com\/v\/dXMvNDQyMzM4MjEzLzY5OTM1OTc1Mi5zaHRtbA&#061;&#061;.html http:\/\/my.tv.sohu.com\/us\/442338213\/699359849.shtml https:\/\/tv.sohu.com\/v\/dXMvNDQyMzM4MjEzLzY5OTM1OTg0OS5zaHRtbA&#061;&#061;.html http:\/\/my.tv.sohu.com\/us\/442338213\/699359939.shtml https:\/\/tv.sohu.com\/v\/dXMvNDQyMzM4MjEzLzY5OTM1OTkzOS5zaHRtbA&#061;&#061;.html http:\/\/my.tv.sohu.com\/us\/442338213\/699359676.shtml https:\/\/tv.sohu.com\/v\/dXMvNDQyMzM4MjEzLzY5OTM1OTY3Ni5zaHRtbA&#061;&#061;.html http:\/\/my.tv.sohu.com\/us\/442338213\/699359859.shtml https:\/\/tv.sohu.com\/v\/dXMvNDQyMzM4MjEzLzY5OTM1OTg1OS5zaHRtbA&#061;&#061;.html http:\/\/my.tv.sohu.com\/us\/442338213\/699359683.shtml https:\/\/tv.sohu.com\/v\/dXMvNDQyMzM4MjEzLzY5OTM1OTY4My5zaHRtbA&#061;&#061;.html http:\/\/my.tv.sohu.com\/us\/442338213\/699359775.shtml https:\/\/tv.sohu.com\/v\/dXMvNDQyMzM4MjEzLzY5OTM1OTc3NS5zaHRtbA&#061;&#061;.html http:\/\/my.tv.sohu.com\/us\/442338213\/699359973.shtml https:\/\/tv.sohu.com\/v\/dXMvNDQyMzM4MjEzLzY5OTM1OTk3My5zaHRtbA&#061;&#061;.html http:\/\/my.tv.sohu.com\/us\/442338213\/699359790.shtml https:\/\/tv.sohu.com\/v\/dXMvNDQyMzM4MjEzLzY5OTM1OTc5MC5zaHRtbA&#061;&#061;.html http:\/\/my.tv.sohu.com\/us\/442338213\/699360205.shtml https:\/\/tv.sohu.com\/v\/dXMvNDQyMzM4MjEzLzY5OTM2MDIwNS5zaHRtbA&#061;&#061;.html http:\/\/my.tv.sohu.com\/us\/442338213\/699360103.shtml https:\/\/tv.sohu.com\/v\/dXMvNDQyMzM4MjEzLzY5OTM2MDEwMy5zaHRtbA&#061;&#061;.html http:\/\/my.tv.sohu.com\/us\/442338213\/699360026.shtml https:\/\/tv.sohu.com\/v\/dXMvNDQyMzM4MjEzLzY5OTM2MDAyNi5zaHRtbA&#061;&#061;.html http:\/\/my.tv.sohu.com\/us\/442338213\/699360000.shtml https:\/\/tv.sohu.com\/v\/dXMvNDQyMzM4MjEzLzY5OTM2MDAwMC5zaHRtbA&#061;&#061;.html http:\/\/my.tv.sohu.com\/us\/442338213\/699360035.shtml https:\/\/tv.sohu.com\/v\/dXMvNDQyMzM4MjEzLzY5OTM2MDAzNS5zaHRtbA&#061;&#061;.html http:\/\/my.tv.sohu.com\/us\/442338213\/699360125.shtml https:\/\/tv.sohu.com\/v\/dXMvNDQyMzM4MjEzLzY5OTM2MDEyNS5zaHRtbA&#061;&#061;.html http:\/\/my.tv.sohu.com\/us\/442338213\/699360042.shtml https:\/\/tv.sohu.com\/v\/dXMvNDQyMzM4MjEzLzY5OTM2MDA0Mi5zaHRtbA&#061;&#061;.html http:\/\/my.tv.sohu.com\/us\/442338213\/699360136.shtml https:\/\/tv.sohu.com\/v\/dXMvNDQyMzM4MjEzLzY5OTM2MDEzNi5zaHRtbA&#061;&#061;.html http:\/\/my.tv.sohu.com\/us\/442338213\/699360142.shtml https:\/\/tv.sohu.com\/v\/dXMvNDQyMzM4MjEzLzY5OTM2MDE0Mi5zaHRtbA&#061;&#061;.html http:\/\/my.tv.sohu.com\/us\/442338213\/699360056.shtml https:\/\/tv.sohu.com\/v\/dXMvNDQyMzM4MjEzLzY5OTM2MDA1Ni5zaHRtbA&#061;&#061;.html http:\/\/my.tv.sohu.com\/us\/442338213\/699360060.shtml https:\/\/tv.sohu.com\/v\/dXMvNDQyMzM4MjEzLzY5OTM2MDA2MC5zaHRtbA&#061;&#061;.html http:\/\/my.tv.sohu.com\/us\/442338213\/699360065.shtml https:\/\/tv.sohu.com\/v\/dXMvNDQyMzM4MjEzLzY5OTM2MDA2NS5zaHRtbA&#061;&#061;.html http:\/\/my.tv.sohu.com\/us\/442338213\/699360162.shtml https:\/\/tv.sohu.com\/v\/dXMvNDQyMzM4MjEzLzY5OTM2MDE2Mi5zaHRtbA&#061;&#061;.html http:\/\/my.tv.sohu.com\/us\/442338213\/699360353.shtml https:\/\/tv.sohu.com\/v\/dXMvNDQyMzM4MjEzLzY5OTM2MDM1My5zaHRtbA&#061;&#061;.html http:\/\/my.tv.sohu.com\/us\/442338213\/699360272.shtml https:\/\/tv.sohu.com\/v\/dXMvNDQyMzM4MjEzLzY5OTM2MDI3Mi5zaHRtbA&#061;&#061;.html http:\/\/my.tv.sohu.com\/us\/442338213\/699360179.shtml https:\/\/tv.sohu.com\/v\/dXMvNDQyMzM4MjEzLzY5OTM2MDE3OS5zaHRtbA&#061;&#061;.html http:\/\/my.tv.sohu.com\/us\/442338213\/699360275.shtml https:\/\/tv.sohu.com\/v\/dXMvNDQyMzM4MjEzLzY5OTM2MDI3NS5zaHRtbA&#061;&#061;.html http:\/\/my.tv.sohu.com\/us\/442338213\/699360279.shtml https:\/\/tv.sohu.com\/v\/dXMvNDQyMzM4MjEzLzY5OTM2MDI3OS5zaHRtbA&#061;&#061;.html http:\/\/my.tv.sohu.com\/us\/442338213\/699360185.shtml https:\/\/tv.sohu.com\/v\/dXMvNDQyMzM4MjEzLzY5OTM2MDE4NS5zaHRtbA&#061;&#061;.html http:\/\/my.tv.sohu.com\/us\/442338213\/699360187.shtml https:\/\/tv.sohu.com\/v\/dXMvNDQyMzM4MjEzLzY5OTM2MDE4Ny5zaHRtbA&#061;&#061;.html http:\/\/my.tv.sohu.com\/us\/442338213\/699360190.shtml https:\/\/tv.sohu.com\/v\/dXMvNDQyMzM4MjEzLzY5OTM2MDE5MC5zaHRtbA&#061;&#061;.html http:\/\/my.tv.sohu.com\/us\/442338213\/699360375.shtml https:\/\/tv.sohu.com\/v\/dXMvNDQyMzM4MjEzLzY5OTM2MDM3NS5zaHRtbA&#061;&#061;.html http:\/\/my.tv.sohu.com\/us\/442338213\/699360378.shtml https:\/\/tv.sohu.com\/v\/dXMvNDQyMzM4MjEzLzY5OTM2MDM3OC5zaHRtbA&#061;&#061;.html http:\/\/my.tv.sohu.com\/us\/442338213\/699360293.shtml https:\/\/tv.sohu.com\/v\/dXMvNDQyMzM4MjEzLzY5OTM2MDI5My5zaHRtbA&#061;&#061;.html http:\/\/my.tv.sohu.com\/us\/442338213\/699360295.shtml https:\/\/tv.sohu.com\/v\/dXMvNDQyMzM4MjEzLzY5OTM2MDI5NS5zaHRtbA&#061;&#061;.html http:\/\/my.tv.sohu.com\/us\/442338213\/699360410.shtml https:\/\/tv.sohu.com\/v\/dXMvNDQyMzM4MjEzLzY5OTM2MDQxMC5zaHRtbA&#061;&#061;.html http:\/\/my.tv.sohu.com\/us\/442338213\/699360611.shtml https:\/\/tv.sohu.com\/v\/dXMvNDQyMzM4MjEzLzY5OTM2MDYxMS5zaHRtbA&#061;&#061;.html http:\/\/my.tv.sohu.com\/us\/442338213\/699360421.shtml https:\/\/tv.sohu.com\/v\/dXMvNDQyMzM4MjEzLzY5OTM2MDQyMS5zaHRtbA&#061;&#061;.html \u00a0<\/p>\n<\/li>\n<li>MethodInterceptor \u63a5\u53e3\u5b9a\u4e49\u4e86\u62e6\u622a\u65b9\u6cd5&#xff1a; public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {<br \/>\n    \/\/ \u524d\u7f6e\u589e\u5f3a\u903b\u8f91 (Before Advice)<br \/>\n    Object result &#061; proxy.invokeSuper(obj, args); \/\/ \u8c03\u7528\u7236\u7c7b(\u76ee\u6807\u7c7b)\u7684\u65b9\u6cd5<br \/>\n    \/\/ \u540e\u7f6e\u589e\u5f3a\u903b\u8f91 (After Advice)<br \/>\n    return result;<br \/>\n}<\/p>\n<ul>\n<li>obj: \u4ee3\u7406\u5bf9\u8c61\u672c\u8eab&#xff08;\u901a\u5e38\u7528\u4e0d\u5230&#xff09;\u3002<\/li>\n<li>method: \u88ab\u8c03\u7528\u7684\u76ee\u6807\u65b9\u6cd5\u3002<\/li>\n<li>args: \u65b9\u6cd5\u53c2\u6570\u3002<\/li>\n<li>proxy: \u7528\u4e8e\u5feb\u901f\u8c03\u7528\u7236\u7c7b&#xff08;\u76ee\u6807\u7c7b&#xff09;\u65b9\u6cd5\u7684\u5de5\u5177\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p>\u7279\u70b9&#xff1a;<\/p>\n<ul>\n<li>\u4f18\u70b9&#xff1a;\u53ef\u4ee5\u4ee3\u7406\u6ca1\u6709\u5b9e\u73b0\u63a5\u53e3\u7684\u666e\u901a\u7c7b\u3002<\/li>\n<li>\u4f18\u70b9&#xff1a;\u901a\u5e38\u8ba4\u4e3a\u5176\u751f\u6210\u7684\u4ee3\u7406\u5bf9\u8c61\u5728\u65b9\u6cd5\u8c03\u7528\u4e0a\u6bd4 JDK \u4ee3\u7406&#xff08;\u57fa\u4e8e\u53cd\u5c04&#xff09;\u6027\u80fd\u66f4\u9ad8&#xff0c;\u56e0\u4e3a\u5b83\u4f7f\u7528\u4e86 FastClass \u673a\u5236\u76f4\u63a5\u8c03\u7528\u65b9\u6cd5\u3002<\/li>\n<li>\u7f3a\u70b9&#xff1a;\u9700\u8981\u989d\u5916\u5f15\u5165 CGLib \u5e93 (Spring Core \u5df2\u5305\u542b)\u3002<\/li>\n<li>\u7f3a\u70b9&#xff1a;\u65e0\u6cd5\u4ee3\u7406 final \u65b9\u6cd5&#xff08;\u56e0\u4e3a\u5b50\u7c7b\u65e0\u6cd5\u8986\u76d6&#xff09;&#xff0c;\u4e5f\u65e0\u6cd5\u4ee3\u7406 final \u7c7b&#xff08;\u56e0\u4e3a\u65e0\u6cd5\u7ee7\u627f&#xff09;\u3002<\/li>\n<li>\u7f3a\u70b9&#xff1a;\u4ee3\u7406\u5bf9\u8c61\u7c7b\u578b\u662f\u76ee\u6807\u7c7b\u7684\u5b50\u7c7b\u7c7b\u578b\u3002<\/li>\n<\/ul>\n<p>\u5b9e\u6218\u793a\u4f8b&#xff1a;<\/p>\n<p>\/\/ 0. \u5f15\u5165 CGLib \u4f9d\u8d56 (\u901a\u5e38\u7531 Spring \u63d0\u4f9b)<br \/>\n\/\/ 1. \u76ee\u6807\u7c7b (\u65e0\u9700\u5b9e\u73b0\u63a5\u53e3)<br \/>\npublic class OrderService {<br \/>\n    public void createOrder(Order order) { \/* \u4e1a\u52a1\u903b\u8f91 *\/ }<br \/>\n    public Order getOrder(String id) { \/* \u4e1a\u52a1\u903b\u8f91 *\/ return new Order(); }<br \/>\n}<\/p>\n<p>\/\/ 2. \u5b9e\u73b0 MethodInterceptor (\u5b9a\u4e49\u589e\u5f3a\u903b\u8f91)<br \/>\npublic class TransactionInterceptor implements MethodInterceptor {<br \/>\n    &#064;Override<br \/>\n    public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {<br \/>\n        System.out.println(&#034;Start transaction for: &#034; &#043; method.getName());<br \/>\n        Object result &#061; proxy.invokeSuper(obj, args); \/\/ \u8c03\u7528\u7236\u7c7b(\u76ee\u6807\u7c7b)\u65b9\u6cd5<br \/>\n        System.out.println(&#034;Commit transaction for: &#034; &#043; method.getName());<br \/>\n        return result;<br \/>\n    }<br \/>\n}<\/p>\n<p>\/\/ 3. \u521b\u5efa\u4ee3\u7406\u5bf9\u8c61\u5e76\u4f7f\u7528<br \/>\npublic class Main {<br \/>\n    public static void main(String[] args) {<br \/>\n        \/\/ \u521b\u5efa\u589e\u5f3a\u5668<br \/>\n        Enhancer enhancer &#061; new Enhancer();<br \/>\n        enhancer.setSuperclass(OrderService.class); \/\/ \u8bbe\u7f6e\u76ee\u6807\u7c7b\u4e3a\u7236\u7c7b<br \/>\n        enhancer.setCallback(new TransactionInterceptor()); \/\/ \u8bbe\u7f6e\u62e6\u622a\u5668<br \/>\n        \/\/ \u521b\u5efa\u4ee3\u7406\u5bf9\u8c61<br \/>\n        OrderService proxy &#061; (OrderService) enhancer.create();<br \/>\n        \/\/ \u901a\u8fc7\u4ee3\u7406\u8c03\u7528\u65b9\u6cd5&#xff0c;\u589e\u5f3a\u903b\u8f91\u4f1a\u88ab\u6267\u884c<br \/>\n        proxy.createOrder(new Order());<br \/>\n        proxy.getOrder(&#034;123&#034;);<br \/>\n    }<br \/>\n}<\/p>\n<hr \/>\n<p>3. JDK \u4ee3\u7406 vs CGLib \u4ee3\u7406 \u5bf9\u6bd4\u603b\u7ed3<\/p>\n<table>\n<tr>\u7279\u6027JDK \u52a8\u6001\u4ee3\u7406CGLib \u52a8\u6001\u4ee3\u7406<\/tr>\n<tbody>\n<tr>\n<td style=\"text-align:left\">\u4f9d\u8d56<\/td>\n<td style=\"text-align:left\">Java \u6807\u51c6\u5e93 (java.lang.reflect)<\/td>\n<td style=\"text-align:left\">\u9700\u8981 CGLib \u5e93<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align:left\">\u4ee3\u7406\u5bf9\u8c61\u7c7b\u578b<\/td>\n<td style=\"text-align:left\">\u5b9e\u73b0\u76ee\u6807\u63a5\u53e3<\/td>\n<td style=\"text-align:left\">\u76ee\u6807\u7c7b\u7684\u5b50\u7c7b<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align:left\">\u76ee\u6807\u8981\u6c42<\/td>\n<td style=\"text-align:left\">\u5fc5\u987b\u5b9e\u73b0\u81f3\u5c11\u4e00\u4e2a\u63a5\u53e3<\/td>\n<td style=\"text-align:left\">\u53ef\u4ee5\u662f\u666e\u901a\u7c7b (\u975e final)<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align:left\">final \u65b9\u6cd5<\/td>\n<td style=\"text-align:left\">\u53ef\u4ee5\u4ee3\u7406<\/td>\n<td style=\"text-align:left\">\u65e0\u6cd5\u4ee3\u7406 (\u5b50\u7c7b\u4e0d\u80fd\u8986\u76d6)<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align:left\">final \u7c7b<\/td>\n<td style=\"text-align:left\">\u5982\u679c\u5b9e\u73b0\u4e86\u63a5\u53e3\u5219\u53ef\u4ee5\u4ee3\u7406<\/td>\n<td style=\"text-align:left\">\u65e0\u6cd5\u4ee3\u7406 (\u4e0d\u80fd\u7ee7\u627f)<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align:left\">\u6027\u80fd (\u521b\u5efa)<\/td>\n<td style=\"text-align:left\">\u76f8\u5bf9\u8f83\u5feb<\/td>\n<td style=\"text-align:left\">\u76f8\u5bf9\u8f83\u6162 (\u9700\u8981\u751f\u6210\u5b57\u8282\u7801)<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align:left\">\u6027\u80fd (\u8c03\u7528)<\/td>\n<td style=\"text-align:left\">\u8f83\u6162 (\u53cd\u5c04\u8c03\u7528)<\/td>\n<td style=\"text-align:left\">\u8f83\u5feb (FastClass \u673a\u5236)<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align:left\">Spring \u9ed8\u8ba4\u9009\u62e9<\/td>\n<td style=\"text-align:left\">\u76ee\u6807\u6709\u63a5\u53e3\u65f6\u4f18\u5148\u4f7f\u7528<\/td>\n<td style=\"text-align:left\">\u76ee\u6807\u65e0\u63a5\u53e3\u65f6\u4f7f\u7528<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<hr \/>\n<p>4. Spring AOP \u5982\u4f55\u9009\u62e9&#xff1f;<\/p>\n<p>Spring AOP \u5185\u90e8\u81ea\u52a8\u6839\u636e\u76ee\u6807\u5bf9\u8c61\u7684\u60c5\u51b5\u9009\u62e9\u5408\u9002\u7684\u4ee3\u7406\u65b9\u5f0f&#xff1a;<\/p>\n<li>\u5982\u679c\u76ee\u6807\u5bf9\u8c61\u5b9e\u73b0\u4e86\u63a5\u53e3(\u4e00\u4e2a\u6216\u591a\u4e2a)&#xff1a;\u9ed8\u8ba4\u4f7f\u7528 JDK \u52a8\u6001\u4ee3\u7406\u3002<\/li>\n<li>\u5982\u679c\u76ee\u6807\u5bf9\u8c61\u6ca1\u6709\u5b9e\u73b0\u4efb\u4f55\u63a5\u53e3&#xff1a;\u5219\u5fc5\u987b\u4f7f\u7528 CGLib \u52a8\u6001\u4ee3\u7406\u3002<\/li>\n<li>\u5f3a\u5236\u4f7f\u7528 CGLib&#xff1a;\u53ef\u4ee5\u901a\u8fc7\u914d\u7f6e (\u5982 &#064;EnableAspectJAutoProxy(proxyTargetClass &#061; true)) \u5f3a\u5236 Spring AOP \u5bf9\u6240\u6709\u76ee\u6807\u90fd\u4f7f\u7528 CGLib \u4ee3\u7406&#xff0c;\u5373\u4f7f\u76ee\u6807\u5b9e\u73b0\u4e86\u63a5\u53e3\u3002<\/li>\n<p>\u6838\u5fc3\u4ef7\u503c&#xff1a;<\/p>\n<p>\u65e0\u8bba\u5e95\u5c42\u4f7f\u7528\u54ea\u79cd\u4ee3\u7406\u6280\u672f&#xff0c;Spring AOP \u90fd\u5411\u5f00\u53d1\u8005\u63d0\u4f9b\u4e86\u4e00\u4e2a\u7edf\u4e00\u7684\u3001\u58f0\u660e\u5f0f\u7684 AOP \u7f16\u7a0b\u6a21\u578b&#xff08;\u4e3b\u8981\u901a\u8fc7 &#064;Aspect, &#064;Before, &#064;After, &#064;Around \u7b49\u6ce8\u89e3&#xff09;\u3002\u5f00\u53d1\u8005\u53ea\u9700\u5173\u6ce8\u5207\u9762\u7684\u5b9a\u4e49\u548c\u589e\u5f3a\u903b\u8f91\u7684\u7f16\u5199&#xff0c;\u65e0\u9700\u76f4\u63a5\u5904\u7406\u590d\u6742\u7684\u4ee3\u7406\u521b\u5efa\u8fc7\u7a0b&#xff0c;\u5927\u5927\u7b80\u5316\u4e86 AOP \u7684\u5e94\u7528\u3002<\/p>\n<p>\u7406\u89e3 JDK \u548c CGLib \u52a8\u6001\u4ee3\u7406\u7684\u539f\u7406&#xff0c;\u6709\u52a9\u4e8e\u6df1\u5165\u638c\u63e1 Spring AOP \u7684\u5de5\u4f5c\u673a\u5236&#xff0c;\u5e76\u5728\u9700\u8981\u65f6\u8fdb\u884c\u66f4\u5e95\u5c42\u7684\u8c03\u8bd5\u6216\u5b9a\u5236\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u597d\u7684&#xff0c;\u6211\u4eec\u6765\u6df1\u5165\u89e3\u6790 Spring AOP \u7684\u6838\u5fc3\u539f\u7406\u4e4b\u4e00&#xff1a;\u52a8\u6001\u4ee3\u7406&#xff0c;\u5e76\u91cd\u70b9\u5bf9\u6bd4 JDK \u52a8\u6001\u4ee3\u7406 \u548c CGLib \u52a8\u6001\u4ee3\u7406 \u7684\u5b9e\u73b0\u65b9\u5f0f\u3001\u533a\u522b\u4ee5\u53ca\u5b9e\u6218\u5e94\u7528\u3002<br \/>\n\u6838\u5fc3\u6982\u5ff5&#xff1a;\u52a8\u6001\u4ee3\u7406\u4e0e AOP<br \/>\nSpring AOP (Aspect-Oriented Programming) \u7684\u6838\u5fc3\u76ee\u6807\u662f\u5c06\u6a2a\u5207\u5173\u6ce8\u70b9&#xff08;\u5982\u65e5\u5fd7\u8bb0\u5f55\u3001\u4e8b\u52a1\u7ba1\u7406\u3001\u5b89\u5168\u68c0\u67e5\u7b49&#xff09;\u4ece\u6838\u5fc3\u4e1a\u52a1\u903b\u8f91\u4e2d\u5206\u79bb\u51fa\u6765\u3002\u4e3a\u4e86\u5b9e\u73b0\u8fd9\u79cd\u975e\u4fb5\u5165\u5f0f\u7684\u589e\u5f3a&amp;#xff<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[99,190],"topic":[],"class_list":["post-73667","post","type-post","status-publish","format-standard","hentry","category-server","tag-java","tag-190"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>SpringAOP\u52a8\u6001\u4ee3\u7406\uff1aJDK\u4e0eCGLib\u6df1\u5ea6\u89e3\u6790 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wsisp.com\/helps\/73667.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"SpringAOP\u52a8\u6001\u4ee3\u7406\uff1aJDK\u4e0eCGLib\u6df1\u5ea6\u89e3\u6790 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u597d\u7684&#xff0c;\u6211\u4eec\u6765\u6df1\u5165\u89e3\u6790 Spring AOP \u7684\u6838\u5fc3\u539f\u7406\u4e4b\u4e00&#xff1a;\u52a8\u6001\u4ee3\u7406&#xff0c;\u5e76\u91cd\u70b9\u5bf9\u6bd4 JDK \u52a8\u6001\u4ee3\u7406 \u548c CGLib \u52a8\u6001\u4ee3\u7406 \u7684\u5b9e\u73b0\u65b9\u5f0f\u3001\u533a\u522b\u4ee5\u53ca\u5b9e\u6218\u5e94\u7528\u3002 \u6838\u5fc3\u6982\u5ff5&#xff1a;\u52a8\u6001\u4ee3\u7406\u4e0e AOP Spring AOP (Aspect-Oriented Programming) \u7684\u6838\u5fc3\u76ee\u6807\u662f\u5c06\u6a2a\u5207\u5173\u6ce8\u70b9&#xff08;\u5982\u65e5\u5fd7\u8bb0\u5f55\u3001\u4e8b\u52a1\u7ba1\u7406\u3001\u5b89\u5168\u68c0\u67e5\u7b49&#xff09;\u4ece\u6838\u5fc3\u4e1a\u52a1\u903b\u8f91\u4e2d\u5206\u79bb\u51fa\u6765\u3002\u4e3a\u4e86\u5b9e\u73b0\u8fd9\u79cd\u975e\u4fb5\u5165\u5f0f\u7684\u589e\u5f3a&amp;#xff\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/73667.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2026-02-07T22:41:39+00:00\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"12 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/73667.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/73667.html\",\"name\":\"SpringAOP\u52a8\u6001\u4ee3\u7406\uff1aJDK\u4e0eCGLib\u6df1\u5ea6\u89e3\u6790 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2026-02-07T22:41:39+00:00\",\"dateModified\":\"2026-02-07T22:41:39+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/73667.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/73667.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/73667.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"SpringAOP\u52a8\u6001\u4ee3\u7406\uff1aJDK\u4e0eCGLib\u6df1\u5ea6\u89e3\u6790\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\",\"url\":\"https:\/\/www.wsisp.com\/helps\/\",\"name\":\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"description\":\"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"contentUrl\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"caption\":\"admin\"},\"sameAs\":[\"http:\/\/wp.wsisp.com\"],\"url\":\"https:\/\/www.wsisp.com\/helps\/author\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"SpringAOP\u52a8\u6001\u4ee3\u7406\uff1aJDK\u4e0eCGLib\u6df1\u5ea6\u89e3\u6790 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wsisp.com\/helps\/73667.html","og_locale":"zh_CN","og_type":"article","og_title":"SpringAOP\u52a8\u6001\u4ee3\u7406\uff1aJDK\u4e0eCGLib\u6df1\u5ea6\u89e3\u6790 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u597d\u7684&#xff0c;\u6211\u4eec\u6765\u6df1\u5165\u89e3\u6790 Spring AOP \u7684\u6838\u5fc3\u539f\u7406\u4e4b\u4e00&#xff1a;\u52a8\u6001\u4ee3\u7406&#xff0c;\u5e76\u91cd\u70b9\u5bf9\u6bd4 JDK \u52a8\u6001\u4ee3\u7406 \u548c CGLib \u52a8\u6001\u4ee3\u7406 \u7684\u5b9e\u73b0\u65b9\u5f0f\u3001\u533a\u522b\u4ee5\u53ca\u5b9e\u6218\u5e94\u7528\u3002 \u6838\u5fc3\u6982\u5ff5&#xff1a;\u52a8\u6001\u4ee3\u7406\u4e0e AOP Spring AOP (Aspect-Oriented Programming) \u7684\u6838\u5fc3\u76ee\u6807\u662f\u5c06\u6a2a\u5207\u5173\u6ce8\u70b9&#xff08;\u5982\u65e5\u5fd7\u8bb0\u5f55\u3001\u4e8b\u52a1\u7ba1\u7406\u3001\u5b89\u5168\u68c0\u67e5\u7b49&#xff09;\u4ece\u6838\u5fc3\u4e1a\u52a1\u903b\u8f91\u4e2d\u5206\u79bb\u51fa\u6765\u3002\u4e3a\u4e86\u5b9e\u73b0\u8fd9\u79cd\u975e\u4fb5\u5165\u5f0f\u7684\u589e\u5f3a&amp;#xff","og_url":"https:\/\/www.wsisp.com\/helps\/73667.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2026-02-07T22:41:39+00:00","author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"12 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/73667.html","url":"https:\/\/www.wsisp.com\/helps\/73667.html","name":"SpringAOP\u52a8\u6001\u4ee3\u7406\uff1aJDK\u4e0eCGLib\u6df1\u5ea6\u89e3\u6790 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2026-02-07T22:41:39+00:00","dateModified":"2026-02-07T22:41:39+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/73667.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/73667.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/73667.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"SpringAOP\u52a8\u6001\u4ee3\u7406\uff1aJDK\u4e0eCGLib\u6df1\u5ea6\u89e3\u6790"}]},{"@type":"WebSite","@id":"https:\/\/www.wsisp.com\/helps\/#website","url":"https:\/\/www.wsisp.com\/helps\/","name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","description":"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41","name":"admin","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/","url":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","contentUrl":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","caption":"admin"},"sameAs":["http:\/\/wp.wsisp.com"],"url":"https:\/\/www.wsisp.com\/helps\/author\/admin"}]}},"_links":{"self":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/73667","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/comments?post=73667"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/73667\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=73667"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=73667"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=73667"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=73667"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}