{"id":93099,"date":"2026-08-11T23:10:31","date_gmt":"2026-08-11T15:10:31","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/93099.html"},"modified":"2026-08-11T23:10:31","modified_gmt":"2026-08-11T15:10:31","slug":"spring-aop-%e8%b6%85%e5%85%a8%e8%af%a6%e8%a7%a3%ef%bc%9a%e9%9d%a2%e5%90%91%e5%88%87%e9%9d%a2%e7%bc%96%e7%a8%8b-%e4%ba%94%e5%a4%a7%e9%80%9a%e7%9f%a5%e7%b1%bb%e5%9e%8b-%e5%88%87%e5%85%a5%e7%82%b9","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/93099.html","title":{"rendered":"Spring AOP \u8d85\u5168\u8be6\u89e3\uff1a\u9762\u5411\u5207\u9762\u7f16\u7a0b + \u4e94\u5927\u901a\u77e5\u7c7b\u578b + \u5207\u5165\u70b9\u8868\u8fbe\u5f0f + ThreadLocal \u5b9e\u6218"},"content":{"rendered":"<h3>\u4e00\u3001AOP \u57fa\u7840\u6982\u5ff5<\/h3>\n<h4>1. \u4ec0\u4e48\u662f AOP<\/h4>\n<p>AOP&#xff08;Aspect Oriented Programming&#xff09;\u5373\u9762\u5411\u5207\u9762\u7f16\u7a0b&#xff0c;\u6838\u5fc3\u601d\u60f3&#xff1a;\u4e0d\u4fee\u6539\u539f\u6709\u4e1a\u52a1\u4ee3\u7801&#xff0c;\u5bf9\u6307\u5b9a\u65b9\u6cd5\u8fdb\u884c\u529f\u80fd\u589e\u5f3a\u3002 AOP \u662f\u4e00\u79cd\u7f16\u7a0b\u601d\u60f3&#xff0c;Spring AOP \u662f\u8be5\u601d\u60f3\u5728 Spring \u6846\u67b6\u4e2d\u7684\u843d\u5730\u5b9e\u73b0&#xff0c;\u5e95\u5c42\u57fa\u4e8e\u52a8\u6001\u4ee3\u7406\u6280\u672f\u3002<\/p>\n<h4>2. \u6838\u5fc3\u4f18\u52bf<\/h4>\n<ul>\n<li>\u51cf\u5c11\u91cd\u590d\u4ee3\u7801&#xff0c;\u901a\u7528\u903b\u8f91\u7edf\u4e00\u7ef4\u62a4<\/li>\n<li>\u5bf9\u4e1a\u52a1\u4ee3\u7801\u96f6\u4fb5\u5165&#xff0c;\u4e0d\u5f71\u54cd\u539f\u6709\u4e1a\u52a1\u903b\u8f91<\/li>\n<li>\u63d0\u5347\u5f00\u53d1\u6548\u7387&#xff0c;\u964d\u4f4e\u540e\u671f\u7ef4\u62a4\u6210\u672c<\/li>\n<\/ul>\n<h4>3. \u5178\u578b\u5e94\u7528\u573a\u666f<\/h4>\n<ul>\n<li>\u7edf\u8ba1\u4e1a\u52a1\u65b9\u6cd5\u6267\u884c\u8017\u65f6<\/li>\n<li>\u7cfb\u7edf\u64cd\u4f5c\u65e5\u5fd7\u8bb0\u5f55<\/li>\n<li>\u58f0\u660e\u5f0f\u4e8b\u52a1\u7ba1\u7406&#xff08;&#064;Transactional\u5e95\u5c42\u5b9e\u73b0&#xff09;<\/li>\n<li>\u63a5\u53e3\u6743\u9650\u7edf\u4e00\u6821\u9a8c<\/li>\n<\/ul>\n<hr \/>\n<h3>\u4e8c\u3001SpringAOP \u5feb\u901f\u5165\u95e8&#xff1a;\u65b9\u6cd5\u8017\u65f6\u7edf\u8ba1<\/h3>\n<h4>\u5b9e\u73b0\u6b65\u9aa4<\/h4>\n<li>pom.xml \u5f15\u5165 AOP \u573a\u666f\u4f9d\u8d56<\/li>\n<li>\u5b9a\u4e49\u5207\u9762\u7c7b&#xff0c;\u6dfb\u52a0&#064;Aspect &#043; &#064;Component\u6ce8\u89e3<\/li>\n<li>\u7f16\u5199\u901a\u77e5\u65b9\u6cd5&#xff0c;\u914d\u7f6e\u5207\u5165\u70b9\u8868\u8fbe\u5f0f&#xff0c;\u5b9e\u73b0\u589e\u5f3a\u903b\u8f91<\/li>\n<h4>\u5b8c\u6574\u4ee3\u7801\u793a\u4f8b<\/h4>\n<h5>1&#xff09;\u5f15\u5165\u4f9d\u8d56<\/h5>\n<p>&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<p>2&#xff09;\u5207\u9762\u7c7b\u5b9e\u73b0<\/p>\n<p>&#064;aspect<br \/>\n&#064;component<br \/>\npublic class recordtimeaspect {<\/p>\n<p>    \/\/ \u73af\u7ed5\u901a\u77e5 &#043; \u5207\u5165\u70b9\u8868\u8fbe\u5f0f&#xff1a;\u5339\u914dservice.impl\u4e0b\u6240\u6709\u7c7b\u7684\u6240\u6709\u65b9\u6cd5<br \/>\n    &#064;around(&#034;execution(* com.itheima.service.impl.*.*(..))&#034;)<br \/>\n    public object recordtime(proceedingjoinpoint pjp) throws throwable {<br \/>\n        \/\/ 1. \u8bb0\u5f55\u65b9\u6cd5\u5f00\u59cb\u65f6\u95f4<br \/>\n        long begin &#061; system.currenttimemillis();<br \/>\n        \/\/ 2. \u6267\u884c\u539f\u59cb\u76ee\u6807\u65b9\u6cd5<br \/>\n        object result &#061; pjp.proceed();<br \/>\n        \/\/ 3. \u8ba1\u7b97\u5e76\u6253\u5370\u8017\u65f6<br \/>\n        long end &#061; system.currenttimemillis();<br \/>\n        system.out.println(&#034;\u65b9\u6cd5\u6267\u884c\u8017\u65f6&#xff1a;&#034; &#043; (end &#8211; begin) &#043; &#034;ms&#034;);<br \/>\n        \/\/ \u8fd4\u56de\u65b9\u6cd5\u6267\u884c\u7ed3\u679c<br \/>\n        return result;<br \/>\n    }<br \/>\n}<\/p>\n<p>\u4e09\u3001AOP \u4e94\u5927\u6838\u5fc3\u6982\u5ff5<\/p>\n<table>\n<tr>\u6838\u5fc3\u6982\u5ff5\u8bf4\u660e<\/tr>\n<tbody>\n<tr>\n<td>\u8fde\u63a5\u70b9 JoinPoint<\/td>\n<td>\u6240\u6709\u53ef\u4ee5\u88ab AOP \u589e\u5f3a\u7684\u65b9\u6cd5&#xff08;\u5019\u9009\u65b9\u6cd5\u96c6\u5408&#xff09;<\/td>\n<\/tr>\n<tr>\n<td>\u901a\u77e5 Advice<\/td>\n<td>\u989d\u5916\u589e\u5f3a\u7684\u4e1a\u52a1\u903b\u8f91&#xff08;\u6bd4\u5982\u7edf\u8ba1\u8017\u65f6\u3001\u8bb0\u5f55\u65e5\u5fd7\u7684\u4ee3\u7801&#xff09;<\/td>\n<\/tr>\n<tr>\n<td>\u5207\u5165\u70b9 PointCut<\/td>\n<td>\u5339\u914d\u89c4\u5219&#xff0c;\u7b5b\u9009\u51fa\u771f\u6b63\u9700\u8981\u589e\u5f3a\u7684\u8fde\u63a5\u70b9<\/td>\n<\/tr>\n<tr>\n<td>\u5207\u9762 Aspect<\/td>\n<td>\u5207\u9762 &#061; \u901a\u77e5 &#043; \u5207\u5165\u70b9&#xff0c;\u5c01\u88c5\u589e\u5f3a\u903b\u8f91\u4e0e\u5339\u914d\u89c4\u5219<\/td>\n<\/tr>\n<tr>\n<td>\u76ee\u6807\u5bf9\u8c61 Target<\/td>\n<td>\u88ab\u589e\u5f3a\u529f\u80fd\u7684\u539f\u59cb\u4e1a\u52a1\u5bf9\u8c61<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u5e95\u5c42\u539f\u7406&#xff1a;Spring \u57fa\u4e8e\u52a8\u6001\u4ee3\u7406\u81ea\u52a8\u751f\u6210\u4ee3\u7406\u5bf9\u8c61&#xff0c;\u66ff\u6362\u539f\u59cb\u5bf9\u8c61\u6ce8\u5165 IOC \u5bb9\u5668&#xff1b;\u65b9\u6cd5\u8c03\u7528\u65f6\u5b9e\u9645\u6267\u884c\u4ee3\u7406\u5bf9\u8c61&#xff0c;\u5148\u6267\u884c\u589e\u5f3a\u903b\u8f91&#xff0c;\u518d\u8c03\u7528\u539f\u65b9\u6cd5\u3002<\/p>\n<h3>\u56db\u3001\u4e94\u5927\u901a\u77e5\u7c7b\u578b\u8be6\u89e3<\/h3>\n<p>\u6839\u636e\u6267\u884c\u65f6\u673a\u4e0d\u540c&#xff0c;\u901a\u77e5\u5206\u4e3a\u4e94\u7c7b&#xff0c;\u5176\u4e2d\u73af\u7ed5\u901a\u77e5\u529f\u80fd\u6700\u5f3a\u5927&#xff1a;<\/p>\n<table>\n<tr>\u6ce8\u89e3\u6267\u884c\u65f6\u673a\u6838\u5fc3\u7279\u70b9<\/tr>\n<tbody>\n<tr>\n<td>&#064;Around \u73af\u7ed5\u901a\u77e5<\/td>\n<td>\u76ee\u6807\u65b9\u6cd5\u6267\u884c\u524d\u540e\u90fd\u53ef\u6267\u884c<\/td>\n<td>\u529f\u80fd\u6700\u5f3a&#xff0c;\u9700\u624b\u52a8\u8c03\u7528proceed()\u89e6\u53d1\u539f\u65b9\u6cd5&#xff0c;\u5fc5\u987b\u8fd4\u56de\u7ed3\u679c<\/td>\n<\/tr>\n<tr>\n<td>&#064;Before \u524d\u7f6e\u901a\u77e5<\/td>\n<td>\u76ee\u6807\u65b9\u6cd5\u6267\u884c\u4e4b\u524d\u8fd0\u884c<\/td>\n<td>\u65e0\u6cd5\u963b\u6b62\u65b9\u6cd5\u6267\u884c&#xff0c;\u4ec5\u505a\u524d\u7f6e\u51c6\u5907<\/td>\n<\/tr>\n<tr>\n<td>&#064;After \u540e\u7f6e\u901a\u77e5<\/td>\n<td>\u76ee\u6807\u65b9\u6cd5\u6267\u884c\u5b8c\u6bd5\u540e\u8fd0\u884c<\/td>\n<td>\u65e0\u8bba\u65b9\u6cd5\u6b63\u5e38\u7ed3\u675f\u8fd8\u662f\u629b\u51fa\u5f02\u5e38&#xff0c;\u90fd\u4f1a\u6267\u884c<\/td>\n<\/tr>\n<tr>\n<td>&#064;AfterReturning \u8fd4\u56de\u540e\u901a\u77e5<\/td>\n<td>\u65b9\u6cd5\u6b63\u5e38\u8fd4\u56de\u7ed3\u679c\u540e\u8fd0\u884c<\/td>\n<td>\u65b9\u6cd5\u629b\u51fa\u5f02\u5e38\u5219\u4e0d\u6267\u884c&#xff0c;\u53ef\u83b7\u53d6\u65b9\u6cd5\u8fd4\u56de\u503c<\/td>\n<\/tr>\n<tr>\n<td>&#064;AfterThrowing \u5f02\u5e38\u901a\u77e5<\/td>\n<td>\u65b9\u6cd5\u629b\u51fa\u5f02\u5e38\u540e\u8fd0\u884c<\/td>\n<td>\u4ec5\u5f02\u5e38\u573a\u666f\u89e6\u53d1&#xff0c;\u53ef\u6355\u83b7\u5f02\u5e38\u4fe1\u606f<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u26a0\ufe0f \u5173\u952e\u6ce8\u610f\u4e8b\u9879&#xff1a;<\/p>\n<ul>\n<li>&#064;Around \u5fc5\u987b\u624b\u52a8\u8c03\u7528 pjp.proceed()&#xff0c;\u5426\u5219\u539f\u59cb\u65b9\u6cd5\u4e0d\u4f1a\u6267\u884c<\/li>\n<li>&#064;Around \u5fc5\u987b\u8fd4\u56deObject\u7c7b\u578b\u7ed3\u679c&#xff0c;\u5426\u5219\u63a5\u53e3\u65e0\u6cd5\u83b7\u53d6\u8fd4\u56de\u6570\u636e<\/li>\n<\/ul>\n<h3>\u4e94\u3001\u5207\u5165\u70b9\u8868\u8fbe\u5f0f<\/h3>\n<h4>1. execution \u8868\u8fbe\u5f0f&#xff08;\u6700\u5e38\u7528&#xff09;<\/h4>\n<p>\u6839\u636e\u5305\u540d\u3001\u7c7b\u540d\u3001\u65b9\u6cd5\u540d\u6279\u91cf\u5339\u914d\u65b9\u6cd5\u3002<\/p>\n<h5>\u8bed\u6cd5\u683c\u5f0f<\/h5>\n<p>execution(\u8fd4\u56de\u503c\u7c7b\u578b \u5305\u540d.\u7c7b\u540d.\u65b9\u6cd5\u540d(\u53c2\u6570\u7c7b\u578b))<\/p>\n<h5>\u901a\u914d\u7b26\u8bf4\u660e<\/h5>\n<ul>\n<li>*&#xff1a;\u5339\u914d\u4efb\u610f\u8fd4\u56de\u503c\u3001\u4efb\u610f\u7c7b\u3001\u4efb\u610f\u65b9\u6cd5\u540d<\/li>\n<li>..&#xff1a;\u5339\u914d\u4efb\u610f\u7c7b\u578b\u3001\u4efb\u610f\u4e2a\u6570\u7684\u53c2\u6570<\/li>\n<\/ul>\n<p>\/\/ \u5339\u914dservice.impl\u5305\u4e0b\u6240\u6709\u7c7b\u7684\u6240\u6709\u65b9\u6cd5<br \/>\nexecution(* com.itheima.service.impl.*.*(..))<\/p>\n<h4>2. &#064;annotation \u6ce8\u89e3\u5339\u914d<\/h4>\n<p>\u5339\u914d\u6807\u6ce8\u4e86\u6307\u5b9a\u81ea\u5b9a\u4e49\u6ce8\u89e3\u7684\u65b9\u6cd5&#xff0c;\u7c92\u5ea6\u66f4\u7075\u6d3b\u3002<\/p>\n<p>\/\/ \u5339\u914d\u6240\u6709\u52a0\u4e86&#064;Log\u6ce8\u89e3\u7684\u65b9\u6cd5<br \/>\n&#064;around(&#034;&#064;annotation(com.itheima.anno.log)&#034;)<\/p>\n<p>\u9002\u7528\u573a\u666f&#xff1a;\u96f6\u6563\u5206\u5e03\u7684\u589e\u5f3a\u9700\u6c42&#xff0c;\u6bd4\u5982\u6307\u5b9a\u90e8\u5206\u63a5\u53e3\u8bb0\u5f55\u64cd\u4f5c\u65e5\u5fd7\u3002<\/p>\n<h3>\u516d\u3001&#064;Pointcut \u5207\u5165\u70b9\u62bd\u53d6<\/h3>\n<h4>\u4f5c\u7528<\/h4>\n<p>\u5c06\u91cd\u590d\u7684\u5207\u5165\u70b9\u8868\u8fbe\u5f0f\u62bd\u53d6\u4e3a\u72ec\u7acb\u65b9\u6cd5&#xff0c;\u5b9e\u73b0\u590d\u7528&#xff0c;\u7b80\u5316\u4ee3\u7801\u3002<\/p>\n<h4>\u4ee3\u7801\u793a\u4f8b<\/h4>\n<p>\/\/ \u62bd\u53d6\u516c\u5171\u5207\u5165\u70b9<br \/>\n&#064;pointcut(&#034;execution(* com.itheima.service.impl.*.*(..))&#034;)<br \/>\nprivate void pt(){}<\/p>\n<p>\/\/ \u76f4\u63a5\u5f15\u7528\u5207\u5165\u70b9\u65b9\u6cd5<br \/>\n&#064;around(&#034;pt()&#034;)<br \/>\npublic object recordtime(proceedingjoinpoint pjp) throws throwable {<br \/>\n    \/\/ \u589e\u5f3a\u903b\u8f91<br \/>\n}<\/p>\n<h4>\u8bbf\u95ee\u6743\u9650<\/h4>\n<ul>\n<li>private&#xff1a;\u4ec5\u5f53\u524d\u5207\u9762\u7c7b\u5185\u90e8\u53ef\u5f15\u7528<\/li>\n<li>public&#xff1a;\u5176\u4ed6\u5207\u9762\u7c7b\u4e5f\u53ef\u4ee5\u5f15\u7528\u8be5\u5207\u5165\u70b9<\/li>\n<\/ul>\n<hr \/>\n<h3>\u4e03\u3001JoinPoint \u8fde\u63a5\u70b9\u5bf9\u8c61<\/h3>\n<h4>\u4e24\u7c7b\u5bf9\u8c61\u533a\u522b<\/h4>\n<ul>\n<li>JoinPoint&#xff1a;\u9002\u7528\u4e8e\u524d\u7f6e\u3001\u540e\u7f6e\u7b49\u666e\u901a\u901a\u77e5&#xff0c;\u53ef\u83b7\u53d6\u65b9\u6cd5\u57fa\u672c\u4fe1\u606f<\/li>\n<li>ProceedingJoinPoint&#xff1a;\u4ec5\u9002\u7528\u4e8e&#064;Around\u73af\u7ed5\u901a\u77e5&#xff0c;\u7ee7\u627f\u81ea JoinPoint&#xff0c;\u989d\u5916\u652f\u6301proceed()\u6267\u884c\u539f\u65b9\u6cd5<\/li>\n<\/ul>\n<h4>\u5e38\u7528\u65b9\u6cd5<\/h4>\n<p>\/\/ \u83b7\u53d6\u76ee\u6807\u7c7b\u5bf9\u8c61<br \/>\nobject target &#061; joinpoint.gettarget();<br \/>\n\/\/ \u83b7\u53d6\u65b9\u6cd5\u7b7e\u540d\u3001\u65b9\u6cd5\u540d<br \/>\nstring methodname &#061; joinpoint.getsignature().getname();<br \/>\n\/\/ \u83b7\u53d6\u65b9\u6cd5\u5165\u53c2\u6570\u7ec4<br \/>\nobject[] args &#061; joinpoint.getargs();<\/p>\n<h3>\u516b\u3001ThreadLocal \u7ebf\u7a0b\u5c40\u90e8\u53d8\u91cf<\/h3>\n<h4>\u6838\u5fc3\u7279\u6027<\/h4>\n<p>ThreadLocal \u4e0d\u662f\u7ebf\u7a0b&#xff0c;\u800c\u662f\u7ebf\u7a0b\u7ea7\u522b\u7684\u5c40\u90e8\u5b58\u50a8\u53d8\u91cf&#xff0c;\u4e3a\u6bcf\u4e2a\u7ebf\u7a0b\u63d0\u4f9b\u72ec\u7acb\u7684\u5b58\u50a8\u7a7a\u95f4&#xff0c;\u7ebf\u7a0b\u4e4b\u95f4\u6570\u636e\u5b8c\u5168\u9694\u79bb&#xff0c;\u4e92\u4e0d\u5e72\u6270\u3002<\/p>\n<h4>\u6838\u5fc3 API<\/h4>\n<table>\n<tr>\u65b9\u6cd5\u4f5c\u7528<\/tr>\n<tbody>\n<tr>\n<td>set(object value)<\/td>\n<td>\u5411\u5f53\u524d\u7ebf\u7a0b\u5b58\u5165\u6570\u636e<\/td>\n<\/tr>\n<tr>\n<td>get()<\/td>\n<td>\u8bfb\u53d6\u5f53\u524d\u7ebf\u7a0b\u5b58\u50a8\u7684\u6570\u636e<\/td>\n<\/tr>\n<tr>\n<td>remove()<\/td>\n<td>\u79fb\u9664\u5f53\u524d\u7ebf\u7a0b\u7684\u6570\u636e<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h4>\u5178\u578b\u5b9e\u6218&#xff1a;\u767b\u5f55\u7528\u6237\u4fe1\u606f\u4f20\u9012<\/h4>\n<p>\u4e00\u6b21\u8bf7\u6c42\u5bf9\u5e94\u4e00\u4e2a\u7ebf\u7a0b&#xff0c;\u5728\u8fc7\u6ee4\u5668\u89e3\u6790\u767b\u5f55\u4fe1\u606f\u540e\u5b58\u5165 ThreadLocal&#xff0c;\u540e\u7eed Controller\u3001Service\u3001AOP \u5747\u53ef\u76f4\u63a5\u83b7\u53d6&#xff0c;\u65e0\u9700\u5c42\u5c42\u4f20\u53c2\u3002<\/p>\n<h5>\u5de5\u5177\u7c7b\u5c01\u88c5<\/h5>\n<p>public class currentholder {<br \/>\n    private static final threadlocal&lt;integer&gt; current_local &#061; new threadlocal&lt;&gt;();<\/p>\n<p>    public static void setcurrentid(integer empid){<br \/>\n        current_local.set(empid);<br \/>\n    }<\/p>\n<p>    public static integer getcurrentid(){<br \/>\n        return current_local.get();<br \/>\n    }<\/p>\n<p>    public static void remove(){<br \/>\n        current_local.remove();<br \/>\n    }<br \/>\n}<\/p>\n<h5>\u4f7f\u7528\u6d41\u7a0b<\/h5>\n<li>Token \u8fc7\u6ee4\u5668\u4e2d\u89e3\u6790\u767b\u5f55 ID&#xff0c;\u8c03\u7528setCurrentId()\u5b58\u5165<\/li>\n<li>AOP\/Service \u4e2d\u8c03\u7528getCurrentId()\u83b7\u53d6\u767b\u5f55\u4eba<\/li>\n<li>\u8bf7\u6c42\u7ed3\u675f\u524d\u8c03\u7528remove()\u6e05\u9664\u6570\u636e<\/li>\n<p>\u26a0\ufe0f \u5f3a\u5236\u6ce8\u610f&#xff1a;\u4f7f\u7528\u5b8c\u6bd5\u5fc5\u987b\u8c03\u7528 remove ()&#xff0c;\u5426\u5219\u7ebf\u7a0b\u590d\u7528\u4f1a\u5bfc\u81f4\u6570\u636e\u9519\u4e71&#xff0c;\u5f15\u53d1\u5185\u5b58\u6cc4\u6f0f\u3002<\/p>\n<hr \/>\n<h3>\u4e5d\u3001\u5b9e\u6218\u6848\u4f8b&#xff1a;\u64cd\u4f5c\u65e5\u5fd7\u8bb0\u5f55<\/h3>\n<h4>\u9700\u6c42<\/h4>\n<p>\u5c06\u7cfb\u7edf\u4e2d\u589e\u3001\u5220\u3001\u6539\u63a5\u53e3\u7684\u64cd\u4f5c\u4fe1\u606f\u8bb0\u5f55\u5230\u6570\u636e\u5e93&#xff0c;\u5305\u542b&#xff1a;\u64cd\u4f5c\u4eba\u3001\u64cd\u4f5c\u65f6\u95f4\u3001\u6267\u884c\u65b9\u6cd5\u3001\u8bf7\u6c42\u53c2\u6570\u3001\u8fd4\u56de\u503c\u3001\u6267\u884c\u65f6\u957f\u3002<\/p>\n<h4>\u5b9e\u73b0\u65b9\u6848<\/h4>\n<li>\u81ea\u5b9a\u4e49&#064;Log\u6ce8\u89e3&#xff0c;\u6807\u8bb0\u9700\u8981\u8bb0\u5f55\u65e5\u5fd7\u7684\u63a5\u53e3<\/li>\n<li>\u5b9a\u4e49\u65e5\u5fd7\u5207\u9762\u7c7b&#xff0c;&#064;Around\u73af\u7ed5\u901a\u77e5\u5339\u914d&#064;Log\u6ce8\u89e3<\/li>\n<li>\u901a\u8fc7ProceedingJoinPoint\u83b7\u53d6\u7c7b\u540d\u3001\u65b9\u6cd5\u540d\u3001\u8bf7\u6c42\u53c2\u6570<\/li>\n<li>\u901a\u8fc7ThreadLocal\u83b7\u53d6\u5f53\u524d\u767b\u5f55\u64cd\u4f5c\u4eba ID<\/li>\n<li>\u8ba1\u7b97\u65b9\u6cd5\u6267\u884c\u8017\u65f6&#xff0c;\u7ec4\u88c5\u65e5\u5fd7\u5b9e\u4f53&#xff0c;\u8c03\u7528 Mapper \u63d2\u5165\u6570\u636e\u5e93<\/li>\n<hr \/>\n<h3>\u5341\u3001\u5f00\u53d1\u907f\u5751\u6307\u5357 \u26a0\ufe0f<\/h3>\n<li>\u5207\u9762\u7c7b\u5fc5\u987b\u540c\u65f6\u52a0&#064;Aspect\u548c&#064;Component&#xff0c;\u4ec5\u52a0&#064;Aspect\u65e0\u6cd5\u88ab Spring \u7ba1\u7406<\/li>\n<li>&#064;Around\u73af\u7ed5\u901a\u77e5\u5fc5\u987b\u8c03\u7528pjp.proceed()&#xff0c;\u4e14\u5fc5\u987b\u8fd4\u56de\u6267\u884c\u7ed3\u679c<\/li>\n<li>ThreadLocal \u4f7f\u7528\u540e\u5fc5\u987b\u624b\u52a8remove()&#xff0c;\u9632\u6b62\u5185\u5b58\u6cc4\u6f0f\u4e0e\u6570\u636e\u6c61\u67d3<\/li>\n<li>\u5207\u5165\u70b9\u8868\u8fbe\u5f0f\u8303\u56f4\u4e0d\u8981\u8fc7\u5927&#xff0c;\u907f\u514d\u4e0d\u5fc5\u8981\u7684\u589e\u5f3a\u5f71\u54cd\u63a5\u53e3\u6027\u80fd<\/li>\n<li>&#064;After\u65e0\u8bba\u5f02\u5e38\u4e0e\u5426\u90fd\u4f1a\u6267\u884c&#xff0c;\u9700\u8981\u4ec5\u6210\u529f\u573a\u666f\u6267\u884c\u9009&#064;AfterReturning<\/li>\n<li>\u540c\u4e00\u4e2a\u65b9\u6cd5\u88ab\u591a\u4e2a\u5207\u9762\u589e\u5f3a&#xff0c;\u53ef\u901a\u8fc7&#064;Order\u6ce8\u89e3\u6307\u5b9a\u6267\u884c\u4f18\u5148\u7ea7<\/li>\n<h3>\u5341\u4e00\u3001\u6587\u672b\u603b\u7ed3<\/h3>\n<p>AOP \u662f Spring \u6846\u67b6\u4e09\u5927\u6838\u5fc3\u7279\u6027\u4e4b\u4e00&#xff0c;\u901a\u8fc7\u52a8\u6001\u4ee3\u7406\u5b9e\u73b0\u4ee3\u7801\u65e0\u4fb5\u5165\u589e\u5f3a&#xff0c;\u662f\u4f01\u4e1a\u5f00\u53d1\u4e2d\u89e3\u8026\u901a\u7528\u903b\u8f91\u7684\u6838\u5fc3\u624b\u6bb5\u3002 \u638c\u63e1\u4e94\u5927\u901a\u77e5\u7c7b\u578b\u3001\u4e24\u79cd\u5207\u5165\u70b9\u8868\u8fbe\u5f0f\u5199\u6cd5&#xff0c;\u7ed3\u5408 ThreadLocal \u5b9e\u73b0\u4e0a\u4e0b\u6587\u6570\u636e\u4f20\u9012&#xff0c;\u5373\u53ef\u8986\u76d6\u7edd\u5927\u591a\u6570 AOP \u4e1a\u52a1\u573a\u666f&#xff0c;\u4e5f\u662f Spring \u4e8b\u52a1\u3001\u65e5\u5fd7\u3001\u6743\u9650\u7b49\u7ec4\u4ef6\u7684\u5e95\u5c42\u57fa\u7840\u3002<\/p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e00\u3001AOP \u57fa\u7840\u6982\u5ff51. \u4ec0\u4e48\u662f AOPAOP&#xff08;Aspect Oriented Programming&#xff09;\u5373\u9762\u5411\u5207\u9762\u7f16\u7a0b&#xff0c;\u6838\u5fc3\u601d\u60f3&#xff1a;\u4e0d\u4fee\u6539\u539f\u6709\u4e1a\u52a1\u4ee3\u7801&#xff0c;\u5bf9\u6307\u5b9a\u65b9\u6cd5\u8fdb\u884c\u529f\u80fd\u589e\u5f3a\u3002 AOP \u662f\u4e00\u79cd\u7f16\u7a0b\u601d\u60f3&#xff0c;Spring AOP \u662f\u8be5\u601d\u60f3\u5728 Spring \u6846\u67b6\u4e2d\u7684\u843d\u5730\u5b9e\u73b0&#xff0c;\u5e95\u5c42\u57fa\u4e8e\u52a8\u6001\u4ee3\u7406\u6280\u672f\u30022. \u6838\u5fc3\u4f18\u52bf\u51cf\u5c11\u91cd\u590d\u4ee3\u7801&#xff0c;\u901a\u7528\u903b\u8f91\u7edf\u4e00\u7ef4\u62a4\u5bf9\u4e1a\u52a1\u4ee3\u7801\u96f6\u4fb5\u5165&#xff0c;\u4e0d\u5f71<\/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":[10187,99,101,87,292,190],"topic":[],"class_list":["post-93099","post","type-post","status-publish","format-standard","hentry","category-server","tag-codingplan","tag-java","tag-spring","tag-87","tag-292","tag-190"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Spring AOP \u8d85\u5168\u8be6\u89e3\uff1a\u9762\u5411\u5207\u9762\u7f16\u7a0b + \u4e94\u5927\u901a\u77e5\u7c7b\u578b + \u5207\u5165\u70b9\u8868\u8fbe\u5f0f + ThreadLocal \u5b9e\u6218 - \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\/93099.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Spring AOP \u8d85\u5168\u8be6\u89e3\uff1a\u9762\u5411\u5207\u9762\u7f16\u7a0b + \u4e94\u5927\u901a\u77e5\u7c7b\u578b + \u5207\u5165\u70b9\u8868\u8fbe\u5f0f + ThreadLocal \u5b9e\u6218 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u4e00\u3001AOP \u57fa\u7840\u6982\u5ff51. \u4ec0\u4e48\u662f AOPAOP&#xff08;Aspect Oriented Programming&#xff09;\u5373\u9762\u5411\u5207\u9762\u7f16\u7a0b&#xff0c;\u6838\u5fc3\u601d\u60f3&#xff1a;\u4e0d\u4fee\u6539\u539f\u6709\u4e1a\u52a1\u4ee3\u7801&#xff0c;\u5bf9\u6307\u5b9a\u65b9\u6cd5\u8fdb\u884c\u529f\u80fd\u589e\u5f3a\u3002 AOP \u662f\u4e00\u79cd\u7f16\u7a0b\u601d\u60f3&#xff0c;Spring AOP \u662f\u8be5\u601d\u60f3\u5728 Spring \u6846\u67b6\u4e2d\u7684\u843d\u5730\u5b9e\u73b0&#xff0c;\u5e95\u5c42\u57fa\u4e8e\u52a8\u6001\u4ee3\u7406\u6280\u672f\u30022. \u6838\u5fc3\u4f18\u52bf\u51cf\u5c11\u91cd\u590d\u4ee3\u7801&#xff0c;\u901a\u7528\u903b\u8f91\u7edf\u4e00\u7ef4\u62a4\u5bf9\u4e1a\u52a1\u4ee3\u7801\u96f6\u4fb5\u5165&#xff0c;\u4e0d\u5f71\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/93099.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2026-08-11T15:10:31+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=\"2 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/93099.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/93099.html\",\"name\":\"Spring AOP \u8d85\u5168\u8be6\u89e3\uff1a\u9762\u5411\u5207\u9762\u7f16\u7a0b + \u4e94\u5927\u901a\u77e5\u7c7b\u578b + \u5207\u5165\u70b9\u8868\u8fbe\u5f0f + ThreadLocal \u5b9e\u6218 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2026-08-11T15:10:31+00:00\",\"dateModified\":\"2026-08-11T15:10:31+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/93099.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/93099.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/93099.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Spring AOP \u8d85\u5168\u8be6\u89e3\uff1a\u9762\u5411\u5207\u9762\u7f16\u7a0b + \u4e94\u5927\u901a\u77e5\u7c7b\u578b + \u5207\u5165\u70b9\u8868\u8fbe\u5f0f + ThreadLocal \u5b9e\u6218\"}]},{\"@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":"Spring AOP \u8d85\u5168\u8be6\u89e3\uff1a\u9762\u5411\u5207\u9762\u7f16\u7a0b + \u4e94\u5927\u901a\u77e5\u7c7b\u578b + \u5207\u5165\u70b9\u8868\u8fbe\u5f0f + ThreadLocal \u5b9e\u6218 - \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\/93099.html","og_locale":"zh_CN","og_type":"article","og_title":"Spring AOP \u8d85\u5168\u8be6\u89e3\uff1a\u9762\u5411\u5207\u9762\u7f16\u7a0b + \u4e94\u5927\u901a\u77e5\u7c7b\u578b + \u5207\u5165\u70b9\u8868\u8fbe\u5f0f + ThreadLocal \u5b9e\u6218 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u4e00\u3001AOP \u57fa\u7840\u6982\u5ff51. \u4ec0\u4e48\u662f AOPAOP&#xff08;Aspect Oriented Programming&#xff09;\u5373\u9762\u5411\u5207\u9762\u7f16\u7a0b&#xff0c;\u6838\u5fc3\u601d\u60f3&#xff1a;\u4e0d\u4fee\u6539\u539f\u6709\u4e1a\u52a1\u4ee3\u7801&#xff0c;\u5bf9\u6307\u5b9a\u65b9\u6cd5\u8fdb\u884c\u529f\u80fd\u589e\u5f3a\u3002 AOP \u662f\u4e00\u79cd\u7f16\u7a0b\u601d\u60f3&#xff0c;Spring AOP \u662f\u8be5\u601d\u60f3\u5728 Spring \u6846\u67b6\u4e2d\u7684\u843d\u5730\u5b9e\u73b0&#xff0c;\u5e95\u5c42\u57fa\u4e8e\u52a8\u6001\u4ee3\u7406\u6280\u672f\u30022. \u6838\u5fc3\u4f18\u52bf\u51cf\u5c11\u91cd\u590d\u4ee3\u7801&#xff0c;\u901a\u7528\u903b\u8f91\u7edf\u4e00\u7ef4\u62a4\u5bf9\u4e1a\u52a1\u4ee3\u7801\u96f6\u4fb5\u5165&#xff0c;\u4e0d\u5f71","og_url":"https:\/\/www.wsisp.com\/helps\/93099.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2026-08-11T15:10:31+00:00","author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"2 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/93099.html","url":"https:\/\/www.wsisp.com\/helps\/93099.html","name":"Spring AOP \u8d85\u5168\u8be6\u89e3\uff1a\u9762\u5411\u5207\u9762\u7f16\u7a0b + \u4e94\u5927\u901a\u77e5\u7c7b\u578b + \u5207\u5165\u70b9\u8868\u8fbe\u5f0f + ThreadLocal \u5b9e\u6218 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2026-08-11T15:10:31+00:00","dateModified":"2026-08-11T15:10:31+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/93099.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/93099.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/93099.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"Spring AOP \u8d85\u5168\u8be6\u89e3\uff1a\u9762\u5411\u5207\u9762\u7f16\u7a0b + \u4e94\u5927\u901a\u77e5\u7c7b\u578b + \u5207\u5165\u70b9\u8868\u8fbe\u5f0f + ThreadLocal \u5b9e\u6218"}]},{"@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\/93099","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=93099"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/93099\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=93099"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=93099"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=93099"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=93099"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}