{"id":11873,"date":"2023-11-28T22:36:33","date_gmt":"2023-11-28T14:36:33","guid":{"rendered":"https:\/\/wx.kaifamiao.info\/?p=11873"},"modified":"2023-12-11T14:35:12","modified_gmt":"2023-12-11T06:35:12","slug":"spring-zhong-you-na-xie-lei-xing-de-tong-zhi-advic","status":"publish","type":"post","link":"http:\/\/wx.kaifamiao.info\/index.php\/2023\/11\/28\/spring-zhong-you-na-xie-lei-xing-de-tong-zhi-advic\/","title":{"rendered":"Spring\u4e2d\u6709\u54ea\u4e9b\u7c7b\u578b\u7684\u901a\u77e5(Advice)?"},"content":{"rendered":"<p>\u5728Spring AOP\u4e2d\uff0c\u6709\u51e0\u79cd\u7c7b\u578b\u7684\u901a\u77e5\uff08Advice\uff09\uff0c\u6bcf\u79cd\u901a\u77e5\u5bf9\u5e94\u4e86\u5728\u76ee\u6807\u65b9\u6cd5\u7684\u6267\u884c\u8fc7\u7a0b\u4e2d\u7684\u4e0d\u540c\u65f6\u673a\u3002\u4ee5\u4e0b\u662fSpring AOP\u4e2d\u5e38\u89c1\u7684\u901a\u77e5\u7c7b\u578b\uff1a<\/p>\n<ol>\n<li>\n<strong>\u524d\u7f6e\u901a\u77e5\uff08Before advice\uff09\uff1a<\/strong><\/p>\n<ul>\n<li>\u5728\u76ee\u6807\u65b9\u6cd5\u6267\u884c\u4e4b\u524d\u6267\u884c\u7684\u901a\u77e5\u3002\u524d\u7f6e\u901a\u77e5\u53ef\u4ee5\u7528\u4e8e\u6267\u884c\u4e00\u4e9b\u51c6\u5907\u64cd\u4f5c\u6216\u8005\u5728\u65b9\u6cd5\u6267\u884c\u524d\u8fdb\u884c\u6743\u9650\u68c0\u67e5\u7b49\u3002<\/li>\n<\/ul>\n<pre><code class=\"language-java\">@Before(&quot;execution(* com.example.MyService.*(..))&quot;)\npublic void beforeAdvice() {\n    \/\/ \u6267\u884c\u524d\u7f6e\u64cd\u4f5c\n}\n<\/code><\/pre>\n<\/li>\n<li>\n<strong>\u540e\u7f6e\u901a\u77e5\uff08After returning advice\uff09\uff1a<\/strong><\/p>\n<ul>\n<li>\u5728\u76ee\u6807\u65b9\u6cd5\u6210\u529f\u6267\u884c\u4e4b\u540e\u6267\u884c\u7684\u901a\u77e5\u3002\u53ef\u4ee5\u7528\u4e8e\u6267\u884c\u4e00\u4e9b\u6e05\u7406\u64cd\u4f5c\u6216\u8005\u83b7\u53d6\u65b9\u6cd5\u6267\u884c\u7684\u7ed3\u679c\u3002<\/li>\n<\/ul>\n<pre><code class=\"language-java\">@AfterReturning(pointcut = &quot;execution(* com.example.MyService.*(..))&quot;, returning = &quot;result&quot;)\npublic void afterReturningAdvice(Object result) {\n    \/\/ \u6267\u884c\u540e\u7f6e\u64cd\u4f5c\uff0c\u53ef\u4ee5\u8bbf\u95ee\u65b9\u6cd5\u6267\u884c\u7ed3\u679c\n}\n<\/code><\/pre>\n<\/li>\n<li>\n<strong>\u73af\u7ed5\u901a\u77e5\uff08Around advice\uff09\uff1a<\/strong><\/p>\n<ul>\n<li>\u5728\u76ee\u6807\u65b9\u6cd5\u6267\u884c\u524d\u540e\u90fd\u53ef\u4ee5\u6267\u884c\u7684\u901a\u77e5\u3002\u73af\u7ed5\u901a\u77e5\u53ef\u4ee5\u5b8c\u5168\u63a7\u5236\u76ee\u6807\u65b9\u6cd5\u7684\u6267\u884c\u8fc7\u7a0b\uff0c\u5305\u62ec\u662f\u5426\u6267\u884c\u3001\u6267\u884c\u524d\u540e\u7684\u903b\u8f91\u7b49\u3002<\/li>\n<\/ul>\n<pre><code class=\"language-java\">@Around(&quot;execution(* com.example.MyService.*(..))&quot;)\npublic Object aroundAdvice(ProceedingJoinPoint joinPoint) throws Throwable {\n    \/\/ \u6267\u884c\u524d\u7f6e\u903b\u8f91\n    Object result = joinPoint.proceed();  \/\/ \u6267\u884c\u76ee\u6807\u65b9\u6cd5\n    \/\/ \u6267\u884c\u540e\u7f6e\u903b\u8f91\n    return result;\n}\n<\/code><\/pre>\n<\/li>\n<li>\n<strong>\u5f02\u5e38\u901a\u77e5\uff08After throwing advice\uff09\uff1a<\/strong><\/p>\n<ul>\n<li>\u5728\u76ee\u6807\u65b9\u6cd5\u629b\u51fa\u5f02\u5e38\u65f6\u6267\u884c\u7684\u901a\u77e5\u3002\u53ef\u4ee5\u7528\u4e8e\u5904\u7406\u5f02\u5e38\u3001\u8bb0\u5f55\u65e5\u5fd7\u7b49\u3002<\/li>\n<\/ul>\n<pre><code class=\"language-java\">@AfterThrowing(pointcut = &quot;execution(* com.example.MyService.*(..))&quot;, throwing = &quot;ex&quot;)\npublic void afterThrowingAdvice(Exception ex) {\n    \/\/ \u5904\u7406\u5f02\u5e38\u903b\u8f91\n}\n<\/code><\/pre>\n<\/li>\n<li>\n<strong>\u6700\u7ec8\u901a\u77e5\uff08After finally advice\uff09\uff1a<\/strong><\/p>\n<ul>\n<li>\u5728\u76ee\u6807\u65b9\u6cd5\u6267\u884c\u7ed3\u675f\u540e\u65e0\u8bba\u6210\u529f\u8fd8\u662f\u5931\u8d25\u90fd\u4f1a\u6267\u884c\u7684\u901a\u77e5\u3002\u901a\u5e38\u7528\u4e8e\u8fdb\u884c\u4e00\u4e9b\u6e05\u7406\u64cd\u4f5c\u3002<\/li>\n<\/ul>\n<pre><code class=\"language-java\">@After(&quot;execution(* com.example.MyService.*(..))&quot;)\npublic void afterFinallyAdvice() {\n    \/\/ \u6267\u884c\u6700\u7ec8\u64cd\u4f5c\n}\n<\/code><\/pre>\n<\/li>\n<\/ol>\n<p>\u8fd9\u4e9b\u901a\u77e5\u7c7b\u578b\u5141\u8bb8\u5f00\u53d1\u8005\u5728\u76ee\u6807\u65b9\u6cd5\u7684\u4e0d\u540c\u6267\u884c\u9636\u6bb5\u63d2\u5165\u6a2a\u5207\u903b\u8f91\uff0c\u5b9e\u73b0\u4e86\u6a2a\u5207\u5173\u6ce8\u70b9\u7684\u6a21\u5757\u5316\u3002\u901a\u8fc7AOP\u7684\u65b9\u5f0f\uff0c\u53ef\u4ee5\u66f4\u597d\u5730\u5b9e\u73b0\u5173\u6ce8\u70b9\u7684\u5206\u79bb\uff0c\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u7ef4\u62a4\u6027\u548c\u53ef\u590d\u7528\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728Spring AOP\u4e2d\uff0c\u6709\u51e0\u79cd\u7c7b\u578b\u7684\u901a\u77e5\uff08Advice\uff09\uff0c\u6bcf\u79cd\u901a\u77e5\u5bf9\u5e94\u4e86\u5728\u76ee\u6807\u65b9\u6cd5\u7684\u6267\u884c\u8fc7\u7a0b\u4e2d\u7684\u4e0d\u540c\u65f6\u673a\u3002\u4ee5 [&hellip;]<\/p>\n","protected":false},"author":9,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[121],"tags":[],"class_list":["post-11873","post","type-post","status-publish","format-standard","hentry","category-spring"],"_links":{"self":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/11873","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/users\/9"}],"replies":[{"embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/comments?post=11873"}],"version-history":[{"count":2,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/11873\/revisions"}],"predecessor-version":[{"id":37197,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/11873\/revisions\/37197"}],"wp:attachment":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/media?parent=11873"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/categories?post=11873"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/tags?post=11873"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}