{"id":113358,"date":"2026-10-06T06:16:51","date_gmt":"2026-10-05T22:16:51","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/113358.html"},"modified":"2026-10-06T06:16:51","modified_gmt":"2026-10-05T22:16:51","slug":"%e4%b8%80%e6%96%87%e7%9c%8b%e6%87%82spring%e4%b9%8baop%e4%b8%8eioc%e4%bb%a3%e7%a0%81%e5%9c%ba%e6%99%af%ef%bc%8c%e9%80%9a%e4%bf%97%e6%98%93%e6%87%82","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/113358.html","title":{"rendered":"\u4e00\u6587\u770b\u61c2Spring\u4e4bAOP\u4e0eIOC(\u4ee3\u7801+\u573a\u666f\uff0c\u901a\u4fd7\u6613\u61c2)"},"content":{"rendered":"<h2>Spring AOP \u8be6\u7ec6\u8bb2\u89e3<\/h2>\n<h3>\u4e00\u3001AOP \u662f\u4ec0\u4e48<\/h3>\n<p>AOP&#xff1a;Aspect Oriented Programming&#xff0c;\u9762\u5411\u5207\u9762\u7f16\u7a0b\u3002<\/p>\n<p>AOP \u7684\u6838\u5fc3\u539f\u7406\u5c31\u662f\u52a8\u6001\u751f\u6210\u4ee3\u7406\u5bf9\u8c61&#xff0c;\u5728\u4e0d\u6539\u52a8\u539f\u4e1a\u52a1\u4ee3\u7801\u7684\u60c5\u51b5\u4e0b&#xff0c;\u901a\u8fc7\u4ee3\u7406\u53bb\u62e6\u622a\u76ee\u6807\u65b9\u6cd5&#xff0c;\u5728\u65b9\u6cd5\u524d\u540e\u63d2\u5165\u5207\u9762\u903b\u8f91\u3002<\/p>\n<\/p>\n<p>\u6838\u5fc3\u601d\u60f3&#xff1a;\u4e1a\u52a1\u4ee3\u7801\u53ea\u5173\u5fc3\u4e1a\u52a1&#xff0c;\u516c\u5171\u903b\u8f91\u4ea4\u7ed9\u5207\u9762\u3002<\/p>\n<p>\u5178\u578b\u901a\u7528\u573a\u666f&#xff1a;<\/p>\n<p>\u65e5\u5fd7\u8bb0\u5f55\u3001\u6027\u80fd\u7edf\u8ba1\u3001\u6743\u9650\u6821\u9a8c\u3001\u4e8b\u52a1\u63a7\u5236\u3001\u5f02\u5e38\u5904\u7406\u3001\u57cb\u70b9\u3002<\/p>\n<p>\u4f8b\u5b50&#xff1a;Spring \u7684 &#064;Transactional \u5e95\u5c42\u5c31\u662f AOP\u3002<\/p>\n<h3>\u4e8c\u3001AOP \u6838\u5fc3\u672f\u8bed<\/h3>\n<li>\n<p>JoinPoint \u8fde\u63a5\u70b9 \u7a0b\u5e8f\u6267\u884c\u8fc7\u7a0b\u4e2d\u53ef\u4ee5\u88ab\u5207\u9762\u5207\u5165\u7684\u70b9\u3002<\/p>\n<p>Spring AOP \u53ea\u652f\u6301\u65b9\u6cd5\u7ea7\u522b\u7684\u8fde\u63a5\u70b9&#xff08;\u53ea\u80fd\u62e6\u622a\u65b9\u6cd5\u6267\u884c&#xff0c;\u4e0d\u80fd\u62e6\u622a\u5b57\u6bb5\u3001\u6784\u9020\u51fd\u6570&#xff09;\u3002<\/p>\n<p>\u7b80\u5355\u8bf4&#xff1a;Spring \u91cc&#xff0c;\u6240\u6709\u76ee\u6807\u5bf9\u8c61\u7684\u65b9\u6cd5\u90fd\u662f\u8fde\u63a5\u70b9\u3002<\/p>\n<\/li>\n<li>\n<p>Pointcut \u5207\u70b9 \u5339\u914d\u89c4\u5219&#xff0c;\u7528\u6765\u7b5b\u9009\u54ea\u4e9b\u8fde\u63a5\u70b9\u9700\u8981\u88ab\u5207\u9762\u589e\u5f3a\u3002<\/p>\n<p>\u901a\u8fc7\u8868\u8fbe\u5f0f\u5339\u914d\u65b9\u6cd5&#xff0c;\u53ea\u6709\u5339\u914d\u4e0a\u7684\u65b9\u6cd5\u624d\u4f1a\u6267\u884c\u901a\u77e5\u3002<\/p>\n<p>JoinPoint \u662f\u5019\u9009\u70b9\u4f4d&#xff0c;Pointcut \u662f\u6311\u9009\u89c4\u5219\u3002<\/p>\n<\/li>\n<li>Advice \u901a\u77e5 \u5207\u9762\u91cc\u8981\u6267\u884c\u7684\u589e\u5f3a\u903b\u8f91&#xff0c;\u4e5f\u5c31\u662f\u5728\u76ee\u6807\u65b9\u6cd5\u7684\u4ec0\u4e48\u65f6\u5019\u6267\u884c\u4ee3\u7801\u3002<span style=\"color:#ad720d\">Spring \u6709 5 \u79cd\u901a\u77e5\u7c7b\u578b<\/span>&#xff1a;\n<ul>\n<li><span style=\"color:#fe2c24\">&#064;Before<\/span>&#xff1a;\u524d\u7f6e\u901a\u77e5&#xff0c;\u76ee\u6807\u65b9\u6cd5\u6267\u884c\u4e4b\u524d\u6267\u884c<\/li>\n<li><span style=\"color:#fe2c24\">&#064;AfterReturning<\/span>&#xff1a;\u8fd4\u56de\u901a\u77e5&#xff0c;\u76ee\u6807\u65b9\u6cd5<span style=\"color:#1c7331\">\u6b63\u5e38\u6267\u884c\u6210\u529f\u8fd4\u56de\u540e<\/span>\u6267\u884c&#xff08;\u5f02\u5e38\u4e0d\u4f1a\u6267\u884c&#xff09;<\/li>\n<li><span style=\"color:#fe2c24\">&#064;AfterThrowing<\/span>&#xff1a;\u5f02\u5e38\u901a\u77e5&#xff0c;\u76ee\u6807\u65b9\u6cd5\u629b\u51fa\u5f02\u5e38\u65f6\u6267\u884c<\/li>\n<li><span style=\"color:#fe2c24\">&#064;After<\/span>&#xff1a;\u540e\u7f6e\u901a\u77e5&#xff08;\u6700\u7ec8\u901a\u77e5&#xff09;&#xff0c;\u76ee\u6807\u65b9\u6cd5\u6267\u884c\u5b8c\u6bd5&#xff0c;\u4e0d\u7ba1\u6b63\u5e38\u8fd8\u662f\u5f02\u5e38\u90fd\u4f1a\u6267\u884c&#xff08;\u7c7b\u4f3c finally&#xff09;<\/li>\n<li>&#064;<span style=\"color:#fe2c24\">Around<\/span>&#xff1a;\u73af\u7ed5\u901a\u77e5&#xff0c;\u5305\u88f9\u76ee\u6807\u65b9\u6cd5&#xff0c;\u53ef\u4ee5\u5728\u65b9\u6cd5\u524d\u540e\u90fd\u5199\u903b\u8f91&#xff0c;\u8fd8\u80fd\u63a7\u5236\u76ee\u6807\u65b9\u6cd5\u662f\u5426\u6267\u884c&#xff0c;\u529f\u80fd\u6700\u5f3a\u3002<\/li>\n<\/ul>\n<\/li>\n<li>Aspect \u5207\u9762 \u5207\u9762 &#061; \u5207\u70b9 (Pointcut) &#043; \u901a\u77e5 (Advice)&#xff0c;\u4e00\u4e2a\u7c7b&#xff0c;\u91cc\u9762\u5b9a\u4e49\u5207\u70b9\u8868\u8fbe\u5f0f\u548c\u901a\u77e5\u65b9\u6cd5\u3002\u7528&#064;Aspect\u6807\u6ce8\u3002<\/li>\n<li>Target \u76ee\u6807\u5bf9\u8c61 \u88ab AOP \u589e\u5f3a\u7684\u539f\u59cb\u5bf9\u8c61&#xff0c;\u6ca1\u6709\u88ab\u4ee3\u7406\u4e4b\u524d\u7684\u5bf9\u8c61\u3002<\/li>\n<li>Proxy \u4ee3\u7406\u5bf9\u8c61 Spring AOP \u5e95\u5c42\u901a\u8fc7\u52a8\u6001\u4ee3\u7406\u751f\u6210\u7684\u5bf9\u8c61&#xff0c;\u4ea4\u7ed9\u5bb9\u5668\u4f7f\u7528\u7684\u662f\u4ee3\u7406\u5bf9\u8c61&#xff0c;\u8c03\u7528\u65b9\u6cd5\u65f6\u5148\u8d70\u4ee3\u7406\u903b\u8f91\u3002<\/li>\n<li>\n<p>Weaving \u7ec7\u5165 \u628a\u5207\u9762\u903b\u8f91\u5e94\u7528\u5230\u76ee\u6807\u5bf9\u8c61&#xff0c;\u751f\u6210\u4ee3\u7406\u5bf9\u8c61\u7684\u8fc7\u7a0b\u3002 Spring AOP \u662f\u8fd0\u884c\u671f\u7ec7\u5165&#xff1a;\u7a0b\u5e8f\u8fd0\u884c\u7684\u65f6\u5019&#xff0c;\u52a8\u6001\u751f\u6210\u4ee3\u7406\u5bf9\u8c61\u3002<\/p>\n<p>AspectJ \u652f\u6301\u7f16\u8bd1\u671f\u7ec7\u5165\u3001\u7c7b\u52a0\u8f7d\u671f\u7ec7\u5165\u3002<\/p>\n<p>\u2705 &#064;Aspect\u3001&#064;Pointcut\u3001&#064;Before\u3001&#064;Around \u8fd9\u4e9b\u6ce8\u89e3\u5305\u540d\u662f org.aspectj.lang.annotation&#xff0c;\u6765\u81ea AspectJ&#xff0c;\u4f46\u662f\u6267\u884c\u5f15\u64ce\u662f Spring<\/p>\n<\/li>\n<p>\u26a0\ufe0f \u91cd\u70b9\u533a\u5206&#xff1a;Spring AOP \u2260 AspectJ<\/p>\n<p>Spring AOP \u4f7f\u7528 AspectJ \u7684\u5207\u70b9\u8bed\u6cd5\u548c\u6ce8\u89e3&#xff0c;\u4f46\u662f\u52a8\u6001\u4ee3\u7406\u5b9e\u73b0&#xff1b;<\/p>\n<p>AspectJ \u662f\u5b8c\u6574 AOP \u6846\u67b6&#xff0c;\u7f16\u8bd1\u671f\u4fee\u6539\u5b57\u8282\u7801\u3002<\/p>\n<p>\u7efc\u5408&#xff1a;<\/p>\n<p><span style=\"color:#1c7331\">\u5207\u9762 (Aspect) <\/span><span style=\"color:#0d0016\">\u5b9a\u4e49<\/span><span style=\"color:#1c7331\">\u5207\u5165\u70b9 (Pointcut) <\/span><span style=\"color:#0d0016\">\u4e0e<\/span><span style=\"color:#1c7331\">\u901a\u77e5 (Advice) <\/span><span style=\"color:#0d0016\">&#xff0c;<\/span><\/p>\n<p><span style=\"color:#1c7331\">\u7ec7\u5165 (Weaving)\u00a0<span style=\"color:#0d0016\"> <\/span>\u5207\u9762&#xff08;Aspect&#xff09;<\/span><span style=\"color:#0d0016\">\u5230<\/span><span style=\"color:#1c7331\">\u76ee\u6807 (Target)<\/span><span style=\"color:#0d0016\">\u7684<\/span><span style=\"color:#1c7331\">\u8fde\u63a5\u70b9 (JoinPoint) <\/span><span style=\"color:#0d0016\">\u5904&#xff0c;<\/span><\/p>\n<p><span style=\"color:#0d0016\">\u6700\u7ec8\u751f\u6210<\/span><span style=\"color:#1c7331\">\u4ee3\u7406 (Proxy) <\/span><span style=\"color:#0d0016\">\u5bf9\u5916\u63d0\u4f9b\u8c03\u7528\u3002<\/span><\/p>\n<\/p>\n<p>AOP \u6838\u5fc3\u7ec4\u4ef6\u5173\u7cfb\u56fe<\/p>\n<p>\u250c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500 Aspect\u3010\u5207\u9762\u3011 \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510<br \/>\n\u2502  \u251c\u2500\u2500\u2500\u2500 Pointcut\u3010\u5207\u5165\u70b9\u3011&#xff1a;\u5339\u914d\u89c4\u5219&#xff0c;\u7b5b\u9009\u54ea\u4e9bJoinPoint\u9700\u8981\u62e6\u622a       \u2502<br \/>\n\u2502  \u2514\u2500\u2500\u2500\u2500 Advice\u3010\u901a\u77e5\u3011&#xff1a;\u5339\u914d\u540e\u8981\u6267\u884c\u7684\u589e\u5f3a\u4ee3\u7801&#xff08;before\/after\u7b49&#xff09;      \u2502<br \/>\n\u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518<\/p>\n<p>                        \u2193 Weaving\u3010\u7ec7\u5165\u3011&#xff08;\u628a\u589e\u5f3a\u4ee3\u7801\u5d4c\u5165\u76ee\u6807&#xff09;<\/p>\n<p>        \u250c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500 JoinPoint\u3010\u8fde\u63a5\u70b9\u3011 \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2510<br \/>\n        \u2502  Target\u3010\u76ee\u6807\u5bf9\u8c61\u3011\u4e0a\u53ef\u88ab\u62e6\u622a\u7684\u6267\u884c\u70b9(\u65b9\u6cd5) \u2502<br \/>\n        \u2514\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2518<\/p>\n<p>                            \u2193<\/p>\n<p>            Proxy\u3010\u4ee3\u7406\u5bf9\u8c61\u3011&#xff08;\u5305\u88c5Target&#xff0c;\u5bf9\u5916\u66b4\u9732&#xff09;<\/p>\n<p>\u4e3e\u4f8b\u5b50&#xff1a;<\/p>\n<p>Aspect (\u4e8b\u52a1\u5207\u9762)<\/p>\n<p>\u251c Pointcut&#xff1a;\u5339\u914d\u5e26\u6709 &#064;Transactional \u6ce8\u89e3\u7684\u65b9\u6cd5<\/p>\n<p>\u2514 Advice&#xff1a;\u4e8b\u52a1\u7ba1\u7406\u903b\u8f91&#xff08;\u5f00\u542f\u3001\u63d0\u4ea4\u3001\u56de\u6eda&#xff09;<\/p>\n<p>Target&#xff1a;\u76ee\u6807\u7684 Service<\/p>\n<p>JoinPoint&#xff1a;\u88ab\u5339\u914d\u5230\u7684\u5e26\u6ce8\u89e3\u7684\u65b9\u6cd5<\/p>\n<hr \/>\n<h3>\u4e09\u3001Spring AOP \u5e95\u5c42\u4e24\u79cd\u52a8\u6001\u4ee3\u7406\u5b9e\u73b0<\/h3>\n<p>Spring \u4f1a\u81ea\u52a8\u9009\u62e9\u4ee3\u7406\u65b9\u5f0f&#xff0c;\u4f18\u5148\u7ea7&#xff1a;\u4f18\u5148 JDK \u52a8\u6001\u4ee3\u7406&#xff0c;\u76ee\u6807\u7c7b\u6ca1\u6709\u5b9e\u73b0\u63a5\u53e3\u65f6\u4f7f\u7528 CGLIB<\/p>\n<p>SpringBoot 2.x \u548c3.x\u9ed8\u8ba4\u5f00\u542f CGLIB \u4ee3\u7406<\/p>\n<h4>1. JDK \u52a8\u6001\u4ee3\u7406<\/h4>\n<ul>\n<li>\u8981\u6c42&#xff1a;\u76ee\u6807\u7c7b\u5fc5\u987b\u5b9e\u73b0\u81f3\u5c11\u4e00\u4e2a\u63a5\u53e3<\/li>\n<li>\u539f\u7406&#xff1a;\u8fd0\u884c\u65f6\u52a8\u6001\u751f\u6210\u4e00\u4e2a\u63a5\u53e3\u7684\u5b9e\u73b0\u7c7b&#xff0c;\u4ee3\u7406\u5b9e\u73b0\u63a5\u53e3\u65b9\u6cd5&#xff0c;\u8c03\u7528\u65f6\u62e6\u622a\u3002<\/li>\n<li>\u9650\u5236&#xff1a;\u53ea\u80fd\u4ee3\u7406\u63a5\u53e3\u5b9a\u4e49\u7684\u65b9\u6cd5&#xff1b;\u76ee\u6807\u7c7b\u81ea\u5df1\u65b0\u589e\u3001\u4e0d\u5728\u63a5\u53e3\u91cc\u7684\u65b9\u6cd5\u65e0\u6cd5\u62e6\u622a\u3002<\/li>\n<\/ul>\n<h4>2. CGLIB \u52a8\u6001\u4ee3\u7406<\/h4>\n<ul>\n<li>\u8981\u6c42&#xff1a;\u76ee\u6807\u7c7b\u4e0d\u9700\u8981\u5b9e\u73b0\u63a5\u53e3<\/li>\n<li>\u539f\u7406&#xff1a;\u8fd0\u884c\u65f6\u751f\u6210\u76ee\u6807\u7c7b\u7684\u5b50\u7c7b&#xff0c;\u91cd\u5199\u975e final \u65b9\u6cd5&#xff0c;\u901a\u8fc7\u5b50\u7c7b\u6765\u62e6\u622a\u7236\u7c7b\u65b9\u6cd5\u3002<\/li>\n<li>\u9650\u5236&#xff1a;final class\u3001final method \u65e0\u6cd5\u4ee3\u7406&#xff08;\u4e0d\u80fd\u7ee7\u627f\u3001\u4e0d\u80fd\u91cd\u5199&#xff09;\u3002<\/li>\n<\/ul>\n<p>\u5e38\u89c1\u5751&#xff1a;<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u540c\u4e00\u4e2a\u7c7b\u5185\u65b9\u6cd5\u4e92\u76f8\u8c03\u7528 this.xxx()&#xff0c;\u4e0d\u4f1a\u89e6\u53d1 AOP \u589e\u5f3a&#xff01;<\/p>\n<p>\u539f\u56e0&#xff1a;<\/p>\n<p>\u00a0\u00a0\u00a0this \u662f\u539f\u59cb\u76ee\u6807\u5bf9\u8c61&#xff0c;\u4e0d\u662f\u4ee3\u7406\u5bf9\u8c61\u3002\u8981\u81ea\u8c03\u7528\u89e6\u53d1 AOP&#xff0c;\u9700\u8981\u62ff\u5230\u4ee3\u7406\u5bf9\u8c61\u8c03\u7528\u3002<\/p>\n<h4>3. \u4e24\u4e2a\u4ee3\u7406\u5bf9\u6bd4<\/h4>\n<ul>\n<li>CGLIB \u4ee3\u7406&#xff1a;\u901a\u8fc7\u7ee7\u627f\u76ee\u6807\u7c7b&#xff08;extends&#xff09;\u751f\u6210\u4ee3\u7406&#xff0c;\u4ee3\u7406\u7c7b\u662f\u76ee\u6807\u7c7b\u7684\u5b50\u7c7b&#xff1b;\u91cd\u5199\u975e final \u65b9\u6cd5\u5b9e\u73b0\u62e6\u622a&#xff1b;\u4e0d\u8981\u6c42\u76ee\u6807\u7c7b\u5b9e\u73b0\u63a5\u53e3\u3002<\/li>\n<li>JDK \u4ee3\u7406&#xff1a;\u901a\u8fc7\u5b9e\u73b0\u63a5\u53e3\u751f\u6210\u4ee3\u7406&#xff0c;\u4ee3\u7406\u7c7b\u4e0e\u76ee\u6807\u7c7b\u5e73\u7ea7&#xff1b;\u53ea\u80fd\u62e6\u622a\u63a5\u53e3\u4e2d\u5b9a\u4e49\u7684\u65b9\u6cd5&#xff1b;\u8981\u6c42\u76ee\u6807\u7c7b\u5fc5\u987b\u5b9e\u73b0\u81f3\u5c11\u4e00\u4e2a\u63a5\u53e3\u3002<\/li>\n<\/ul>\n<p>CGLIB \u4ee3\u7406&#xff1a;\u4f8b\u5b50 &#043; \u573a\u666f<\/p>\n<\/p>\n<p>1. \u793a\u4f8b\u4ee3\u7801<\/p>\n<p>\u539f\u59cb\u76ee\u6807\u7c7b&#xff08;\u6ca1\u6709\u5b9e\u73b0\u4efb\u4f55\u63a5\u53e3&#xff09;<\/p>\n<p> \/\/ Target&#xff0c;\u6ca1\u6709\u63a5\u53e3&#xff0c;CGLIB\u53ef\u4ee5\u4ee3\u7406<br \/>\npublic class UserService {<br \/>\n    \/\/ \u666e\u901a\u65b9\u6cd5&#xff0c;\u53ef\u4ee5\u88ab\u5b50\u7c7b\u91cd\u5199&#xff0c;\u80fd\u88abAOP\u62e6\u622a<br \/>\n    public void addUser() {<br \/>\n        System.out.println(&#034;\u65b0\u589e\u7528\u6237&#xff0c;\u4e1a\u52a1\u903b\u8f91&#034;);<br \/>\n    }<\/p>\n<p>    \/\/ final\u65b9\u6cd5&#xff1a;\u4e0d\u80fd\u88ab\u5b50\u7c7b\u91cd\u5199&#xff01;CGLIB\u65e0\u6cd5\u62e6\u622a\u8fd9\u4e2a\u65b9\u6cd5<br \/>\n    public final void deleteUser() {<br \/>\n        System.out.println(&#034;\u5220\u9664\u7528\u6237&#034;);<br \/>\n    }<br \/>\n}<\/p>\n<p>CGLIB \u8fd0\u884c\u65f6\u52a8\u6001\u751f\u6210\u7684\u5b50\u7c7b&#xff08;\u4f2a\u4ee3\u7801&#xff0c;\u6846\u67b6\u81ea\u52a8\u751f\u6210&#xff0c;\u6211\u4eec\u770b\u4e0d\u5230&#xff09;<\/p>\n<p> \/\/ CGLIB\u52a8\u6001\u751f\u6210&#xff1a;\u7ee7\u627fUserService<br \/>\npublic class UserService$$CGLIB extends UserService {<\/p>\n<p>    &#064;Override<br \/>\n    public void addUser() {<br \/>\n        \/\/ \u3010Advice\u589e\u5f3a\u903b\u8f91\u3011\u524d\u7f6e\u901a\u77e5&#xff1a;\u65e5\u5fd7\/\u4e8b\u52a1<br \/>\n        System.out.println(&#034;\u65b9\u6cd5\u6267\u884c\u524d&#xff1a;\u8bb0\u5f55\u65e5\u5fd7&#034;);<\/p>\n<p>        \/\/ \u8c03\u7528\u7236\u7c7b\u539f\u59cb\u4e1a\u52a1\u65b9\u6cd5<br \/>\n        super.addUser();<\/p>\n<p>        \/\/ \u3010Advice\u589e\u5f3a\u903b\u8f91\u3011\u540e\u7f6e\u901a\u77e5<br \/>\n        System.out.println(&#034;\u65b9\u6cd5\u6267\u884c\u540e&#034;);<br \/>\n    }<\/p>\n<p>    \/\/ \u2757 deleteUser \u662ffinal&#xff0c;\u5b50\u7c7b\u4e0d\u80fd\u91cd\u5199&#xff0c;\u8fd9\u91cc\u65e0\u6cd5\u751f\u6210\u589e\u5f3a\u903b\u8f91<br \/>\n    \/\/ \u4e0d\u4f1a\u751f\u6210\u91cd\u5199\u7684deleteUser\u65b9\u6cd5<br \/>\n}<\/p>\n<p>\u4f7f\u7528<\/p>\n<p> \/\/ Spring\u62ff\u5230\u7684Bean&#xff0c;\u5176\u5b9e\u662f UserService$$CGLIB \u4ee3\u7406\u5bf9\u8c61<br \/>\nUserService userService &#061; context.getBean(UserService.class);<br \/>\nuserService.addUser(); \/\/ \u8d70\u4ee3\u7406&#xff0c;\u89e6\u53d1AOP\u589e\u5f3a \u2705<br \/>\nuserService.deleteUser(); \/\/ \u76f4\u63a5\u8c03\u7528\u7236\u7c7b\u539f\u59cb\u65b9\u6cd5&#xff0c;**\u4e0d\u4f1a\u89e6\u53d1AOP** \u274c <\/p>\n<p>JDK \u52a8\u6001\u4ee3\u7406<\/p>\n<p>\u524d\u63d0&#xff1a;<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u76ee\u6807\u7c7b\u5fc5\u987b\u81f3\u5c11\u5b9e\u73b0 1 \u4e2a\u63a5\u53e3\u3002<\/p>\n<p>\u9650\u5236&#xff1a;<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u53ea\u80fd\u62e6\u622a\u63a5\u53e3\u4e2d\u5b9a\u4e49\u7684\u65b9\u6cd5&#xff1b;<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u76ee\u6807\u7c7b\u81ea\u5df1\u65b0\u589e\u7684\u3001\u4e0d\u5728\u63a5\u53e3\u91cc\u7684\u65b9\u6cd5&#xff0c;\u4ee3\u7406\u5bf9\u8c61\u8c03\u7528\u4e0d\u5230\u3002<\/p>\n<p>1. \u5b8c\u6574\u4ee3\u7801\u793a\u4f8b<\/p>\n<p>\u2460 \u5148\u5b9a\u4e49\u63a5\u53e3&#xff08;JDK \u4ee3\u7406\u79bb\u4e0d\u5f00\u8fd9\u4e2a&#xff09;<\/p>\n<p> public interface UserService {<br \/>\n    void addUser();<br \/>\n}<\/p>\n<p>\u2461 \u76ee\u6807\u5b9e\u73b0\u7c7b&#xff08;Target&#xff0c;\u5b9e\u73b0\u4e0a\u9762\u63a5\u53e3&#xff09;<\/p>\n<p> \/\/ \u76ee\u6807\u7c7b&#xff0c;\u5b9e\u73b0\u4e86UserService\u63a5\u53e3<br \/>\npublic class UserServiceImpl implements UserService {<\/p>\n<p>    &#064;Override<br \/>\n    public void addUser() {<br \/>\n        System.out.println(&#034;\u3010\u4e1a\u52a1\u3011\u65b0\u589e\u7528\u6237&#034;);<br \/>\n    }<\/p>\n<p>    \/\/ \u8fd9\u4e2a\u65b9\u6cd5\u4e0d\u5728\u63a5\u53e3\u91cc\u9762&#xff01;\u91cd\u70b9&#xff01;<br \/>\n    public void hello() {<br \/>\n        System.out.println(&#034;hello&#xff0c;\u8fd9\u662f\u5b9e\u73b0\u7c7b\u72ec\u6709\u7684\u65b9\u6cd5&#034;);<br \/>\n    }<br \/>\n}<\/p>\n<p>\u2462 JDK \u4ee3\u7406\u5de5\u5382&#xff0c;\u751f\u6210\u4ee3\u7406\u5bf9\u8c61<\/p>\n<p> import java.lang.reflect.InvocationHandler;<br \/>\nimport java.lang.reflect.Method;<br \/>\nimport java.lang.reflect.Proxy;<\/p>\n<p>public class JdkProxyFactory {<br \/>\n    \/\/ target&#xff1a;\u539f\u59cb\u76ee\u6807\u5bf9\u8c61 UserServiceImpl<br \/>\n    public static UserService getProxy(UserService target) {<br \/>\n        \/\/ InvocationHandler&#xff1a;\u56de\u8c03&#xff0c;\u4ee3\u7406\u65b9\u6cd5\u88ab\u8c03\u7528\u65f6\u8fdb\u5165\u8fd9\u91cc&#xff08;\u5bf9\u5e94AOP\u7684Advice&#xff09;<br \/>\n        InvocationHandler handler &#061; new InvocationHandler() {<br \/>\n            &#064;Override<br \/>\n            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {<br \/>\n                \/\/ \u524d\u7f6e\u589e\u5f3a&#xff08;Advice&#xff09;<br \/>\n                System.out.println(&#034;\u3010JDK\u4ee3\u7406\u524d\u7f6e\u3011\u65b9\u6cd5\u6267\u884c\u524d&#034;);<br \/>\n                \/\/ \u6267\u884c\u539f\u59cb\u76ee\u6807\u65b9\u6cd5<br \/>\n                Object result &#061; method.invoke(target, args);<br \/>\n                \/\/ \u540e\u7f6e\u589e\u5f3a&#xff08;Advice&#xff09;<br \/>\n                System.out.println(&#034;\u3010JDK\u4ee3\u7406\u540e\u7f6e\u3011\u65b9\u6cd5\u6267\u884c\u540e&#034;);<br \/>\n                return result;<br \/>\n            }<br \/>\n        };<\/p>\n<p>        \/\/ \u6838\u5fc3&#xff1a;JDK Proxy.newProxyInstance<br \/>\n        \/\/ \u53c2\u6570&#xff1a;\u7c7b\u52a0\u8f7d\u5668\u3001\u3010\u63a5\u53e3\u6570\u7ec4\u3011\u3001InvocationHandler<br \/>\n        return (UserService) Proxy.newProxyInstance(<br \/>\n                target.getClass().getClassLoader(),<br \/>\n                target.getClass().getInterfaces(), \/\/ \u2705 \u4f20\u5165\u63a5\u53e3&#xff01;JDK\u4ee3\u7406\u9760\u8fd9\u4e2a\u751f\u6210\u4ee3\u7406\u7c7b<br \/>\n                handler<br \/>\n        );<br \/>\n    }<br \/>\n}<\/p>\n<p>\u2463 \u6d4b\u8bd5\u8c03\u7528<\/p>\n<p> public class Test {<br \/>\n    public static void main(String[] args) {<br \/>\n        UserService target &#061; new UserServiceImpl();<br \/>\n        \/\/ \u62ff\u5230\u4ee3\u7406\u5bf9\u8c61&#xff0c;\u6ce8\u610f&#xff1a;\u4ee3\u7406\u5bf9\u8c61\u7c7b\u578b\u662f \u63a5\u53e3 UserService&#xff0c;\u4e0d\u662f UserServiceImpl<br \/>\n        UserService proxy &#061; JdkProxyFactory.getProxy(target);<\/p>\n<p>        proxy.addUser(); \/\/ \u2705 \u5728\u63a5\u53e3\u4e2d&#xff0c;\u8d70\u4ee3\u7406&#xff0c;\u89e6\u53d1\u589e\u5f3a<\/p>\n<p>        \/\/ proxy.hello(); \/\/ \u2757\u7f16\u8bd1\u62a5\u9519&#xff01;<br \/>\n        \/\/ proxy \u662f\u63a5\u53e3\u7c7b\u578bUserService&#xff0c;\u63a5\u53e3\u6ca1\u6709hello\u65b9\u6cd5&#xff0c;\u8c03\u7528\u4e0d\u5230\u5b9e\u73b0\u7c7b\u72ec\u6709\u7684\u65b9\u6cd5<br \/>\n    }<br \/>\n}<\/p>\n<h4>\u8f93\u51fa\u7ed3\u679c<\/h4>\n<p> \u3010JDK\u4ee3\u7406\u524d\u7f6e\u3011\u65b9\u6cd5\u6267\u884c\u524d<br \/>\n\u3010\u4e1a\u52a1\u3011\u65b0\u589e\u7528\u6237<br \/>\n\u3010JDK\u4ee3\u7406\u540e\u7f6e\u3011\u65b9\u6cd5\u6267\u884c\u540e<\/p>\n<p>2. JDK \u52a8\u6001\u751f\u6210\u7684\u4ee3\u7406\u7c7b\u4f2a\u4ee3\u7801&#xff08;\u91cd\u70b9\u7406\u89e3&#xff09;<\/p>\n<p>JDK \u5728\u8fd0\u884c\u65f6\u81ea\u52a8\u751f\u6210 $Proxy0&#xff0c;implements \u63a5\u53e3&#xff0c;\u4e0d\u662f extends \u76ee\u6807\u7c7b&#xff01;<\/p>\n<p> \/\/ JDK\u81ea\u52a8\u751f\u6210\u7684\u4ee3\u7406\u7c7b $Proxy0<br \/>\npublic class $Proxy0 implements UserService {<br \/>\n    private InvocationHandler h;<\/p>\n<p>    public $Proxy0(InvocationHandler h) {<br \/>\n        this.h &#061; h;<br \/>\n    }<\/p>\n<p>    &#064;Override<br \/>\n    public void addUser() {<br \/>\n        \/\/ \u8c03\u7528\u6211\u4eec\u5199\u7684invoke&#xff0c;\u6267\u884c\u589e\u5f3a\u903b\u8f91<br \/>\n        h.invoke(this, addUserMethod, null);<br \/>\n    }<\/p>\n<p>}<\/p>\n<p>\u5173\u952e\u70b9&#xff1a;$Proxy0 implements UserService \u4ee3\u7406\u7c7b\u548cUserServiceImpl\u662f\u5e73\u7ea7&#xff0c;\u90fd\u5b9e\u73b0\u540c\u4e00\u4e2a\u63a5\u53e3&#xff0c;\u6ca1\u6709\u7236\u5b50\u7ee7\u627f\u5173\u7cfb&#xff01;<\/p>\n<p>\u00a0\u6269\u5c55&#xff1a;<\/p>\n<p>Spring AOP \u5e95\u5c42\u6838\u5fc3\u662f\u4ee3\u7406\u6a21\u5f0f\u3002<\/p>\n<p>\u4e24\u79cd\u4ee3\u7406\u5b9e\u73b0&#xff1a;JDK \u52a8\u6001\u4ee3\u7406\u548cCGLIB \u52a8\u6001\u4ee3\u7406<\/p>\n<p>\u4ee3\u7406\u6a21\u5f0f\u6838\u5fc3\u610f\u56fe&#xff1a;\u63a7\u5236\u5bf9\u76ee\u6807\u5bf9\u8c61 (Target) \u7684\u8bbf\u95ee&#xff0c;\u5728\u4e0d\u4fee\u6539\u76ee\u6807\u7c7b\u6e90\u7801\u7684\u524d\u63d0\u4e0b&#xff0c;\u5728\u65b9\u6cd5\u524d\u540e\u589e\u52a0\u989d\u5916\u903b\u8f91&#xff08;Advice&#xff0c;\u65e5\u5fd7\u3001\u4e8b\u52a1&#xff09;&#xff0c;\u5b8c\u7f8e\u5951\u5408 AOP \u7684\u9700\u6c42\u3002<\/p>\n<p> \u8c03\u7528\u8005 \u2192 Proxy\u4ee3\u7406\u5bf9\u8c61&#xff08;\u589e\u5f3a\u903b\u8f91&#xff09; \u2192 Target\u76ee\u6807\u5bf9\u8c61&#xff08;\u539f\u59cb\u4e1a\u52a1&#xff09;<\/p>\n<p>\u4f46\u662f&#xff1a;Spring AOP \u4e0d\u53ea\u7528\u4ee3\u7406\u6a21\u5f0f<\/p>\n<p>\u8fd8\u7528\u5230\u522b\u7684\u8bbe\u8ba1\u6a21\u5f0f&#xff0c;\u4f8b\u5982&#xff1a;<\/p>\n<li>\u5de5\u5382\u6a21\u5f0f&#xff1a;ProxyFactoryBean \/ DefaultAopProxyFactory&#xff0c;\u6839\u636e\u6761\u4ef6\u5224\u65ad\u521b\u5efa JDK \u4ee3\u7406\u8fd8\u662f CGLIB \u4ee3\u7406\u5bf9\u8c61\u3002<\/li>\n<li>\u88c5\u9970\u5668\u6a21\u5f0f&#xff08;\u5bb9\u6613\u548c\u4ee3\u7406\u6df7\u6dc6&#xff09;<\/li>\n<p>\u7b80\u5355\u533a\u5206<\/p>\n<ul>\n<li>\u4ee3\u7406\u6a21\u5f0f&#xff1a;\u91cd\u70b9\u662f\u63a7\u5236\u8bbf\u95ee&#xff0c;\u4ee3\u7406\u548c\u76ee\u6807\u5b9e\u73b0\u540c\u4e00\u4e2a\u63a5\u53e3 \/ \u7ee7\u627f\u540c\u4e00\u4e2a\u7c7b&#xff0c;\u4ee3\u7406\u6301\u6709\u76ee\u6807\u5bf9\u8c61\u3002Spring AOP \u5c5e\u4e8e\u4ee3\u7406\u3002<\/li>\n<li>\u88c5\u9970\u5668\u6a21\u5f0f&#xff1a;\u91cd\u70b9\u662f\u52a8\u6001\u589e\u5f3a\u5bf9\u8c61\u529f\u80fd&#xff0c;\u901a\u5e38\u5305\u88c5\u5bf9\u8c61\u5c42\u5c42\u5d4c\u5957\u3002<\/li>\n<\/ul>\n<p>\u5173\u4e8e\u8bbe\u8ba1\u6a21\u5f0f&#xff0c;\u6211\u5199\u8fc7\u4e00\u7bc7\u535a\u5ba2\u8be6\u89e3&#xff1a;23\u79cd\u8bbe\u8ba1\u6a21\u5f0f\u5b8c\u6574\u8bb2\u89e3&#xff08;\u573a\u666f &#043; \u4f8b\u5b50 &#043; \u4ee3\u7801&#xff09;<\/p>\n<h3>\u56db\u3001AOP\u5207\u70b9\u8868\u8fbe\u5f0f execution&#xff08;\u6700\u5e38\u7528&#xff09;<\/h3>\n<p>\u8bed\u6cd5&#xff1a;<\/p>\n<p>execution(\u4fee\u9970\u7b26 \u8fd4\u56de\u503c \u5305\u540d.\u7c7b\u540d.\u65b9\u6cd5\u540d(\u53c2\u6570\u5217\u8868))<\/p>\n<p>\u901a\u914d\u7b26\u8bf4\u660e&#xff1a;<\/p>\n<ul>\n<li>*&#xff1a;\u5339\u914d\u4efb\u610f\u5b57\u7b26&#xff08;\u5305\u3001\u7c7b\u3001\u65b9\u6cd5\u3001\u8fd4\u56de\u503c&#xff09;<\/li>\n<li>..&#xff1a;\u5305\u8def\u5f84\u4efb\u610f\u591a\u7ea7&#xff1b;\u65b9\u6cd5\u53c2\u6570\u4efb\u610f\u4e2a\u6570\u4efb\u610f\u7c7b\u578b<\/li>\n<li>&#043;&#xff1a;\u5339\u914d\u7c7b\u53ca\u5176\u5b50\u7c7b<\/li>\n<\/ul>\n<p>\u793a\u4f8b&#xff1a;<\/p>\n<p>\/\/ \u5339\u914d com.service \u5305\u4e0b\u6240\u6709\u7c7b\u7684\u6240\u6709public\u65b9\u6cd5<br \/>\nexecution(public * com.service.*.*(..))<\/p>\n<p>\/\/ \u5339\u914d com.service\u5305\u53ca\u5176\u5b50\u5305\u4e0b\u6240\u6709\u7c7b\u6240\u6709\u65b9\u6cd5<br \/>\nexecution(* com.service..*.*(..))<\/p>\n<p>\/\/ \u5339\u914d UserService \u7c7b\u4e0b\u6240\u6709\u65b9\u6cd5<br \/>\nexecution(* com.service.UserService.*(..))<\/p>\n<p>\/\/ \u5339\u914d save\u5f00\u5934\u7684\u65b9\u6cd5<br \/>\nexecution(* com.service..*.save*(..))<\/p>\n<p>\u9664\u4e86 execution&#xff0c;\u8fd8\u6709\u5176\u4ed6\u5207\u70b9\u6307\u793a\u5668&#xff1a;<\/p>\n<ul>\n<li>within&#xff1a;\u9650\u5b9a\u7c7b<\/li>\n<li>&#064;annotation&#xff1a;\u5339\u914d\u6807\u6ce8\u4e86\u6307\u5b9a\u6ce8\u89e3\u7684\u65b9\u6cd5&#xff08;\u6700\u5e38\u7528&#xff0c;\u81ea\u5b9a\u4e49\u6ce8\u89e3\u505a\u5207\u9762&#xff09;<\/li>\n<li>args&#xff1a;\u5339\u914d\u65b9\u6cd5\u53c2\u6570\u7c7b\u578b<\/li>\n<li>&#064;target \/ &#064;this&#xff1a;\u5339\u914d\u7c7b\u4e0a\u6ce8\u89e3<\/li>\n<\/ul>\n<p>&#064;annotation \u5b9e\u6218\u573a\u666f&#xff1a;\u81ea\u5b9a\u4e49\u6ce8\u89e3&#064;LogRecord&#xff0c;\u65b9\u6cd5\u6253\u4e0a\u6ce8\u89e3\u5c31\u81ea\u52a8\u8bb0\u5f55\u64cd\u4f5c\u65e5\u5fd7\u3002<\/p>\n<hr \/>\n<h3>\u4e94\u3001AOP\u901a\u77e5\u6267\u884c\u987a\u5e8f<\/h3>\n<h4>\u6b63\u5e38\u6267\u884c&#xff08;\u65e0\u5f02\u5e38&#xff09;<\/h4>\n<p>&#064;Around \u524d\u7f6e\u903b\u8f91 \u2192 &#064;Before \u2192 \u76ee\u6807\u65b9\u6cd5\u6267\u884c \u2192 &#064;AfterReturning \u2192 &#064;After \u2192 &#064;Around \u540e\u7f6e\u903b\u8f91<\/p>\n<h4>\u629b\u51fa\u5f02\u5e38<\/h4>\n<p>&#064;Around \u524d\u7f6e\u903b\u8f91 \u2192 &#064;Before \u2192 \u76ee\u6807\u65b9\u6cd5\u629b\u51fa\u5f02\u5e38 \u2192 &#064;AfterThrowing \u2192 &#064;After<\/p>\n<p>&#064;AfterReturning \u4e0d\u4f1a\u6267\u884c&#xff0c;\u73af\u7ed5\u901a\u77e5\u91cc\u5982\u679c\u4e0d catch \u5f02\u5e38&#xff0c;\u5f02\u5e38\u7ee7\u7eed\u5411\u4e0a\u629b\u51fa\u3002<\/p>\n<p>\u591a\u4e2a\u5207\u9762\u65f6&#xff0c;\u53ef\u4ee5\u7528&#064;Order(int)\u6307\u5b9a\u5207\u9762\u4f18\u5148\u7ea7&#xff0c;\u6570\u5b57\u8d8a\u5c0f\u4f18\u5148\u7ea7\u8d8a\u9ad8\u3002<\/p>\n<p>\u9ad8\u4f18\u5148\u7ea7\u5207\u9762&#xff1a;\u5148\u8fdb\u5148\u51fa&#xff0c;\u540e\u8fdb\u5148\u51fa\u3002<\/p>\n<p>\u5207\u9762 A order&#061;1&#xff0c;\u5207\u9762 B order&#061;2&#xff1a;<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0A \u524d\u7f6e \u2192<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0B \u524d\u7f6e \u2192 \u76ee\u6807\u65b9\u6cd5 \u2192 B \u8fd4\u56de \/ \u540e\u7f6e \u2192<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0A \u8fd4\u56de \/ \u540e\u7f6e<\/p>\n<p><span style=\"color:#b95514\">\u5bf9\u5e94\u4e1a\u52a1\u573a\u666f\u7406\u89e3<\/span><\/p>\n<p>\u5047\u8bbe\u8fd9\u4e2a\u5207\u9762\u505a\u63a5\u53e3\u8017\u65f6\u7edf\u8ba1 &#043; \u65e5\u5fd7\u8bb0\u5f55<\/p>\n<li>&#064;Around \u524d\u7f6e&#xff1a;\u8bb0\u5f55\u5f00\u59cb\u65f6\u95f4&#xff08;startTime &#061; System.currentTimeMillis()&#xff09;<\/li>\n<li>&#064;Before&#xff1a;\u6821\u9a8c\u8bf7\u6c42\u53c2\u6570\u3001\u6253\u5370\u5165\u53c2&#xff08;\u4e1a\u52a1\u6267\u884c\u524d\u6821\u9a8c&#xff09;<\/li>\n<li>\u76ee\u6807\u65b9\u6cd5&#xff1a;\u6267\u884c\u4e1a\u52a1\u903b\u8f91&#xff0c;\u521b\u5efa\u8ba2\u5355<\/li>\n<li>&#064;AfterReturning&#xff1a;\u62ff\u5230\u8fd4\u56de\u8ba2\u5355\u53f7&#xff0c;\u8bb0\u5f55\u4e1a\u52a1\u6210\u529f\u65e5\u5fd7\u3001\u57cb\u70b9\u4e0a\u62a5\u6210\u529f\u4e8b\u4ef6<\/li>\n<li>&#064;After&#xff1a;\u8d44\u6e90\u6e05\u7406&#xff08;\u5173\u95ed\u4e00\u4e9b\u4e34\u65f6\u8d44\u6e90&#xff0c;\u4e0d\u7ba1\u6210\u529f\u5931\u8d25\u90fd\u6e05\u7406&#xff09;<\/li>\n<li>&#064;Around \u540e\u7f6e&#xff1a;\u8ba1\u7b97\u603b\u8017\u65f6&#xff0c;\u6253\u5370\u63a5\u53e3\u8017\u65f6<\/li>\n<p>\u6982\u62ec&#xff1a;<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u7edf\u8ba1\u63a5\u53e3\u8017\u65f6\u653e\u5728 Around&#xff0c;<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u53c2\u6570\u6821\u9a8c\u653e Before&#xff0c;<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u6210\u529f\u57cb\u70b9 AfterReturning&#xff0c;<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u8d44\u6e90\u91ca\u653e\u653e After\u3002<\/p>\n<h3>\u516d\u3001\u7b80\u5355\u5b8c\u6574\u4ee3\u7801\u793a\u4f8b<\/h3>\n<p>&lt;!&#8211; spring-boot-starter-aop \u81ea\u5e26aspectj\u6ce8\u89e3\u652f\u6301 &#8211;&gt;<br \/>\n&lt;dependency&gt;<br \/>\n    &lt;groupId&gt;org.springframework.boot&lt;\/groupId&gt;<br \/>\n    &lt;artifactId&gt;spring-boot-starter-aop&lt;\/artifactId&gt;<br \/>\n&lt;\/dependency&gt;<\/p>\n<h4>2. \u4e1a\u52a1\u7c7b&#xff08;\u76ee\u6807\u7c7b&#xff09;<\/h4>\n<p>&#064;Service<br \/>\npublic class UserService {<br \/>\n    public void addUser(String name){<br \/>\n        System.out.println(&#034;\u4e1a\u52a1&#xff1a;\u65b0\u589e\u7528\u6237 &#034; &#043; name);<br \/>\n    }<br \/>\n}<\/p>\n<h4>3. \u5207\u9762\u7c7b&#xff08;&#064;Aspect &#043; &#064;Component&#xff09;<\/h4>\n<p>&#064;Aspect<br \/>\n&#064;Component<br \/>\npublic class LogAspect {<\/p>\n<p>    \/\/ \u5b9a\u4e49\u5207\u70b9&#xff1a;\u5339\u914dUserService\u6240\u6709\u65b9\u6cd5<br \/>\n    &#064;Pointcut(&#034;execution(* com.service.UserService.*(..))&#034;)<br \/>\n    public void pointCut(){}<\/p>\n<p>    \/\/ \u524d\u7f6e\u901a\u77e5<br \/>\n    &#064;Before(&#034;pointCut()&#034;)<br \/>\n    public void before(JoinPoint joinPoint){<br \/>\n        String methodName &#061; joinPoint.getSignature().getName();<br \/>\n        Object[] args &#061; joinPoint.getArgs();<br \/>\n        System.out.println(&#034;\u3010\u524d\u7f6e\u901a\u77e5\u3011\u65b9\u6cd5&#xff1a;&#034;&#043;methodName &#043; &#034; \u53c2\u6570&#xff1a;&#034;&#043; Arrays.toString(args));<br \/>\n    }<\/p>\n<p>    \/\/ \u8fd4\u56de\u901a\u77e5<br \/>\n    &#064;AfterReturning(value &#061; &#034;pointCut()&#034;, returning &#061; &#034;result&#034;)<br \/>\n    public void afterReturn(Object result){<br \/>\n        System.out.println(&#034;\u3010\u8fd4\u56de\u901a\u77e5\u3011\u65b9\u6cd5\u6b63\u5e38\u6267\u884c&#xff0c;\u8fd4\u56de\u503c&#xff1a;&#034;&#043;result);<br \/>\n    }<\/p>\n<p>    \/\/ \u5f02\u5e38\u901a\u77e5<br \/>\n    &#064;AfterThrowing(value &#061; &#034;pointCut()&#034;, throwing &#061; &#034;ex&#034;)<br \/>\n    public void afterThrow(Exception ex){<br \/>\n        System.out.println(&#034;\u3010\u5f02\u5e38\u901a\u77e5\u3011\u6355\u83b7\u5f02\u5e38&#xff1a;&#034;&#043;ex.getMessage());<br \/>\n    }<\/p>\n<p>    \/\/ \u6700\u7ec8\u901a\u77e5<br \/>\n    &#064;After(&#034;pointCut()&#034;)<br \/>\n    public void after(){<br \/>\n        System.out.println(&#034;\u3010\u6700\u7ec8\u901a\u77e5\u3011\u65e0\u8bba\u662f\u5426\u5f02\u5e38\u90fd\u4f1a\u6267\u884c&#034;);<br \/>\n    }<\/p>\n<p>    \/\/ \u73af\u7ed5\u901a\u77e5&#xff0c;ProceedingJoinPoint\u53ea\u80fd\u7528\u5728Around<br \/>\n    &#064;Around(&#034;pointCut()&#034;)<br \/>\n    public Object around(ProceedingJoinPoint pjp) throws Throwable {<br \/>\n        System.out.println(&#034;\u3010\u73af\u7ed5\u901a\u77e5\u3011\u65b9\u6cd5\u6267\u884c\u524d&#034;);<br \/>\n        \/\/ \u6267\u884c\u76ee\u6807\u65b9\u6cd5<br \/>\n        Object res &#061; pjp.proceed();<br \/>\n        System.out.println(&#034;\u3010\u73af\u7ed5\u901a\u77e5\u3011\u65b9\u6cd5\u6267\u884c\u540e&#034;);<br \/>\n        return res;<br \/>\n    }<br \/>\n}<\/p>\n<p>\u8c03\u7528 userService.addUser(&#034;\u5f20\u4e09&#034;)&#xff0c;\u8f93\u51fa\u987a\u5e8f&#xff1a;<\/p>\n<p>\u3010\u73af\u7ed5\u901a\u77e5\u3011\u65b9\u6cd5\u6267\u884c\u524d<br \/>\n\u3010\u524d\u7f6e\u901a\u77e5\u3011\u65b9\u6cd5&#xff1a;addUser \u53c2\u6570&#xff1a;[\u5f20\u4e09]<br \/>\n    \u4e1a\u52a1&#xff1a;\u65b0\u589e\u7528\u6237 \u5f20\u4e09<br \/>\n\u3010\u8fd4\u56de\u901a\u77e5\u3011\u65b9\u6cd5\u6b63\u5e38\u6267\u884c&#xff0c;\u8fd4\u56de\u503c&#xff1a;null<br \/>\n\u3010\u6700\u7ec8\u901a\u77e5\u3011\u65e0\u8bba\u662f\u5426\u5f02\u5e38\u90fd\u4f1a\u6267\u884c<br \/>\n\u3010\u73af\u7ed5\u901a\u77e5\u3011\u65b9\u6cd5\u6267\u884c\u540e<\/p>\n<p>ProceedingJoinPoint \u662f JoinPoint \u5b50\u7c7b&#xff0c;\u53ea\u6709\u73af\u7ed5\u901a\u77e5\u53ef\u4ee5\u4f7f\u7528&#xff0c;\u8c03\u7528proceed()\u624d\u4f1a\u6267\u884c\u76ee\u6807\u65b9\u6cd5&#xff1b;<\/p>\n<p>\u53ef\u4ee5\u4e0d\u8c03\u7528&#xff0c;\u76f4\u63a5\u62e6\u622a&#xff0c;\u4e5f\u53ef\u4ee5\u591a\u6b21\u8c03\u7528\u3002<\/p>\n<h3>\u4e03\u3001\u5b8c\u6574 AOP \u5b9e\u6218\u6848\u4f8b&#xff1a;\u63a5\u53e3\u64cd\u4f5c\u65e5\u5fd7\u5207\u9762<\/h3>\n<p>\u4e1a\u52a1\u573a\u666f\u9700\u6c42<\/p>\n<p>\u540e\u53f0\u7ba1\u7406\u7cfb\u7edf&#xff0c;\u9700\u8981\u8bb0\u5f55\u7ba1\u7406\u5458\u64cd\u4f5c\u65e5\u5fd7&#xff1a;<\/p>\n<li>\u8fdb\u5165\u63a5\u53e3\u524d&#xff1a;\u83b7\u53d6\u5f53\u524d\u767b\u5f55\u7528\u6237\u3001\u8bf7\u6c42\u53c2\u6570<\/li>\n<li>\u63a5\u53e3\u6b63\u5e38\u6267\u884c\u6210\u529f&#xff1a;\u8bb0\u5f55\u3010\u64cd\u4f5c\u6210\u529f\u3011\u3001\u63a5\u53e3\u8fd4\u56de\u7ed3\u679c<\/li>\n<li>\u63a5\u53e3\u629b\u51fa\u5f02\u5e38&#xff1a;\u8bb0\u5f55\u3010\u64cd\u4f5c\u5931\u8d25\u3011\u3001\u5f02\u5e38\u4fe1\u606f<\/li>\n<li>\u7edf\u8ba1\u63a5\u53e3\u6267\u884c\u8017\u65f6<\/li>\n<p>\u8981\u6c42&#xff1a;<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u4e1a\u52a1\u4ee3\u7801\u5b8c\u5168\u4e0d\u5199\u65e5\u5fd7\u76f8\u5173\u4ee3\u7801&#xff0c;\u7528 AOP \u6a2a\u5207\u7edf\u4e00\u5b9e\u73b0\u3002<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u7528\u5230\u5168\u90e8 AOP \u7ec4\u4ef6&#xff1a;\u5207\u9762 Aspect\u3001\u5207\u70b9 Pointcut\u3001\u901a\u77e5 Advice\u3001\u8fde\u63a5\u70b9 JoinPoint<\/p>\n<h4>\u6b65\u9aa4 1&#xff1a;\u81ea\u5b9a\u4e49\u6ce8\u89e3<\/h4>\n<p>\u7528\u6765\u6807\u8bb0\u9700\u8981\u8bb0\u5f55\u64cd\u4f5c\u65e5\u5fd7\u7684\u63a5\u53e3<\/p>\n<p>\/**<br \/>\n * \u6807\u8bb0\u8be5\u65b9\u6cd5\u9700\u8981\u8bb0\u5f55\u64cd\u4f5c\u65e5\u5fd7<br \/>\n *\/<br \/>\n&#064;Target(ElementType.METHOD)<br \/>\n&#064;Retention(RetentionPolicy.RUNTIME)<br \/>\npublic &#064;interface OperateLog {<br \/>\n    \/\/ \u64cd\u4f5c\u63cf\u8ff0&#xff1a;\u65b0\u589e\u7528\u6237\u3001\u4fee\u6539\u8ba2\u5355\u7b49<br \/>\n    String value();<br \/>\n}<\/p>\n<h4>\u6b65\u9aa4 2&#xff1a;\u5b9e\u4f53\u7c7b<\/h4>\n<p>\u64cd\u4f5c\u65e5\u5fd7\u4fdd\u5b58\u5bf9\u8c61<\/p>\n<p>&#064;Data<br \/>\npublic class OperateLogDTO {<br \/>\n    private String username;    \/\/ \u5f53\u524d\u64cd\u4f5c\u4eba<br \/>\n    private String operateDesc; \/\/ \u64cd\u4f5c\u63cf\u8ff0<br \/>\n    private String params;      \/\/ \u8bf7\u6c42\u53c2\u6570<br \/>\n    private String result;      \/\/ \u8fd4\u56de\u7ed3\u679c<br \/>\n    private Long costTime;      \/\/ \u8017\u65f6ms<br \/>\n    private boolean success;    \/\/ \u662f\u5426\u6210\u529f<br \/>\n    private String errorMsg;    \/\/ \u5f02\u5e38\u4fe1\u606f<br \/>\n}<\/p>\n<h4>\u6b65\u9aa4 3&#xff1a;\u5207\u9762\u7c7b<\/h4>\n<p><span style=\"color:#fe2c24\">\u6838\u5fc3&#xff01;<\/span>\u5305\u542b\u5207\u70b9 &#043; 5 \u79cd\u901a\u77e5<\/p>\n<p>import org.aspectj.lang.JoinPoint;<br \/>\nimport org.aspectj.lang.ProceedingJoinPoint;<br \/>\nimport org.aspectj.lang.annotation.*;<br \/>\nimport org.springframework.stereotype.Component;<br \/>\nimport com.alibaba.fastjson2.JSON;<\/p>\n<p>&#064;Aspect<br \/>\n&#064;Component<br \/>\npublic class OperateLogAspect {<\/p>\n<p>    \/\/ &#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061; \u5207\u70b9 Pointcut &#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;<br \/>\n    \/\/ \u5207\u70b9\u89c4\u5219&#xff1a;\u62e6\u622a\u3010\u52a0\u4e86&#064;OperateLog\u6ce8\u89e3\u3011\u7684\u65b9\u6cd5<br \/>\n    &#064;Pointcut(&#034;&#064;annotation(operateLog)&#034;)<br \/>\n    public void logPointcut(OperateLog operateLog) {<br \/>\n    }<\/p>\n<p>    \/\/ &#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061; \u73af\u7ed5\u901a\u77e5 Around&#xff08;\u5305\u88f9\u6574\u4e2a\u76ee\u6807\u65b9\u6cd5&#xff09; &#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;<br \/>\n    &#064;Around(&#034;logPointcut(operateLog)&#034;)<br \/>\n    public Object around(ProceedingJoinPoint joinPoint, OperateLog operateLog) throws Throwable {<br \/>\n        OperateLogDTO logDto &#061; new OperateLogDTO();<br \/>\n        long start &#061; System.currentTimeMillis();<br \/>\n        logDto.setOperateDesc(operateLog.value());<br \/>\n        logDto.setUsername(&#034;admin&#034;); \/\/ \u6a21\u62df\u4ece\u4e0a\u4e0b\u6587\u83b7\u53d6\u767b\u5f55\u7528\u6237<br \/>\n        logDto.setParams(JSON.toJSONString(joinPoint.getArgs()));<\/p>\n<p>        Object result;<br \/>\n        try {<br \/>\n            \/\/ proceed()&#xff1a;\u6267\u884c\u76ee\u6807\u65b9\u6cd5&#xff0c;\u4e2d\u95f4\u4f1a\u89e6\u53d1&#064;Before<br \/>\n            result &#061; joinPoint.proceed();<br \/>\n            logDto.setSuccess(true);<br \/>\n            logDto.setResult(JSON.toJSONString(result));<br \/>\n        } catch (Throwable e) {<br \/>\n            logDto.setSuccess(false);<br \/>\n            logDto.setErrorMsg(e.getMessage());<br \/>\n            throw e; \/\/ \u7ee7\u7eed\u5411\u4e0a\u629b\u5f02\u5e38&#xff0c;\u4ea4\u7ed9\u5168\u5c40\u5f02\u5e38\u5904\u7406\u5668<br \/>\n        } finally {<br \/>\n            long cost &#061; System.currentTimeMillis() &#8211; start;<br \/>\n            logDto.setCostTime(cost);<br \/>\n            \/\/ \u6a21\u62df\u4fdd\u5b58\u65e5\u5fd7\u5230\u6570\u636e\u5e93<br \/>\n            saveLog(logDto);<br \/>\n        }<br \/>\n        return result;<br \/>\n    }<\/p>\n<p>    \/\/ &#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061; \u524d\u7f6e\u901a\u77e5 Before &#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;<br \/>\n    &#064;Before(&#034;logPointcut(operateLog)&#034;)<br \/>\n    public void before(JoinPoint joinPoint, OperateLog operateLog) {<br \/>\n        System.out.println(&#034;&#061;&#061;&#061;&#061; &#064;Before \u6267\u884c&#xff1a;\u51c6\u5907\u6267\u884c\u3010&#034; &#043; operateLog.value() &#043; &#034;\u3011\u64cd\u4f5c &#061;&#061;&#061;&#061;&#034;);<br \/>\n    }<\/p>\n<p>    \/\/ &#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061; \u6b63\u5e38\u8fd4\u56de\u901a\u77e5 AfterReturning &#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;<br \/>\n    &#064;AfterReturning(value &#061; &#034;logPointcut(operateLog)&#034;, returning &#061; &#034;ret&#034;)<br \/>\n    public void afterReturning(JoinPoint joinPoint, OperateLog operateLog, Object ret) {<br \/>\n        System.out.println(&#034;&#061;&#061;&#061;&#061; &#064;AfterReturning \u6267\u884c&#xff1a;\u64cd\u4f5c\u3010&#034;&#043;operateLog.value()&#043;&#034;\u3011\u6210\u529f&#xff0c;\u8fd4\u56de\u503c&#xff1a;&#034; &#043; ret);<br \/>\n    }<\/p>\n<p>    \/\/ &#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061; \u5f02\u5e38\u901a\u77e5 AfterThrowing &#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;<br \/>\n    &#064;AfterThrowing(value &#061; &#034;logPointcut(operateLog)&#034;, throwing &#061; &#034;ex&#034;)<br \/>\n    public void afterThrowing(JoinPoint joinPoint, OperateLog operateLog, Throwable ex) {<br \/>\n        System.out.println(&#034;&#061;&#061;&#061;&#061; &#064;AfterThrowing \u6267\u884c&#xff1a;\u64cd\u4f5c\u3010&#034;&#043;operateLog.value()&#043;&#034;\u3011\u53d1\u751f\u5f02\u5e38&#xff1a;&#034; &#043; ex.getMessage());<br \/>\n    }<\/p>\n<p>    \/\/ &#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061; \u6700\u7ec8\u901a\u77e5 After&#xff08;\u7c7b\u4f3cfinally&#xff0c;\u65e0\u8bba\u6210\u529f\u5931\u8d25\u90fd\u6267\u884c&#xff09; &#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;&#061;<br \/>\n    &#064;After(&#034;logPointcut(operateLog)&#034;)<br \/>\n    public void after(JoinPoint joinPoint, OperateLog operateLog) {<br \/>\n        System.out.println(&#034;&#061;&#061;&#061;&#061; &#064;After \u6267\u884c&#xff1a;\u64cd\u4f5c\u3010&#034;&#043;operateLog.value()&#043;&#034;\u3011\u6d41\u7a0b\u7ed3\u675f&#xff08;finally&#xff09; &#061;&#061;&#061;&#061;&#034;);<br \/>\n    }<\/p>\n<p>    \/\/ \u6a21\u62df\u5165\u5e93<br \/>\n    private void saveLog(OperateLogDTO dto) {<br \/>\n        System.out.println(&#034;\u3010\u4fdd\u5b58\u64cd\u4f5c\u65e5\u5fd7\u5230DB\u3011&#034; &#043; JSON.toJSONString(dto));<br \/>\n    }<br \/>\n}<\/p>\n<h4>\u6b65\u9aa4 4&#xff1a;\u76ee\u6807\u4e1a\u52a1 Controller&#xff08;\u76ee\u6807 Target&#xff09;<\/h4>\n<p>&#064;RestController<br \/>\n&#064;RequestMapping(&#034;\/user&#034;)<br \/>\npublic class UserController {<\/p>\n<p>    \/\/ \u6253\u4e0a\u6ce8\u89e3&#xff0c;AOP\u5207\u9762\u4f1a\u62e6\u622a\u8fd9\u4e2a\u65b9\u6cd5<br \/>\n    &#064;OperateLog(value &#061; &#034;\u65b0\u589e\u7528\u6237&#034;)<br \/>\n    &#064;PostMapping(&#034;\/add&#034;)<br \/>\n    public String addUser(String username, Integer age){<br \/>\n        System.out.println(&#034;&#061;&#061;&#061;&#061;&#061; \u76ee\u6807\u65b9\u6cd5&#xff1a;\u65b0\u589e\u7528\u6237\u4e1a\u52a1\u903b\u8f91\u6267\u884c &#061;&#061;&#061;&#061;&#061;&#034;);<br \/>\n        \/\/ \u6a21\u62df\u4e1a\u52a1<br \/>\n        return &#034;\u7528\u6237\u65b0\u589e\u6210\u529f, name:&#034; &#043; username;<br \/>\n    }<\/p>\n<p>    \/\/ \u6a21\u62df\u5f02\u5e38\u573a\u666f<br \/>\n    &#064;OperateLog(value &#061; &#034;\u5220\u9664\u7528\u6237&#034;)<br \/>\n    &#064;PostMapping(&#034;\/delete&#034;)<br \/>\n    public String deleteUser(){<br \/>\n        System.out.println(&#034;&#061;&#061;&#061;&#061;&#061; \u76ee\u6807\u65b9\u6cd5&#xff1a;\u5220\u9664\u7528\u6237\u4e1a\u52a1&#xff0c;\u629b\u51fa\u5f02\u5e38 &#061;&#061;&#061;&#061;&#061;&#034;);<br \/>\n        throw new RuntimeException(&#034;\u7528\u6237\u4e0d\u5b58\u5728&#xff0c;\u5220\u9664\u5931\u8d25&#034;);<br \/>\n    }<br \/>\n}<\/p>\n<hr \/>\n<h4>\u573a\u666f 1&#xff1a;\u8c03\u7528 \/user\/add \u3010\u6b63\u5e38\u65e0\u5f02\u5e38\u3011<\/h4>\n<p>\u8f93\u51fa\u987a\u5e8f&#xff08;\u5bf9\u5e94 AOP \u901a\u77e5\u987a\u5e8f&#xff09;<\/p>\n<p>&#061;&#061;&#061;&#061; &#064;Before \u6267\u884c&#xff1a;\u51c6\u5907\u6267\u884c\u3010\u65b0\u589e\u7528\u6237\u3011\u64cd\u4f5c &#061;&#061;&#061;&#061;<\/p>\n<p>&#061;&#061;&#061;&#061;&#061; \u76ee\u6807\u65b9\u6cd5&#xff1a;\u65b0\u589e\u7528\u6237\u4e1a\u52a1\u903b\u8f91\u6267\u884c &#061;&#061;&#061;&#061;&#061;<\/p>\n<p>&#061;&#061;&#061;&#061; &#064;AfterReturning \u6267\u884c&#xff1a;\u64cd\u4f5c\u3010\u65b0\u589e\u7528\u6237\u3011\u6210\u529f&#xff0c;\u8fd4\u56de\u503c&#xff1a;\u7528\u6237\u65b0\u589e\u6210\u529f, name:zhangsan<\/p>\n<p>&#061;&#061;&#061;&#061; &#064;After \u6267\u884c&#xff1a;\u64cd\u4f5c\u3010\u65b0\u589e\u7528\u6237\u3011\u6d41\u7a0b\u7ed3\u675f&#xff08;finally&#xff09; &#061;&#061;&#061;&#061;<br \/>\n\u3010\u4fdd\u5b58\u64cd\u4f5c\u65e5\u5fd7\u5230DB\u3011{&#034;username&#034;:&#034;admin&#034;,&#034;operateDesc&#034;:&#034;\u65b0\u589e\u7528\u6237&#034;,&#034;params&#034;:<br \/>\n[&#034;zhangsan&#034;,20],&#034;result&#034;:&#034;\u7528\u6237\u65b0\u589e\u6210\u529f, name:zhangsan&#034;,&#034;costTime&#034;:5,&#034;success&#034;:true,&#034;errorMsg&#034;:null}<\/p>\n<p>\u5b8c\u6574\u6267\u884c\u94fe\u8def&#xff1a;<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0&#064;Around \u524d\u7f6e\u4ee3\u7801 \u2192 &#064;Before \u2192 \u76ee\u6807\u65b9\u6cd5 \u2192 &#064;AfterReturning \u2192 &#064;After \u2192 Around \u4e2d proceed \u6267\u884c\u5b8c\u6bd5&#xff0c;\u8fdb\u5165 finally \u4fdd\u5b58\u65e5\u5fd7<\/p>\n<p>\u573a\u666f 2&#xff1a;\u8c03\u7528 \/user\/delete \u3010\u629b\u51fa\u5f02\u5e38\u3011<\/p>\n<p>&#061;&#061;&#061;&#061; &#064;Before \u6267\u884c&#xff1a;\u51c6\u5907\u6267\u884c\u3010\u5220\u9664\u7528\u6237\u3011\u64cd\u4f5c &#061;&#061;&#061;&#061;<\/p>\n<p>&#061;&#061;&#061;&#061;&#061; \u76ee\u6807\u65b9\u6cd5&#xff1a;\u5220\u9664\u7528\u6237\u4e1a\u52a1&#xff0c;\u629b\u51fa\u5f02\u5e38 &#061;&#061;&#061;&#061;&#061;<\/p>\n<p>&#061;&#061;&#061;&#061; &#064;AfterThrowing \u6267\u884c&#xff1a;\u64cd\u4f5c\u3010\u5220\u9664\u7528\u6237\u3011\u53d1\u751f\u5f02\u5e38&#xff1a;\u7528\u6237\u4e0d\u5b58\u5728&#xff0c;\u5220\u9664\u5931\u8d25<\/p>\n<p>&#061;&#061;&#061;&#061; &#064;After \u6267\u884c&#xff1a;\u64cd\u4f5c\u3010\u5220\u9664\u7528\u6237\u3011\u6d41\u7a0b\u7ed3\u675f&#xff08;finally&#xff09; &#061;&#061;&#061;&#061;<br \/>\n\u3010\u4fdd\u5b58\u64cd\u4f5c\u65e5\u5fd7\u5230DB\u3011{&#034;username&#034;:&#034;admin&#034;,&#034;operateDesc&#034;:&#034;\u5220\u9664\u7528\u6237&#034;,&#034;params&#034;:[],&#034;result&#034;:null,&#034;costTime&#034;:2,&#034;success&#034;:false,&#034;errorMsg&#034;:&#034;\u7528\u6237\u4e0d\u5b58\u5728&#xff0c;\u5220\u9664\u5931\u8d25&#034;}<\/p>\n<p>\u5f02\u5e38\u94fe\u8def&#xff1a;<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0&#064;Around \u524d\u7f6e \u2192 &#064;Before \u2192 \u76ee\u6807\u629b\u5f02\u5e38 \u2192 &#064;AfterThrowing \u2192 &#064;After \u2192 Around \u6355\u83b7\u5f02\u5e38&#xff0c;\u8bb0\u5f55\u9519\u8bef\u65e5\u5fd7<\/p>\n<p>\u2757\ufe0f\u91cd\u70b9&#xff1a;\u5f02\u5e38\u65f6 AfterReturning \u4e0d\u4f1a\u6267\u884c&#xff01;<\/p>\n<hr \/>\n<p>\u5bf9\u5e94 AOP \u6838\u5fc3\u540d\u8bcd\u5bf9\u7167<\/p>\n<table>\n<tr>AOP \u540d\u8bcd\u5728\u672c\u6848\u4f8b\u5bf9\u5e94\u7684\u4e1c\u897f<\/tr>\n<tbody>\n<tr>\n<td>Target \u76ee\u6807\u5bf9\u8c61<\/td>\n<td>UserController<\/td>\n<\/tr>\n<tr>\n<td>JoinPoint \u8fde\u63a5\u70b9<\/td>\n<td>addUser ()\u3001deleteUser () \u8fd9\u4e24\u4e2a\u65b9\u6cd5&#xff08;\u53ef\u88ab\u589e\u5f3a\u7684\u70b9\u4f4d&#xff09;<\/td>\n<\/tr>\n<tr>\n<td>Pointcut \u5207\u70b9<\/td>\n<td>&#064;annotation(OperateLog) \u5339\u914d\u89c4\u5219&#xff0c;\u7b5b\u9009\u54ea\u4e9b\u8fde\u63a5\u70b9\u8981\u62e6\u622a<\/td>\n<\/tr>\n<tr>\n<td>Advice \u901a\u77e5<\/td>\n<td>&#064;Before \/ &#064;Around \/ &#064;AfterReturning \/ &#064;AfterThrowing \/ &#064;After \u8fd9 5 \u6bb5\u589e\u5f3a\u4ee3\u7801<\/td>\n<\/tr>\n<tr>\n<td>Aspect \u5207\u9762<\/td>\n<td>OperateLogAspect \u6574\u4e2a\u7c7b&#xff08;\u5207\u70b9 &#043; \u6240\u6709\u901a\u77e5\u7ec4\u5408\u5728\u4e00\u8d77&#xff09;<\/td>\n<\/tr>\n<tr>\n<td>Weaving \u7ec7\u5165<\/td>\n<td>Spring \u542f\u52a8\u540e&#xff0c;\u8fd0\u884c\u671f\u521b\u5efa UserController \u7684\u4ee3\u7406\u5bf9\u8c61&#xff0c;\u628a\u5207\u9762\u903b\u8f91\u5d4c\u5165\u5230\u76ee\u6807\u65b9\u6cd5\u7684\u8fc7\u7a0b<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h4>\u6982\u62ec<\/h4>\n<p>\u7528\u540e\u53f0\u64cd\u4f5c\u65e5\u5fd7\u8fd9\u4e2a\u573a\u666f\u7406\u89e3 AOP\u3002<\/p>\n<p>\u9700\u6c42\u662f\u7edf\u4e00\u8bb0\u5f55\u7ba1\u7406\u5458\u64cd\u4f5c&#xff0c;\u4e0d\u60f3\u5728\u6bcf\u4e2a\u63a5\u53e3\u5199\u65e5\u5fd7\u4ee3\u7801\u3002<\/p>\n<li>\u81ea\u5b9a\u4e49&#064;OperateLog\u6ce8\u89e3\u6807\u8bb0\u9700\u8981\u8bb0\u5f55\u65e5\u5fd7\u7684\u63a5\u53e3&#xff1b;<\/li>\n<li>\u5199\u5207\u9762\u7c7b&#xff0c;\u5207\u70b9\u4f7f\u7528&#064;annotation\u5339\u914d\u8be5\u6ce8\u89e3&#xff1b;<\/li>\n<li>\u901a\u8fc7\u591a\u79cd\u901a\u77e5\u5b9e\u73b0&#xff1a;Before \u6253\u5370\u51c6\u5907\u4fe1\u606f&#xff0c;Around \u7edf\u8ba1\u8017\u65f6\u3001\u6355\u83b7\u5f02\u5e38&#xff1b;AfterReturning \u5904\u7406\u6210\u529f\u573a\u666f&#xff1b;AfterThrowing \u6355\u83b7\u5f02\u5e38&#xff1b;After \u505a\u6536\u5c3e\u3002<\/li>\n<li>Spring \u8fd0\u884c\u671f\u52a8\u6001\u4ee3\u7406\u628a\u5207\u9762\u7ec7\u5165\u76ee\u6807 Controller \u65b9\u6cd5&#xff1b;\u4e1a\u52a1\u4ee3\u7801\u53ea\u5199\u4e1a\u52a1&#xff0c;\u65e5\u5fd7\u6a2a\u5207\u903b\u8f91\u5b8c\u5168\u62bd\u79bb&#xff0c;\u89e3\u8026\u3002<\/li>\n<hr \/>\n<h3>\u516b\u3001Spring AOP \u5e38\u89c1\u5751<\/h3>\n<h4>\u5751 1&#xff1a;\u540c\u7c7b\u65b9\u6cd5\u81ea\u8c03\u7528&#xff0c;AOP \u5931\u6548<\/h4>\n<p>&#064;Service<br \/>\npublic class UserService{<br \/>\n    public void test(){<br \/>\n        \/\/ this\u662f\u539f\u59cb\u5bf9\u8c61&#xff0c;\u4e0d\u662f\u4ee3\u7406\u5bf9\u8c61&#xff01;\u5207\u9762\u4e0d\u4f1a\u89e6\u53d1<br \/>\n        this.addUser(&#034;test&#034;);<br \/>\n    }<br \/>\n    public void addUser(String name){}<br \/>\n}<\/p>\n<p>\u89e3\u51b3&#xff1a;<\/p>\n<li>\u81ea\u5df1\u6ce8\u5165\u81ea\u5df1 &#064;Autowired UserService self;&#xff0c;\u7528self.addUser()<\/li>\n<li>\u901a\u8fc7 AopContext \u83b7\u53d6\u4ee3\u7406\u5bf9\u8c61 ((UserService) AopContext.currentProxy()).addUser()&#xff0c;\u9700\u8981\u5f00\u542f &#064;EnableAspectJAutoProxy(exposeProxy &#061; true)<\/li>\n<h4>\u5751 2&#xff1a;private \u65b9\u6cd5\u4e0d\u80fd\u88ab AOP \u589e\u5f3a<\/h4>\n<p>Spring AOP \u53ea\u80fd\u62e6\u622apublic \u65b9\u6cd5&#xff0c;private \u65b9\u6cd5\u4e0d\u4f1a\u88ab\u4ee3\u7406\u3002<\/p>\n<h4>\u5751 3&#xff1a;final \u7c7b \/final \u65b9\u6cd5\u65e0\u6cd5\u88ab CGLIB \u4ee3\u7406<\/h4>\n<p>CGLIB \u751f\u6210\u5b50\u7c7b&#xff0c;final \u4e0d\u80fd\u91cd\u5199&#xff0c;\u589e\u5f3a\u5931\u6548\u3002<\/p>\n<h4>\u5751 4&#xff1a;\u4e8b\u52a1\u6ce8\u89e3 &#064;Transactional \u653e\u5728\u975e public \u65b9\u6cd5\u5931\u6548<\/h4>\n<p>\u5e95\u5c42\u5c31\u662f AOP&#xff0c;\u53ea\u80fd\u62e6\u622a public \u65b9\u6cd5\u3002<\/p>\n<h4>\u5751 5&#xff1a;\u591a\u4e2a\u5207\u9762\u987a\u5e8f\u6df7\u6dc6<\/h4>\n<p>\u8bb0\u4f4f &#064;Order&#xff1a;\u6570\u5b57\u8d8a\u5c0f&#xff0c;\u5207\u9762\u8d8a\u5148\u8fdb\u5165&#xff0c;\u6700\u540e\u9000\u51fa\u3002<\/p>\n<hr \/>\n<h2>Spring IOC \u8be6\u7ec6\u8bb2\u89e3<\/h2>\n<h3>\u4e00\u3001IOC \u662f\u4ec0\u4e48<\/h3>\n<p>IOC&#xff1a;Inversion of Control&#xff0c;\u63a7\u5236\u53cd\u8f6c<\/p>\n<p>\u662f\u4e00\u79cd\u8bbe\u8ba1\u601d\u60f3&#xff0c;\u4e0d\u662f Spring \u72ec\u6709\u7684&#xff0c;Spring \u662f IOC \u601d\u60f3\u7684\u5b9e\u73b0\u6846\u67b6\u3002<\/p>\n<p>\u4f20\u7edf\u5f00\u53d1&#xff1a;\u5f00\u53d1\u8005 new \u5bf9\u8c61&#xff0c;\u4e3b\u52a8\u521b\u5efa\u3001\u7ba1\u7406\u5bf9\u8c61\u7684\u4f9d\u8d56&#xff08;\u63a7\u5236\u6743\u5728\u7a0b\u5e8f\u5458\u624b\u4e0a&#xff09;<\/p>\n<\/p>\n<p>IOC&#xff1a;\u628a\u5bf9\u8c61\u521b\u5efa\u3001\u9500\u6bc1\u3001\u4f9d\u8d56\u6ce8\u5165\u7684\u63a7\u5236\u6743\u4ea4\u7ed9\u5bb9\u5668&#xff0c;\u7a0b\u5e8f\u5458\u4e0d\u518d\u624b\u52a8 new \u5bf9\u8c61&#xff1b;\u7531\u5bb9\u5668\u6765\u5b9e\u4f8b\u5316\u5bf9\u8c61\u3001\u88c5\u914d\u4f9d\u8d56\u3002<\/p>\n<p>\u63a7\u5236\u6743\u4ece\u4ee3\u7801\u53cd\u8f6c\u5230\u5bb9\u5668&#xff0c;\u6240\u4ee5\u53eb\u63a7\u5236\u53cd\u8f6c\u3002<\/p>\n<p>DI&#xff08;Dependency Injection&#xff0c;\u4f9d\u8d56\u6ce8\u5165&#xff09;&#xff1a;\u662f IOC \u601d\u60f3\u6700\u4e3b\u6d41\u7684\u5b9e\u73b0\u65b9\u5f0f\u3002<\/p>\n<p>IOC \u662f\u601d\u60f3&#xff0c;DI \u662f\u624b\u6bb5\u3002<\/p>\n<p><span style=\"color:#1a439c\">\u5bb9\u5668\u5728\u521b\u5efa Bean \u7684\u65f6\u5019&#xff0c;\u81ea\u52a8\u628a\u5bf9\u8c61\u9700\u8981\u7684\u4f9d\u8d56\u6ce8\u5165\u8fdb\u53bb\u3002&#xff08;\u770b\u8fd9\u4e2a\u5177\u4f53\u7684\u4e1a\u52a1\u573a\u666f\u4f8b\u5b50&#xff09;<\/span><\/p>\n<p>\u4e00\u53e5\u8bdd\u7406\u89e3\u539f\u53e5&#xff1a;<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u7a0b\u5e8f\u5458\u4e0d\u518d\u81ea\u5df1new A()\u3001new B()&#xff0c;\u4e0d\u518d\u624b\u52a8\u7ed9 A \u8bbe\u7f6e B&#xff1b;<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0Spring \u5bb9\u5668\u8d1f\u8d23 new Bean&#xff0c;\u521b\u5efa A \u8fd9\u4e2a Bean \u7684\u540c\u65f6&#xff0c;\u81ea\u52a8\u627e\u5230 A \u9700\u8981\u7684 B&#xff0c;\u628a B \u585e\u7ed9 A&#xff0c;\u8fd9\u4e2a\u52a8\u4f5c\u5c31\u662f DI\u3002<\/p>\n<p><span style=\"color:#b95514\">\u4e1a\u52a1\u573a\u666f<\/span><\/p>\n<p>\u7535\u5546\u4e0b\u5355\u573a\u666f&#xff1a; OrderService&#xff08;\u4e0b\u5355&#xff09;\u9700\u8981\u8c03\u7528 PayService&#xff08;\u652f\u4ed8&#xff09;\u3002 <span style=\"color:#1a439c\">OrderService \u4f9d\u8d56 PayService<\/span>\u3002<\/p>\n<\/p>\n<p>Step1&#xff1a;\u4e24\u4e2aService\u4ea4\u7ed9 Spring \u7ba1\u7406&#xff0c;\u53d8\u6210 Bean<\/p>\n<p> &#064;Service<br \/>\npublic class PayService {<br \/>\n    public boolean pay(Long orderId, BigDecimal money){<br \/>\n        System.out.println(&#034;\u53d1\u8d77\u652f\u4ed8&#034;);<br \/>\n        return true;<br \/>\n    }<br \/>\n}<\/p>\n<p> &#064;Service<br \/>\npublic class OrderService {<br \/>\n    private final PayService payService;<\/p>\n<p>    \/\/ \u2705 \u6784\u9020\u5668\u6ce8\u5165&#xff08;\u63a8\u8350&#xff09;<br \/>\n    \/\/ \u3010\u91cd\u70b9\u3011\u5bb9\u5668\u521b\u5efaOrderService Bean\u7684\u65f6\u5019&#xff0c;\u4f1a\u8c03\u7528\u8fd9\u4e2a\u6784\u9020\u65b9\u6cd5<br \/>\n    \/\/ \u5bb9\u5668\u4ece\u5bb9\u5668\u5185\u90e8\u627e\u5230PayService Bean&#xff0c;\u81ea\u52a8\u4f20\u8fdb\u6765<br \/>\n    public OrderService(PayService payService) {<br \/>\n        this.payService &#061; payService;<br \/>\n    }<\/p>\n<p>    public void createOrder(Long orderId, BigDecimal money){<br \/>\n        System.out.println(&#034;\u521b\u5efa\u8ba2\u5355&#034;);<br \/>\n        payService.pay(orderId, money);<br \/>\n    }<br \/>\n}<\/p>\n<p>Step2&#xff1a;Spring \u5bb9\u5668\u5185\u90e8\u6267\u884c\u6d41\u7a0b&#xff08;\u5bf9\u5e94\u90a3\u53e5\u8bdd&#xff09;<\/p>\n<li>Spring \u542f\u52a8&#xff0c;\u626b\u63cf&#064;Service&#xff0c;\u8bc6\u522bPayService\u3001OrderService\u4e24\u4e2a\u7c7b&#xff0c;\u51c6\u5907\u521b\u5efa Bean\u3002<\/li>\n<li>\u5148\u5b9e\u4f8b\u5316PayService&#xff0c;\u5b58\u5165\u5bb9\u5668\u7684 Bean \u6c60\u3002<\/li>\n<li>\u5f00\u59cb\u521b\u5efa OrderService \u8fd9\u4e2a Bean&#xff1a; \u5bb9\u5668\u53d1\u73b0OrderService\u6784\u9020\u51fd\u6570\u9700\u8981PayService\u7c7b\u578b\u53c2\u6570&#xff1b; \u5bb9\u5668\u53bb Bean \u6c60\u4e2d\u67e5\u627e&#xff0c;\u62ff\u5230\u5df2\u7ecf\u5efa\u597d\u7684PayService\u5b9e\u4f8b&#xff1b; \u81ea\u52a8\u4f20\u5165\u6784\u9020\u5668&#xff0c;\u5b8c\u6210\u4f9d\u8d56\u6ce8\u5165\u3002<\/li>\n<li>\u5b8c\u6210 Bean \u521b\u5efa\u3002\u4e1a\u52a1\u4ee3\u7801\u91cc\u6ca1\u6709\u4efb\u4f55 new&#xff0c;\u6ca1\u6709\u624b\u52a8\u8d4b\u503c\u3002<\/li>\n<p> &#064;SpringBootApplication<br \/>\npublic class IoCDemoApplication {<br \/>\n    public static void main(String[] args) {<br \/>\n        ApplicationContext ctx &#061; SpringApplication.run(IoCDemoApplication.class, args);<br \/>\n        \/\/ \u76f4\u63a5\u4ece\u5bb9\u5668\u62ffOrderService&#xff0c;\u4f9d\u8d56\u5df2\u7ecf\u6ce8\u5165\u5b8c\u6210<br \/>\n        OrderService orderService &#061; ctx.getBean(OrderService.class);<br \/>\n        orderService.createOrder(1L, new BigDecimal(&#034;99&#034;));<br \/>\n    }<br \/>\n}<\/p>\n<p>\u8f93\u51fa&#xff1a;<\/p>\n<p> \u521b\u5efa\u8ba2\u5355<br \/>\n\u53d1\u8d77\u652f\u4ed8<\/p>\n<p>\u539f\u53e5\u7ffb\u8bd1\u5230\u672c\u6848\u4f8b&#xff1a;<\/p>\n<p>\u5bb9\u5668\u5728\u521b\u5efa OrderService Bean \u7684\u65f6\u5019&#xff0c;\u81ea\u52a8\u628a\u5b83\u9700\u8981\u7684\u4f9d\u8d56 PayService \u6ce8\u5165\u8fdb\u53bb\u3002<\/p>\n<p>\u603b\u7ed3&#xff1a;<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0IOC &#061; \u628a\u5bf9\u8c61\u751f\u547d\u5468\u671f\u548c\u4f9d\u8d56\u4ea4\u7ed9 Spring \u5bb9\u5668\u7ba1\u7406&#xff1b;<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0DI &#061; \u5bb9\u5668\u81ea\u52a8\u7ed9 Bean \u6ce8\u5165\u4f9d\u8d56\u5c5e\u6027\u3002<\/p>\n<p>\u5173\u952e\u8bcd\u62c6\u89e3&#xff1a;<\/p>\n<li>Bean&#xff1a;\u4ea4\u7ed9 Spring \u5bb9\u5668\u7ba1\u7406\u7684\u5bf9\u8c61&#xff08;\u52a0&#064;Service\/&#064;Component\/&#064;Controller\u7b49&#xff09;<\/li>\n<li>\u4f9d\u8d56&#xff1a;A \u7c7b\u91cc\u9762\u8981\u7528\u5230 B \u7c7b&#xff0c;B \u5c31\u662f A \u7684\u4f9d\u8d56<\/li>\n<li>\u4f9d\u8d56\u6ce8\u5165 DI&#xff1a;\u4e0d\u662f A \u81ea\u5df1 new B&#xff0c;\u800c\u662f\u5bb9\u5668\u628a B \u9001\u7ed9 A<\/li>\n<li>\u65f6\u673a&#xff1a;\u521b\u5efa Bean \u7684\u65f6\u5019&#xff08;\u5b9e\u4f8b\u5316 Bean \u9636\u6bb5&#xff09; \u5c31\u5b8c\u6210\u6ce8\u5165<\/li>\n<h3>\u4e8c\u3001\u4e3a\u4ec0\u4e48\u8981\u7528 IOC&#xff08;\u89e3\u51b3\u4ec0\u4e48\u95ee\u9898&#xff09;<\/h3>\n<li>\u89e3\u8026&#xff1a;\u4e0d\u7528\u786c\u7f16\u7801 new XXXService()&#xff0c;\u7c7b\u4e4b\u95f4\u4e0d\u76f4\u63a5\u4f9d\u8d56\u5177\u4f53\u5b9e\u73b0&#xff0c;\u4f9d\u8d56\u62bd\u8c61&#xff0c;\u65b9\u4fbf\u66ff\u6362\u5b9e\u73b0\u7c7b<\/li>\n<li>\u7edf\u4e00\u7ba1\u7406\u5bf9\u8c61\u751f\u547d\u5468\u671f&#xff1a;\u521b\u5efa\u3001\u521d\u59cb\u5316\u3001\u9500\u6bc1\u7531\u5bb9\u5668\u63a5\u7ba1<\/li>\n<li>\u65b9\u4fbf\u4f9d\u8d56\u6ce8\u5165&#xff0c;\u4e0d\u7528\u624b\u52a8\u7ec4\u88c5\u5bf9\u8c61<\/li>\n<li>\u4fbf\u4e8e AOP\u3001\u4e8b\u52a1\u7b49\u529f\u80fd\u6269\u5c55&#xff08;Spring AOP \u5e95\u5c42\u4f9d\u8d56 IOC \u5bb9\u5668&#xff0c;\u751f\u6210\u4ee3\u7406 Bean&#xff09;<\/li>\n<li>\u65b9\u4fbf\u5355\u5143\u6d4b\u8bd5&#xff0c;\u53ef\u4ee5\u8f7b\u677e mock \u4f9d\u8d56<\/li>\n<p>\u53cd\u4f8b&#xff08;\u4f20\u7edf\u5199\u6cd5&#xff0c;\u7d27\u8026\u5408&#xff09;<\/p>\n<p>public class UserController {<br \/>\n    \/\/ \u786c\u7f16\u7801new&#xff0c;\u8026\u5408\u4e25\u91cd&#xff0c;\u66ff\u6362\u5b9e\u73b0\u8981\u6539\u4ee3\u7801<br \/>\n    private UserService userService &#061; new UserServiceImpl();<br \/>\n}<\/p>\n<p>IOC \u5199\u6cd5&#xff1a;<\/p>\n<p>public class UserController {<br \/>\n    \/\/ \u5bb9\u5668\u81ea\u52a8\u6ce8\u5165&#xff0c;\u4e0d\u9700\u8981new<br \/>\n    &#064;Autowired<br \/>\n    private UserService userService;<br \/>\n}<\/p>\n<h3>\u4e09\u3001IOC \u5bb9\u5668\u6838\u5fc3\u7ec4\u4ef6<\/h3>\n<p>Spring IOC \u5bb9\u5668\u9876\u5c42\u63a5\u53e3\u6709\u4e24\u4e2a&#xff1a;<\/p>\n<li>BeanFactory&#xff1a;\u6700\u5e95\u5c42\u5bb9\u5668\u63a5\u53e3&#xff0c;IOC \u5bb9\u5668\u7684\u6839\u63a5\u53e3\u3002\u63d0\u4f9b\u57fa\u7840 Bean \u83b7\u53d6\u80fd\u529b&#xff0c;\u61d2\u52a0\u8f7d&#xff08;getBean \u7684\u65f6\u5019\u624d\u5b9e\u4f8b\u5316 Bean&#xff09;&#xff0c;\u529f\u80fd\u7b80\u5355\u3002\n<ul>\n<li>\u65b9\u6cd5&#xff1a;getBean()\u3001containsBean()\u3001isSingleton()\u7b49<\/li>\n<\/ul>\n<\/li>\n<li>ApplicationContext&#xff1a;BeanFactory \u7684\u5b50\u63a5\u53e3&#xff0c;\u5e94\u7528\u4e0a\u4e0b\u6587&#xff0c;\u65e5\u5e38\u5f00\u53d1\u51e0\u4e4e\u90fd\u7528\u5b83\u3002\n<ul>\n<li>\u5728 BeanFactory \u57fa\u7840\u4e0a\u6269\u5c55&#xff1a;\n<ul>\n<li>\u5bb9\u5668\u542f\u52a8\u65f6\u9884\u5b9e\u4f8b\u5316\u5355\u4f8b Bean&#xff08;\u997f\u6c49&#xff09;<\/li>\n<li>\u56fd\u9645\u5316\u3001\u8d44\u6e90\u52a0\u8f7d\u3001\u4e8b\u4ef6\u53d1\u5e03\u3001\u73af\u5883\u53d8\u91cf\u3001AOP \u652f\u6301<\/li>\n<\/ul>\n<\/li>\n<li>\u5e38\u7528\u5b9e\u73b0\u7c7b&#xff1a;\n<ul>\n<li>ClassPathXmlApplicationContext&#xff1a;\u4ece classpath \u8bfb\u53d6 xml \u914d\u7f6e<\/li>\n<li>FileSystemXmlApplicationContext&#xff1a;\u8bfb\u53d6\u6587\u4ef6\u7cfb\u7edf xml<\/li>\n<li>AnnotationConfigApplicationContext&#xff1a;\u6ce8\u89e3\u9a71\u52a8&#xff08;SpringBoot \u9ed8\u8ba4&#xff0c;\u8bfb\u53d6&#064;Configuration&#xff09;<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<p>\u533a\u5206&#xff1a;<\/p>\n<p>BeanFactory \u662f\u7b80\u964b\u7248 IOC&#xff1b;<\/p>\n<p>ApplicationContext \u662f\u589e\u5f3a\u7248 BeanFactory\u3002<\/p>\n<h3>\u56db\u3001Bean&#xff1a;IOC \u5bb9\u5668\u7ba1\u7406\u7684\u5bf9\u8c61<\/h3>\n<p>\u5bb9\u5668\u4e2d\u7ba1\u7406\u7684\u6240\u6709\u5bf9\u8c61\u53eb\u505a Bean\u3002<\/p>\n<p>Bean \u7684\u5b9a\u4e49\u4fe1\u606f\u5b58\u5728 BeanDefinition&#xff08;Bean \u5b9a\u4e49&#xff09;&#xff1a;\u4fdd\u5b58 Bean \u7684 class \u7c7b\u578b\u3001\u4f5c\u7528\u57df\u3001\u61d2\u52a0\u8f7d\u3001\u521d\u59cb\u5316\u65b9\u6cd5\u3001\u9500\u6bc1\u65b9\u6cd5\u3001\u4f9d\u8d56\u4fe1\u606f\u7b49\u3002<\/p>\n<p>BeanDefinition \u2260 Bean<\/p>\n<p>\u5b9e\u4f8b BeanDefinition \u662f Bean \u7684\u5143\u6570\u636e\u63cf\u8ff0&#xff1b;<\/p>\n<p>\u5bb9\u5668\u6839\u636e BeanDefinition \u53bb\u5b9e\u4f8b\u5316 Bean \u5bf9\u8c61\u3002<\/p>\n<h4>Bean \u7684\u4f5c\u7528\u57df scope<\/h4>\n<li>singleton&#xff08;\u9ed8\u8ba4&#xff09;&#xff1a;\u5355\u4f8b\u3002\u6574\u4e2a\u5bb9\u5668\u4e2d\u53ea\u521b\u5efa 1 \u4e2a\u5b9e\u4f8b&#xff0c;\u5bb9\u5668\u542f\u52a8\u65f6&#xff08;\u975e\u61d2\u52a0\u8f7d&#xff09;\u5c31\u521b\u5efa&#xff0c;\u5b58\u5165\u5355\u4f8b\u6c60&#xff08;Map&lt;String,Object&gt; singletonObjects&#xff09;\u3002<\/li>\n<p>\u26a0\ufe0f \u5355\u4f8b Bean \u4e0d\u662f\u7ebf\u7a0b\u5b89\u5168&#xff01;\u591a\u7ebf\u7a0b\u5e76\u53d1\u4fee\u6539\u6210\u5458\u53d8\u91cf\u4f1a\u6709\u5e76\u53d1\u95ee\u9898\u3002<\/p>\n<li>prototype&#xff1a;\u539f\u578b\u3002\u6bcf\u6b21getBean()\u90fd\u4f1a\u65b0\u5efa\u4e00\u4e2a\u5bf9\u8c61&#xff0c;\u5bb9\u5668\u53ea\u8d1f\u8d23\u521b\u5efa&#xff0c;\u4e0d\u7ba1\u7406\u9500\u6bc1\u3002<\/li>\n<li>request&#xff1a;web \u73af\u5883&#xff0c;\u4e00\u6b21 http \u8bf7\u6c42\u4e00\u4e2a\u5b9e\u4f8b<\/li>\n<li>session&#xff1a;web \u73af\u5883&#xff0c;\u4e00\u6b21 session \u4e00\u4e2a\u5b9e\u4f8b<\/li>\n<li>application&#xff1a;web \u73af\u5883&#xff0c;ServletContext \u7ea7\u522b\u5355\u4f8b<\/li>\n<p>\u6269\u5c55&#xff1a;<\/p>\n<\/p>\n<p>Q&#xff1a;&#064;Autowired private UserService userService; \u8fd9\u4e2a UserService \u662f\u5168\u5c40\u552f\u4e00\u7684\u5417&#xff1f;<\/p>\n<\/p>\n<p>A&#xff1a;<\/p>\n<p>\u662f&#xff0c;\u56e0\u4e3aSpring Bean\u9ed8\u8ba4\u662f\u5168\u5c40\u552f\u4e00&#xff0c;\u56e0\u4e3a Spring Bean \u9ed8\u8ba4\u662f singleton&#xff08;\u5355\u4f8b&#xff09;\u3002<\/p>\n<p>&#064;Service \u6ce8\u89e3\u7684 Bean \u9ed8\u8ba4 &#064;Scope(&#034;singleton&#034;)<\/p>\n<ul>\n<li>Spring \u5bb9\u5668\u542f\u52a8\u65f6&#xff0c;\u53ea\u5b9e\u4f8b\u5316\u8fd9\u4e00\u4e2a UserServiceImpl \u5bf9\u8c61<\/li>\n<li>\u6240\u6709\u5730\u65b9&#064;Autowired UserService\u6ce8\u5165\u7684&#xff0c;\u62ff\u5230\u540c\u4e00\u4e2a\u5bf9\u8c61\u5f15\u7528\u3002<\/li>\n<\/ul>\n<p>Q&#xff1a;\u8fd9\u6837\u7684\u597d\u5904\u662f&#xff1f;<\/p>\n<p>A&#xff1a;<\/p>\n<p><span style=\"color:#b95514\">1. \u51cf\u5c11\u5bf9\u8c61\u521b\u5efa\u3001\u9500\u6bc1\u5f00\u9500&#xff0c;\u63d0\u5347\u6027\u80fd<\/span><\/p>\n<p>\u5bb9\u5668\u542f\u52a8\u9636\u6bb5\u4e00\u6b21\u6027 new \u4e00\u6b21 UserServiceImpl&#xff1b; Controller\u3001\u5176\u4ed6 Service \u591a\u5904&#064;Autowired\u6ce8\u5165\u65f6&#xff0c;\u4e0d\u518d\u91cd\u590d new \u5bf9\u8c61&#xff0c;\u76f4\u63a5\u8fd4\u56de\u540c\u4e00\u4e2a\u5bf9\u8c61\u5f15\u7528\u3002<\/p>\n<p>\u5982\u679c\u662f prototype&#xff0c;\u6bcf\u6b21\u6ce8\u5165\u90fd new&#xff0c;\u9891\u7e41\u521b\u5efa\u5bf9\u8c61\u3001\u89e6\u53d1 GC&#xff0c;\u589e\u52a0 CPU \u4e0e GC \u538b\u529b\u3002<\/p>\n<\/p>\n<p><span style=\"color:#ad720d\">2. \u8282\u7701\u5806\u5185\u5b58<\/span><\/p>\n<p>\u6574\u4e2a\u5bb9\u5668\u53ea\u4fdd\u7559\u4e00\u4efd UserServiceImpl \u5b9e\u4f8b\u3002<\/p>\n<p>\u9879\u76ee\u91cc\u5927\u91cf Service\u3001Controller\u3001Dao \u90fd\u590d\u7528\u8fd9\u4e00\u4e2a\u5bf9\u8c61&#xff0c;\u51cf\u5c11\u5806\u4e2d\u5bf9\u8c61\u6570\u91cf\u3002<\/p>\n<\/p>\n<p><span style=\"color:#ad720d\">3. \u4e00\u6b21\u6027\u5b8c\u6210 Bean \u6240\u6709\u540e\u7f6e\u5904\u7406&#xff08;AOP \u7ec7\u5165\u3001\u4f9d\u8d56\u6ce8\u5165\u3001\u521d\u59cb\u5316&#xff09;<\/span><\/p>\n<p>\u53ea\u505a\u4e00\u6b21&#xff1a; \u5b9e\u4f8b\u5316 \u2192 \u586b\u5145\u4f9d\u8d56&#xff08;&#064;Autowired&#xff09; \u2192 \u521d\u59cb\u5316\u56de\u8c03 \u2192 AOP \u4ee3\u7406\u7ec7\u5165&#xff08;\u751f\u6210 Proxy&#xff09;<\/p>\n<p>\u91cd\u70b9&#xff01;\u4e8b\u52a1\u5207\u9762&#064;Transactional\u7684\u4ee3\u7406\u53ea\u751f\u6210\u4e00\u6b21\u3002<\/p>\n<p>\u5982\u679c\u662f prototype&#xff0c;\u6bcf\u6b21\u83b7\u53d6 Bean \u90fd\u8981\u91cd\u65b0\u505a\u4e00\u904d\u4ee3\u7406\u3001\u521d\u59cb\u5316&#xff0c;\u5f00\u9500\u5f88\u5927\u3002<\/p>\n<p><span style=\"color:#ad720d\">4. \u5bb9\u5668\u7f13\u5b58&#xff0c;\u83b7\u53d6 Bean \u901f\u5ea6\u5feb<\/span><\/p>\n<p>\u5b9e\u4f8b\u521b\u5efa\u5b8c\u6210\u540e\u653e\u5165\u5bb9\u5668\u5355\u4f8b\u6c60&#xff08;singletonObjects Map&#xff09;&#xff0c;\u540e\u7eed getBean \/ \u6ce8\u5165\u76f4\u63a5\u4ece Map \u62ff\u5f15\u7528&#xff0c;\u4e0d\u7528\u53cd\u5c04\u521b\u5efa\u5bf9\u8c61\u3002<\/p>\n<\/p>\n<p><span style=\"color:#ad720d\">5. \u9002\u5408\u65e0\u72b6\u6001\u4e1a\u52a1\u5bf9\u8c61&#xff08;Service \u5929\u7136\u6ee1\u8db3&#xff09;<\/span><\/p>\n<p>UserServiceImpl \u91cc\u9762\u4e00\u822c\u4e0d\u5b58\u653e\u53ef\u53d8\u6210\u5458\u53d8\u91cf&#xff0c;\u53ea\u6709\u4e1a\u52a1\u65b9\u6cd5&#xff1b;\u65b9\u6cd5\u5185\u5c40\u90e8\u53d8\u91cf\u5728\u6808\u4e0a&#xff0c;\u591a\u7ebf\u7a0b\u4e92\u4e0d\u5e72\u6270&#xff0c;\u5929\u7136\u65e0\u72b6\u6001&#xff0c;\u53ef\u4ee5\u5b89\u5168\u590d\u7528\u540c\u4e00\u4e2a\u5b9e\u4f8b\u3002<\/p>\n<\/p>\n<p><span style=\"color:#1c7892\">\u6982\u62ec&#xff1a;<\/span><\/p>\n<p>&#064;Service \u9ed8\u8ba4 singleton&#xff0c;\u5bb9\u5668\u542f\u52a8\u4ec5\u5b9e\u4f8b\u5316\u4e00\u6b21&#xff0c;\u6240\u6709\u6ce8\u5165\u590d\u7528\u540c\u4e00\u4e2a\u5bf9\u8c61\u3002<\/p>\n<p>\u51cf\u5c11\u5bf9\u8c61\u521b\u5efa GC \u5f00\u9500\u3001\u8282\u7701\u5806\u5185\u5b58\u3002<\/p>\n<p>\u4f9d\u8d56\u6ce8\u5165\u3001AOP \u4ee3\u7406\u3001\u521d\u59cb\u5316\u53ea\u6267\u884c\u4e00\u6b21&#xff0c;\u63d0\u5347\u6027\u80fd\u3002<\/p>\n<p>Service \u672c\u8eab\u65e0\u72b6\u6001&#xff0c;\u9002\u5408\u591a\u7ebf\u7a0b\u590d\u7528\u3002<\/p>\n<\/p>\n<p>&#xff08;\u5173\u4e8eGC\u5f00\u9500\u548c\u8282\u7701\u5806\u5185\u5b58&#xff0c;\u6211\u5199\u8fc7\u4e00\u7bc7\u535a\u5ba2\u8be6\u7ec6\u8bb2\u89e3\u4e86\u8fd9\u4e2a\u8fc7\u7a0b&#xff1a;\u4e00\u6587\u8bb2\u61c2JVM\u4e0e\u8c03\u4f18&#xff09;<\/p>\n<\/p>\n<p>Q&#xff1a;\u65e2\u7136\u590d\u7528\u540c\u4e00\u4e2a\u5bf9\u8c61&#xff0c;\u90a3\u591a\u7ebf\u7a0b\u5e76\u53d1\u8bbf\u95ee UserServiceImpl \u4f1a\u4e0d\u4f1a\u7ebf\u7a0b\u4e0d\u5b89\u5168&#xff1f;<\/p>\n<p>A&#xff1a;\u4e0d\u4f1a&#xff0c;\u524d\u63d0\u662f\u4e0d\u5b9a\u4e49\u53ef\u53d8\u6210\u5458\u53d8\u91cf\u3002<\/p>\n<p>Spring MVC \u6bcf\u4e2a\u8bf7\u6c42\u662f\u72ec\u7acb\u7ebf\u7a0b&#xff0c;\u8c03\u7528 service \u65b9\u6cd5\u65f6&#xff0c;\u65b9\u6cd5\u5185\u7684\u5c40\u90e8\u53d8\u91cf\u5b58\u653e\u5728\u7ebf\u7a0b\u6808&#xff0c;\u7ebf\u7a0b\u9694\u79bb&#xff1b;\u53ea\u8981\u4e0d\u5728 Service \u91cc\u5199\u53ef\u4fee\u6539\u7684\u6210\u5458\u5b57\u6bb5&#xff0c;\u5c31\u6ca1\u6709\u5e76\u53d1\u95ee\u9898\u3002<\/p>\n<\/p>\n<p>Q&#xff1a;\u4ec0\u4e48\u65f6\u5019\u4e0d\u80fd\u7528 singleton&#xff0c;\u8981\u6539\u6210 prototype&#xff1f;<\/p>\n<p>A&#xff1a;\u5f53 Bean \u9700\u8981\u4fdd\u5b58\u53ef\u53d8\u7684\u6210\u5458\u72b6\u6001&#xff0c;\u6bcf\u4e2a\u8c03\u7528\u8005\u5fc5\u987b\u62e5\u6709\u72ec\u7acb\u5bf9\u8c61&#xff0c;\u6bd4\u5982\u6709\u72b6\u6001\u7684\u4e1a\u52a1\u6a21\u578b\u5bf9\u8c61\u3002<\/p>\n<h3>\u4e94\u3001\u4f9d\u8d56\u6ce8\u5165 DI \u7684 3 \u79cd\u65b9\u5f0f<\/h3>\n<h4>1. \u6784\u9020\u5668\u6ce8\u5165&#xff08;Spring \u5b98\u65b9\u63a8\u8350&#xff09;<\/h4>\n<p>\u5bb9\u5668\u8c03\u7528\u7c7b\u7684\u6784\u9020\u65b9\u6cd5&#xff0c;\u628a\u4f9d\u8d56\u4f20\u5165\u3002<\/p>\n<p>&#064;Service<br \/>\npublic class UserController {<br \/>\n    private final UserService userService;<\/p>\n<p>    \/\/ \u6784\u9020\u6ce8\u5165&#xff0c;Spring4.3&#043;\u5355\u6784\u9020\u5668\u53ef\u7701\u7565&#064;Autowired<br \/>\n    public UserController(UserService userService) {<br \/>\n        this.userService &#061; userService;<br \/>\n    }<br \/>\n}<\/p>\n<p>\u2705 \u4f18\u70b9&#xff1a;<\/p>\n<ul>\n<li>\u4f9d\u8d56\u4e0d\u53ef\u53d8&#xff08;final \u4fee\u9970&#xff09;<\/li>\n<li>\u4fdd\u8bc1 Bean \u521b\u5efa\u5b8c\u6210\u65f6\u4f9d\u8d56\u5df2\u7ecf\u5c31\u7eea&#xff0c;\u4e0d\u4f1a\u51fa\u73b0\u7a7a\u6307\u9488<\/li>\n<li>\u5f3a\u5236\u4f9d\u8d56\u5fc5\u987b\u5b58\u5728&#xff0c;\u5bb9\u5668\u542f\u52a8\u5931\u8d25\u5feb\u901f\u66b4\u9732\u7f3a\u5931\u4f9d\u8d56<\/li>\n<li>\u907f\u514d\u5faa\u73af\u4f9d\u8d56&#xff08;\u90e8\u5206\u573a\u666f&#xff09;<\/li>\n<\/ul>\n<h4>2. Setter \u65b9\u6cd5\u6ce8\u5165<\/h4>\n<p>\u5bb9\u5668\u8c03\u7528 set \u65b9\u6cd5\u6ce8\u5165\u5c5e\u6027\u3002<\/p>\n<p>&#064;Service<br \/>\npublic class UserController {<br \/>\n    private UserService userService;<\/p>\n<p>    &#064;Autowired<br \/>\n    public void setUserService(UserService userService) {<br \/>\n        this.userService &#061; userService;<br \/>\n    }<br \/>\n}<\/p>\n<p>\u2705 \u9002\u5408&#xff1a;\u53ef\u9009\u4f9d\u8d56&#xff0c;\u53ef\u5728 Bean \u521b\u5efa\u540e\u4fee\u6539\u4f9d\u8d56\u3002<\/p>\n<h4>3. \u5b57\u6bb5\u6ce8\u5165&#xff08;&#064;Autowired \u76f4\u63a5\u6253\u5728\u5c5e\u6027\u4e0a&#xff09;<\/h4>\n<p>&#064;Service<br \/>\npublic class UserController {<br \/>\n    &#064;Autowired<br \/>\n    private UserService userService;<br \/>\n}<\/p>\n<p>\u274c \u7f3a\u70b9&#xff1a;<\/p>\n<ul>\n<li>\u65e0\u6cd5 final \u4fee\u9970&#xff0c;\u4f9d\u8d56\u53ef\u53d8<\/li>\n<li>\u65e0\u6cd5\u8131\u79bb\u5bb9\u5668\u505a\u5355\u5143\u6d4b\u8bd5&#xff08;new \u5bf9\u8c61\u65f6\u65e0\u6cd5\u6ce8\u5165&#xff09;<\/li>\n<li>\u9690\u85cf\u4f9d\u8d56&#xff0c;\u4ee3\u7801\u53ef\u8bfb\u6027\u5dee<\/li>\n<\/ul>\n<p>Spring \u5b98\u65b9\u4e0d\u63a8\u8350\u5b57\u6bb5\u6ce8\u5165&#xff0c;\u4f46\u662f\u4e1a\u52a1\u4ee3\u7801\u5199\u7684\u6700\u591a\u3002<\/p>\n<h4>\u6ce8\u5165\u6ce8\u89e3&#xff1a;&#064;Autowired\u3001&#064;Resource\u533a\u522b<\/h4>\n<li>&#064;Autowired&#xff1a;Spring \u6ce8\u89e3&#xff0c;\u9ed8\u8ba4\u6309\u7c7b\u578b byType&#xff0c;\u627e\u4e0d\u5230\u518d\u6309\u540d\u5b57 byName&#xff1b;\u53ef\u4ee5\u914d\u5408&#064;Qualifier\u6307\u5b9a Bean \u540d\u79f0&#xff1b;required&#061;true \u9ed8\u8ba4&#xff0c;\u627e\u4e0d\u5230 Bean \u629b\u5f02\u5e38\u3002<\/li>\n<li>&#064;Resource&#xff1a;Java \u6807\u51c6 JSR250&#xff0c;\u9ed8\u8ba4\u6309\u540d\u79f0 byName&#xff0c;\u540d\u5b57\u627e\u4e0d\u5230\u518d byType\u3002<\/li>\n<p>\u8fd9\u4e2a\u6211\u4e13\u95e8\u5199\u4e86\u4e00\u7bc7\u535a\u5ba2\u8be6\u89e3&#xff0c;\u975e\u5e38\u8be6\u7ec6&#xff1a;\u4e00\u6587\u770b\u61c2&#064;Autowired \u548c &#064;Resource\u533a\u522b&#xff08;\u9644\u4ee3\u7801&#xff09;<\/p>\n<h3>\u516d\u3001Bean \u5b8c\u6574\u751f\u547d\u5468\u671f<\/h3>\n<p>\u4ee5\u5355\u4f8b\u3001\u975e\u61d2\u52a0\u8f7d Bean\u4e3a\u4f8b&#xff1a;<\/p>\n<li>\u5b9e\u4f8b\u5316&#xff1a;\u5bb9\u5668\u6839\u636e BeanDefinition&#xff0c;\u901a\u8fc7\u53cd\u5c04 new \u5bf9\u8c61&#xff08;\u53ea\u662f\u5806\u91cc\u521b\u5efa\u7a7a\u5bf9\u8c61&#xff0c;\u5c5e\u6027\u8fd8\u6ca1\u8d4b\u503c&#xff09;<\/li>\n<li>\u5c5e\u6027\u586b\u5145&#xff08;populateBean&#xff09;&#xff1a;\u8fdb\u884c\u4f9d\u8d56\u6ce8\u5165&#xff0c;\u7ed9\u5bf9\u8c61\u5c5e\u6027\u8d4b\u503c&#xff08;&#064;Autowired \u6ce8\u5165\u5728\u8fd9\u91cc&#xff09;<\/li>\n<li>Aware \u63a5\u53e3\u56de\u8c03&#xff1a;\u5982\u679c Bean \u5b9e\u73b0\u4e86BeanNameAware\u3001BeanFactoryAware\u3001ApplicationContextAware&#xff0c;\u56de\u8c03\u65b9\u6cd5&#xff0c;\u8ba9 Bean \u62ff\u5230\u5bb9\u5668\u4fe1\u606f<\/li>\n<li>BeanPostProcessor \u524d\u7f6e\u5904\u7406&#xff1a;\u521d\u59cb\u5316\u524d\u7f6e\u540e\u7f6e\u5904\u7406\u5668&#xff0c;postProcessBeforeInitialization<\/li>\n<li>\u521d\u59cb\u5316 Initialization\n<ul>\n<li>&#064;PostConstruct \u6ce8\u89e3\u65b9\u6cd5&#xff08;JSR \u89c4\u8303&#xff09;<\/li>\n<li>InitializingBean#afterPropertiesSet() \u63a5\u53e3<\/li>\n<li>xml \u6307\u5b9a init-method \u81ea\u5b9a\u4e49\u521d\u59cb\u5316\u65b9\u6cd5<\/li>\n<\/ul>\n<\/li>\n<li>BeanPostProcessor \u540e\u7f6e\u5904\u7406&#xff1a;postProcessAfterInitialization\n<p>\u2728 AOP \u4ee3\u7406\u5bf9\u8c61\u5c31\u662f\u5728\u8fd9\u91cc\u751f\u6210\u3002<\/p>\n<p>\u5982\u679c\u9700\u8981 AOP \u589e\u5f3a&#xff0c;\u8fd4\u56de\u4ee3\u7406\u5bf9\u8c61&#xff0c;\u5426\u5219\u8fd4\u56de\u539f\u5bf9\u8c61\u3002<\/p>\n<\/li>\n<li>Bean \u5b8c\u6210&#xff1a;Bean \u5c31\u7eea&#xff0c;\u53ef\u4ee5\u4f7f\u7528&#xff0c;\u653e\u5165\u5355\u4f8b\u6c60<\/li>\n<li>\u5bb9\u5668\u5173\u95ed&#xff0c;\u9500\u6bc1\n<ul>\n<li>&#064;PreDestroy<\/li>\n<li>DisposableBean#destroy()<\/li>\n<li>xml \u6307\u5b9a destroy-method<\/li>\n<\/ul>\n<\/li>\n<p>\u603b\u7ed3&#xff1a;\u5b9e\u4f8b\u5316 \u2192 \u586b\u5145\u5c5e\u6027 \u2192 Aware \u2192 \u524d\u7f6e\u5904\u7406\u5668 \u2192 \u521d\u59cb\u5316 \u2192 \u540e\u7f6e\u5904\u7406\u5668 (AOP) \u2192 \u4f7f\u7528 \u2192 \u9500\u6bc1<\/p>\n<h3>\u4e03\u3001\u5faa\u73af\u4f9d\u8d56&#xff08;IOC \u7ecf\u5178\u96be\u70b9&#xff09;<\/h3>\n<h4>\u4ec0\u4e48\u662f\u5faa\u73af\u4f9d\u8d56<\/h4>\n<p>A \u91cc\u9762\u4f9d\u8d56 B&#xff0c;B \u91cc\u9762\u4f9d\u8d56 A\u3002<\/p>\n<p>&#064;Component<br \/>\npublic class A {<br \/>\n    &#064;Autowired<br \/>\n    private B b;<br \/>\n}<br \/>\n&#064;Component<br \/>\npublic class B {<br \/>\n    &#064;Autowired<br \/>\n    private A a;<br \/>\n}<\/p>\n<h4>Spring \u80fd\u89e3\u51b3\u54ea\u79cd\u5faa\u73af\u4f9d\u8d56&#xff1f;<\/h4>\n<p>\u2705 \u5355\u4f8b Bean &#043; setter \/ \u5b57\u6bb5\u6ce8\u5165&#xff1a;\u53ef\u4ee5\u89e3\u51b3<\/p>\n<p>\u274c \u6784\u9020\u5668\u6ce8\u5165\u7684\u5faa\u73af\u4f9d\u8d56&#xff1a;\u65e0\u6cd5\u89e3\u51b3&#xff0c;\u76f4\u63a5\u629b\u5f02\u5e38<\/p>\n<p>\u274c prototype \u539f\u578b Bean&#xff1a;\u5b8c\u5168\u65e0\u6cd5\u89e3\u51b3<\/p>\n<h4>\u4e09\u7ea7\u7f13\u5b58\u539f\u7406&#xff08;\u9762\u8bd5\u5fc5\u95ee&#xff09;<\/h4>\n<p>Spring \u5355\u4f8b\u6c60\u4e09\u7ea7\u7f13\u5b58&#xff1a;<\/p>\n<li>singletonObjects&#xff1a;\u4e00\u7ea7\u7f13\u5b58&#xff0c;\u5b8c\u6574\u5c31\u7eea Bean&#xff08;\u5b9e\u4f8b\u5316 &#043; \u5c5e\u6027\u586b\u5145 &#043; \u521d\u59cb\u5316\u5b8c\u6210&#xff09;<\/li>\n<li>earlySingletonObjects&#xff1a;\u4e8c\u7ea7\u7f13\u5b58&#xff0c;\u63d0\u524d\u66b4\u9732\u7684\u534a\u6210\u54c1 Bean&#xff08;\u5b9e\u4f8b\u5316\u5b8c\u6210&#xff0c;\u4f46\u8fd8\u6ca1\u586b\u5145\u5c5e\u6027\u3001\u521d\u59cb\u5316&#xff1b;\u53ef\u80fd\u662f\u4ee3\u7406\u5bf9\u8c61&#xff09;<\/li>\n<li>singletonFactories&#xff1a;\u4e09\u7ea7\u7f13\u5b58&#xff0c;\u5b58\u653e Bean \u5de5\u5382\u5bf9\u8c61 ObjectFactory\u3002\u53ea\u6709\u53d1\u751f\u5faa\u73af\u4f9d\u8d56\u65f6&#xff0c;\u624d\u4f1a\u8c03\u7528 ObjectFactory#getObject ()&#xff0c;\u6267\u884c getEarlyBeanReference&#xff0c;\u6309\u9700\u751f\u6210\u539f\u59cb Bean \u6216\u8005 AOP \u4ee3\u7406\u5bf9\u8c61&#xff0c;\u7528\u4e8e\u63d0\u524d\u66b4\u9732\u5f15\u7528\u3002<\/li>\n<p>\u89e3\u51b3\u6d41\u7a0b\u7b80\u8ff0&#xff1a;<\/p>\n<li>\u521b\u5efa A&#xff0c;\u5b9e\u4f8b\u5316 A&#xff0c;\u5f80\u4e09\u7ea7\u7f13\u5b58 singletonFactories \u5b58\u5165 A \u5bf9\u5e94\u7684 ObjectFactory&#xff1b;\u5f00\u59cb\u586b\u5145\u5c5e\u6027&#xff0c;\u53d1\u73b0\u9700\u8981\u4f9d\u8d56 B\u3002<\/li>\n<li>\u521b\u5efa B&#xff0c;\u5b9e\u4f8b\u5316 B&#xff0c;\u653e\u5165\u4e09\u7ea7\u7f13\u5b58&#xff1b;\u586b\u5145\u5c5e\u6027\u65f6\u53d1\u73b0\u9700\u8981\u4f9d\u8d56 A\u3002<\/li>\n<li>B \u53bb\u7f13\u5b58\u67e5\u627e A&#xff1a;\u4e00\u7ea7\u6ca1\u6709&#xff0c;\u4e8c\u7ea7\u6ca1\u6709&#xff0c;\u53d6\u51fa\u4e09\u7ea7\u7f13\u5b58\u4e2d A \u7684 ObjectFactory&#xff0c;\u8c03\u7528 getObject ()\u3002\n<ul>\n<li>\u82e5 A \u9700\u8981 AOP \u4ee3\u7406&#xff1a;\u5728\u8fd9\u91cc\u76f4\u63a5\u751f\u6210 A \u7684\u4ee3\u7406\u5bf9\u8c61&#xff1b;\u4e0d\u9700\u8981\u4ee3\u7406\u5219\u8fd4\u56de\u539f\u59cb\u5b9e\u4f8b\u3002<\/li>\n<li>\u5c06\u62ff\u5230\u7684\u5bf9\u8c61&#xff08;\u4ee3\u7406 \/ \u539f\u59cb&#xff09;\u5b58\u5165\u4e8c\u7ea7\u7f13\u5b58 earlySingletonObjects&#xff0c;\u540c\u65f6\u5220\u9664\u4e09\u7ea7\u7f13\u5b58\u91cc A \u7684 ObjectFactory\u3002<\/li>\n<\/ul>\n<\/li>\n<li>B \u62ff\u5230\u4e8c\u7ea7\u7f13\u5b58\u4e2d A \u7684\u5f15\u7528&#xff0c;B \u5b8c\u6210\u5c5e\u6027\u586b\u5145\u3001\u521d\u59cb\u5316&#xff0c;B \u653e\u5165\u4e00\u7ea7\u7f13\u5b58\u3002<\/li>\n<li>A \u62ff\u5230 B \u7684\u5f15\u7528&#xff0c;A \u7ee7\u7eed\u5b8c\u6210\u5c5e\u6027\u586b\u5145\u3001\u521d\u59cb\u5316\u3002\n<p>\u91cd\u70b9&#xff1a;A \u521d\u59cb\u5316\u9636\u6bb5&#xff0c;AOP \u540e\u7f6e\u5904\u7406\u5668\u4f1a\u5224\u65ad\u8be5 Bean \u5df2\u7ecf\u63d0\u524d\u751f\u6210\u8fc7\u4ee3\u7406&#xff08;earlyProxyReferences \u6807\u8bb0&#xff09;&#xff0c;\u4e0d\u518d\u91cd\u590d\u521b\u5efa\u4ee3\u7406\u3002<\/p>\n<\/li>\n<li>A \u521d\u59cb\u5316\u5b8c\u6210&#xff0c;\u628a\u4e8c\u7ea7\u7f13\u5b58\u91cc\u7684 A \u5bf9\u8c61&#xff08;\u4ee3\u7406 \/ \u539f\u59cb&#xff09;\u79fb\u5165\u4e00\u7ea7\u7f13\u5b58&#xff0c;\u6e05\u9664\u4e8c\u7ea7\u7f13\u5b58\u4e2d A \u7684\u8bb0\u5f55\u3002<\/li>\n<p>\u5173\u952e\u70b9&#xff1a;\u63d0\u524d\u66b4\u9732\u534a\u6210\u54c1 Bean \u7684\u5f15\u7528\u3002<\/p>\n<p>\u5982\u679c\u662f\u6784\u9020\u6ce8\u5165&#xff1a;\u5b9e\u4f8b\u5316\u548c\u5c5e\u6027\u6ce8\u5165\u5728\u6784\u9020\u65b9\u6cd5\u9636\u6bb5&#xff0c;\u5bf9\u8c61\u8fd8\u6ca1 new \u51fa\u6765&#xff0c;\u65e0\u6cd5\u63d0\u524d\u66b4\u9732\u5f15\u7528&#xff0c;\u6240\u4ee5\u65e0\u6cd5\u89e3\u51b3\u3002<\/p>\n<p>\u5f88\u591a\u4eba\u8bef\u533a&#xff1a;\u4e09\u7ea7\u7f13\u5b58\u5c31\u662f\u4e3a\u4e86\u89e3\u5faa\u73af\u4f9d\u8d56\u3002<\/p>\n<p>\u4e0d\u5b8c\u5168\u5bf9&#xff0c;\u4e09\u7ea7\u7f13\u5b58\u4e3b\u8981\u662f\u4e3a\u4e86\u5faa\u73af\u4f9d\u8d56\u4e0b\u540c\u65f6\u652f\u6301 AOP \u4ee3\u7406\u3002<\/p>\n<p>\u5982\u679c\u6ca1\u6709 AOP&#xff0c;\u4e8c\u7ea7\u7f13\u5b58\u5c31\u8db3\u591f\u3002<\/p>\n<h3>\u516b\u3001Bean \u7684\u914d\u7f6e\u65b9\u5f0f<\/h3>\n<li>\u6ce8\u89e3\u65b9\u5f0f&#xff1a;&#064;Component \/ &#064;Service \/ &#064;Controller \/ &#064;Repository&#xff0c;\u7ec4\u4ef6\u626b\u63cf&#064;ComponentScan<\/li>\n<li>JavaConfig&#xff08;SpringBoot \u4e3b\u6d41&#xff09;&#xff1a;&#064;Configuration &#043; &#064;Bean<\/li>\n<p>&#064;Configuration<br \/>\npublic class MyConfig {<br \/>\n    &#064;Bean<br \/>\n    public UserService userService(){<br \/>\n        return new UserServiceImpl();<br \/>\n    }<br \/>\n}<\/p>\n<h3>\u4e5d\u3001IOC \u5bb9\u5668\u542f\u52a8\u5927\u81f4\u6d41\u7a0b&#xff08;ApplicationContext&#xff09;<\/h3>\n<li>\u52a0\u8f7d\u914d\u7f6e&#xff08;\u6ce8\u89e3 \/ XML&#xff09;&#xff0c;\u626b\u63cf\u6240\u6709\u7c7b<\/li>\n<li>\u89e3\u6790\u5f97\u5230\u6240\u6709 BeanDefinition&#xff0c;\u5b58\u5165 BeanDefinitionMap<\/li>\n<li>\u904d\u5386 BeanDefinition&#xff0c;\u5b9e\u4f8b\u5316\u6240\u6709\u975e\u61d2\u52a0\u8f7d\u5355\u4f8b Bean&#xff08;\u89e6\u53d1 Bean \u751f\u547d\u5468\u671f&#xff09;<\/li>\n<li>\u5b9e\u4f8b\u5316\u8fc7\u7a0b\u4e2d\u81ea\u52a8\u505a\u4f9d\u8d56\u6ce8\u5165&#xff0c;\u5904\u7406\u5faa\u73af\u4f9d\u8d56<\/li>\n<li>\u5168\u90e8 Bean \u521b\u5efa\u5b8c\u6210&#xff0c;\u5bb9\u5668\u542f\u52a8\u5b8c\u6bd5&#xff0c;\u53d1\u5e03\u5bb9\u5668\u5237\u65b0\u5b8c\u6210\u4e8b\u4ef6<\/li>\n<h3>\u5341\u3001IOC \u9ad8\u9891\u9762\u8bd5\u9898\u6c47\u603b<\/h3>\n<li>IOC \u548c DI \u7684\u5173\u7cfb&#xff1f;<\/li>\n<p>IOC \u662f\u63a7\u5236\u53cd\u8f6c\u601d\u60f3&#xff1b;<\/p>\n<p>DI \u4f9d\u8d56\u6ce8\u5165\u662f IOC \u7684\u5b9e\u73b0\u65b9\u5f0f\u3002<\/p>\n<li>BeanFactory \u548c ApplicationContext \u533a\u522b&#xff1f;<\/li>\n<p>BeanFactory \u61d2\u52a0\u8f7d&#xff0c;\u57fa\u7840 Bean \u83b7\u53d6&#xff1b;<\/p>\n<p>ApplicationContext \u662f\u5b50\u63a5\u53e3&#xff0c;\u542f\u52a8\u9884\u5b9e\u4f8b\u5355\u4f8b Bean&#xff0c;\u6269\u5c55\u4e8b\u4ef6\u3001\u56fd\u9645\u5316\u7b49\u80fd\u529b\u3002<\/p>\n<li>singleton \u5355\u4f8b Bean \u4ec0\u4e48\u65f6\u5019\u521b\u5efa&#xff1f;<\/li>\n<p>\u9ed8\u8ba4\u5bb9\u5668\u5237\u65b0\u65f6\u63d0\u524d\u5b9e\u4f8b\u5316&#xff1b;\u52a0\u4e0a&#064;Lazy\u61d2\u52a0\u8f7d&#xff0c;\u7b2c\u4e00\u6b21 getBean \u624d\u521b\u5efa\u3002<\/p>\n<li>\u4e09\u79cd\u6ce8\u5165\u65b9\u5f0f\u4f18\u7f3a\u70b9&#xff1f;\u63a8\u8350\u54ea\u4e2a&#xff1f;<\/li>\n<p>\u6784\u9020\u6ce8\u5165\u5b98\u65b9\u63a8\u8350&#xff0c;\u4fdd\u8bc1\u4f9d\u8d56\u5fc5\u586b&#xff1b;setter \u9002\u5408\u53ef\u9009\u4f9d\u8d56&#xff1b;\u5b57\u6bb5\u6ce8\u5165\u53ef\u8bfb\u6027\u5dee&#xff0c;\u4e0d\u63a8\u8350\u3002<\/p>\n<li>\u5faa\u73af\u4f9d\u8d56 Spring \u600e\u4e48\u89e3\u51b3&#xff1f;\u4e09\u7ea7\u7f13\u5b58\u5206\u522b\u4f5c\u7528&#xff1f;\u54ea\u4e9b\u573a\u666f\u65e0\u6cd5\u89e3\u51b3\u5faa\u73af\u4f9d\u8d56&#xff1f;<\/li>\n<li>BeanPostProcessor \u4f5c\u7528&#xff1f;<\/li>\n<p>Bean \u540e\u7f6e\u5904\u7406\u5668&#xff0c;\u5728 Bean \u521d\u59cb\u5316\u524d\u540e\u62e6\u622a&#xff1b;AOP \u4ee3\u7406\u751f\u6210\u5c31\u5728postProcessAfterInitialization\u3002<\/p>\n<li>&#064;Component \u548c &#064;Bean \u533a\u522b&#xff1f;<\/li>\n<p>&#064;Component \u6253\u5728\u7c7b\u4e0a&#xff0c;\u7ec4\u4ef6\u626b\u63cf\u81ea\u52a8\u521b\u5efa Bean&#xff1b;<\/p>\n<p>&#064;Bean \u6253\u5728\u65b9\u6cd5\u4e0a&#xff0c;\u624b\u52a8\u5b9e\u4f8b\u5316\u5bf9\u8c61\u653e\u5230\u5bb9\u5668&#xff0c;\u9002\u5408\u5f15\u5165\u7b2c\u4e09\u65b9\u7c7b\u3002<\/p>\n<hr \/>\n<h2>IOC \u548c AOP \u7684\u5173\u7cfb\u603b\u7ed3<\/h2>\n<p>IOC \u662f Spring \u7684\u6839\u57fa&#xff0c;AOP \u5efa\u7acb\u5728 IOC \u4e4b\u4e0a<\/p>\n<li>IOC \u5bb9\u5668\u8d1f\u8d23\u521b\u5efa\u6240\u6709 Bean&#xff0c;\u7ba1\u7406 Bean \u751f\u547d\u5468\u671f&#xff1b;<\/li>\n<li>AOP \u7684\u4ee3\u7406\u5bf9\u8c61&#xff0c;\u662f\u5728 Bean \u751f\u547d\u5468\u671f\u7684BeanPostProcessor\u540e\u7f6e\u5904\u7406\u9636\u6bb5\u751f\u6210&#xff1b;<\/li>\n<li>\u5982\u679c\u6ca1\u6709 IOC&#xff0c;Spring \u65e0\u6cd5\u81ea\u52a8\u7ba1\u7406\u76ee\u6807 Bean \u548c\u5207\u9762 Bean&#xff0c;AOP \u65e0\u6cd5\u81ea\u52a8\u751f\u6548\u3002<\/li>\n<p>\u7b80\u5355\u7406\u89e3&#xff1a;IOC \u8d1f\u8d23\u9020\u5bf9\u8c61&#xff0c;AOP \u8d1f\u8d23\u7ed9\u5bf9\u8c61\u5957\u4ee3\u7406\u589e\u5f3a\u3002<\/p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Spring AOP \u8be6\u7ec6\u8bb2\u89e3<br \/>\n\u4e00\u3001AOP \u662f\u4ec0\u4e48<br \/>\nAOP&#xff1a;Aspect Oriented Programming&#xff0c;\u9762\u5411\u5207\u9762\u7f16\u7a0b\u3002 AOP \u7684\u6838\u5fc3\u539f\u7406\u5c31\u662f\u52a8\u6001\u751f\u6210\u4ee3\u7406\u5bf9\u8c61&#xff0c;\u5728\u4e0d\u6539\u52a8\u539f\u4e1a\u52a1\u4ee3\u7801\u7684\u60c5\u51b5\u4e0b&#xff0c;\u901a\u8fc7\u4ee3\u7406\u53bb\u62e6\u622a\u76ee\u6807\u65b9\u6cd5&#xff0c;\u5728\u65b9\u6cd5\u524d\u540e\u63d2\u5165\u5207\u9762\u903b\u8f91\u3002 \u6838\u5fc3\u601d\u60f3&#xff1a;\u4e1a\u52a1\u4ee3\u7801\u53ea\u5173\u5fc3\u4e1a\u52a1&#xff0c;\u516c\u5171\u903b\u8f91\u4ea4\u7ed9\u5207\u9762\u3002 \u5178\u578b\u901a\u7528\u573a\u666f&#xff1a; \u65e5\u5fd7\u8bb0\u5f55\u3001\u6027\u80fd\u7edf\u8ba1\u3001\u6743\u9650\u6821\u9a8c\u3001\u4e8b\u52a1\u63a7\u5236\u3001\u5f02\u5e38\u5904\u7406<\/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":[11820,12899,4887,5220,99,101],"topic":[],"class_list":["post-113358","post","type-post","status-publish","format-standard","hentry","category-server","tag-atomgit","tag-12899","tag-aop","tag-ioc","tag-java","tag-spring"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>\u4e00\u6587\u770b\u61c2Spring\u4e4bAOP\u4e0eIOC(\u4ee3\u7801+\u573a\u666f\uff0c\u901a\u4fd7\u6613\u61c2) - \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\/113358.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"\u4e00\u6587\u770b\u61c2Spring\u4e4bAOP\u4e0eIOC(\u4ee3\u7801+\u573a\u666f\uff0c\u901a\u4fd7\u6613\u61c2) - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"Spring AOP \u8be6\u7ec6\u8bb2\u89e3 \u4e00\u3001AOP \u662f\u4ec0\u4e48 AOP&#xff1a;Aspect Oriented Programming&#xff0c;\u9762\u5411\u5207\u9762\u7f16\u7a0b\u3002 AOP \u7684\u6838\u5fc3\u539f\u7406\u5c31\u662f\u52a8\u6001\u751f\u6210\u4ee3\u7406\u5bf9\u8c61&#xff0c;\u5728\u4e0d\u6539\u52a8\u539f\u4e1a\u52a1\u4ee3\u7801\u7684\u60c5\u51b5\u4e0b&#xff0c;\u901a\u8fc7\u4ee3\u7406\u53bb\u62e6\u622a\u76ee\u6807\u65b9\u6cd5&#xff0c;\u5728\u65b9\u6cd5\u524d\u540e\u63d2\u5165\u5207\u9762\u903b\u8f91\u3002 \u6838\u5fc3\u601d\u60f3&#xff1a;\u4e1a\u52a1\u4ee3\u7801\u53ea\u5173\u5fc3\u4e1a\u52a1&#xff0c;\u516c\u5171\u903b\u8f91\u4ea4\u7ed9\u5207\u9762\u3002 \u5178\u578b\u901a\u7528\u573a\u666f&#xff1a; \u65e5\u5fd7\u8bb0\u5f55\u3001\u6027\u80fd\u7edf\u8ba1\u3001\u6743\u9650\u6821\u9a8c\u3001\u4e8b\u52a1\u63a7\u5236\u3001\u5f02\u5e38\u5904\u7406\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/113358.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2026-10-05T22:16:51+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=\"15 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/113358.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/113358.html\",\"name\":\"\u4e00\u6587\u770b\u61c2Spring\u4e4bAOP\u4e0eIOC(\u4ee3\u7801+\u573a\u666f\uff0c\u901a\u4fd7\u6613\u61c2) - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2026-10-05T22:16:51+00:00\",\"dateModified\":\"2026-10-05T22:16:51+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/113358.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/113358.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/113358.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"\u4e00\u6587\u770b\u61c2Spring\u4e4bAOP\u4e0eIOC(\u4ee3\u7801+\u573a\u666f\uff0c\u901a\u4fd7\u6613\u61c2)\"}]},{\"@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":"\u4e00\u6587\u770b\u61c2Spring\u4e4bAOP\u4e0eIOC(\u4ee3\u7801+\u573a\u666f\uff0c\u901a\u4fd7\u6613\u61c2) - \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\/113358.html","og_locale":"zh_CN","og_type":"article","og_title":"\u4e00\u6587\u770b\u61c2Spring\u4e4bAOP\u4e0eIOC(\u4ee3\u7801+\u573a\u666f\uff0c\u901a\u4fd7\u6613\u61c2) - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"Spring AOP \u8be6\u7ec6\u8bb2\u89e3 \u4e00\u3001AOP \u662f\u4ec0\u4e48 AOP&#xff1a;Aspect Oriented Programming&#xff0c;\u9762\u5411\u5207\u9762\u7f16\u7a0b\u3002 AOP \u7684\u6838\u5fc3\u539f\u7406\u5c31\u662f\u52a8\u6001\u751f\u6210\u4ee3\u7406\u5bf9\u8c61&#xff0c;\u5728\u4e0d\u6539\u52a8\u539f\u4e1a\u52a1\u4ee3\u7801\u7684\u60c5\u51b5\u4e0b&#xff0c;\u901a\u8fc7\u4ee3\u7406\u53bb\u62e6\u622a\u76ee\u6807\u65b9\u6cd5&#xff0c;\u5728\u65b9\u6cd5\u524d\u540e\u63d2\u5165\u5207\u9762\u903b\u8f91\u3002 \u6838\u5fc3\u601d\u60f3&#xff1a;\u4e1a\u52a1\u4ee3\u7801\u53ea\u5173\u5fc3\u4e1a\u52a1&#xff0c;\u516c\u5171\u903b\u8f91\u4ea4\u7ed9\u5207\u9762\u3002 \u5178\u578b\u901a\u7528\u573a\u666f&#xff1a; \u65e5\u5fd7\u8bb0\u5f55\u3001\u6027\u80fd\u7edf\u8ba1\u3001\u6743\u9650\u6821\u9a8c\u3001\u4e8b\u52a1\u63a7\u5236\u3001\u5f02\u5e38\u5904\u7406","og_url":"https:\/\/www.wsisp.com\/helps\/113358.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2026-10-05T22:16:51+00:00","author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"15 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/113358.html","url":"https:\/\/www.wsisp.com\/helps\/113358.html","name":"\u4e00\u6587\u770b\u61c2Spring\u4e4bAOP\u4e0eIOC(\u4ee3\u7801+\u573a\u666f\uff0c\u901a\u4fd7\u6613\u61c2) - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2026-10-05T22:16:51+00:00","dateModified":"2026-10-05T22:16:51+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/113358.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/113358.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/113358.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"\u4e00\u6587\u770b\u61c2Spring\u4e4bAOP\u4e0eIOC(\u4ee3\u7801+\u573a\u666f\uff0c\u901a\u4fd7\u6613\u61c2)"}]},{"@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\/113358","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=113358"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/113358\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=113358"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=113358"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=113358"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=113358"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}