{"id":590,"date":"2023-03-23T21:28:47","date_gmt":"2023-03-23T13:28:47","guid":{"rendered":""},"modified":"2023-03-23T21:28:47","modified_gmt":"2023-03-23T13:28:47","slug":"SpringBoot AOP","status":"publish","type":"post","link":"https:\/\/bianchenghao6.com\/590.html","title":{"rendered":"SpringBoot AOP"},"content":{"rendered":"
\n
Web\u5c42: <\/strong>\u5b83\u4f7f\u7528REST\u6216Web\u5e94\u7528\u7a0b\u5e8f\u516c\u5f00\u670d\u52a1<\/strong>\u3002<\/span> \u5c06\u8de8\u9886\u57df\u5173\u6ce8\u4f5c\u4e3a\u4e00\u4e2a\u65b9\u9762\u3002<\/span> \u9996\u5148\uff0c\u6bcf\u4e2a\u5173\u6ce8\u70b9\u7684\u903b\u8f91\u73b0\u5728\u90fd\u96c6\u4e2d\u5728\u4e00\u4e2a\u5730\u65b9\uff0c\u800c\u4e0d\u662f\u5206\u6563\u5728\u6574\u4e2a\u4ee3\u7801\u5e93\u4e2d\u3002<\/span> \u5b83\u662f\u7528\u7eafJava\u5b9e\u73b0\u7684\u3002<\/span> Aspect: <\/strong>\u65b9\u9762\u662f\u4e00\u4e2a\u6a21\u5757\uff0c\u5176\u4e2d\u5c01\u88c5\u4e86advice<\/strong>\u548cpointcuts<\/strong>\uff0c\u5e76\u63d0\u4f9bcross-cutting<\/strong>\u53ef\u4ee5\u6709\u8bb8\u591a\u65b9\u9762\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u5e26\u6709 @Aspect <\/strong>\u6279\u6ce8\u7684\u5e38\u89c4\u7c7b\u6765\u5b9e\u73b0\u65b9\u9762\u3002<\/span> Before Advice<\/span> <\/code>\n <\/div>\n <\/body>
\n \u4e1a\u52a1\u5c42: <\/strong>\u5b83\u5b9e\u73b0\u4e86\u5e94\u7528\u7a0b\u5e8f\u7684\u4e1a\u52a1\u903b\u8f91<\/strong>\u3002<\/span>
\n \u6570\u636e\u5c42: <\/strong>\u5b83\u5b9e\u73b0\u4e86\u5e94\u7528\u7a0b\u5e8f\u7684\u6301\u4e45\u6027\u903b\u8f91<\/strong>\u3002<\/span> <\/p>\n
\n \u65e5\u5fd7\u8bb0\u5f55\uff0c\u5b89\u5168\u6027\uff0c\u9a8c\u8bc1\uff0c\u7f13\u5b58\u7b49<\/strong>\u3002\u8fd9\u4e9b\u5e38\u89c1\u65b9\u9762\u88ab\u79f0\u4e3a
\n \u8de8\u9886\u57df\u5173\u6ce8\u70b9\u3002<\/strong>\n <\/div>\n
\n \u9762\u5411\u65b9\u9762\u7684\u7f16\u7a0b<\/strong>(AOP)\u63d0\u4f9b\u4e86\u4e00\u79cd\u89e3\u51b3\u8de8\u9886\u57df\u95ee\u9898\u7684\u89e3\u51b3\u65b9\u6848\u3002\n <\/div>\n
\n \u5b9a\u4e49\u5207\u5165\u70b9\u4ee5\u6307\u793a\u5fc5\u987b\u5728\u4f55\u5904\u5e94\u7528\u65b9\u9762\u3002<\/span> <\/p>\n AOP <\/h2>\n
\n (Aspect-Oriented Programming)<\/strong>\u662f\u4e00\u79cd\u901a\u8fc7\u5141\u8bb8
\n \u8de8\u9886\u57df\u5173\u6ce8\u70b9<\/strong>\u5206\u79bb\u6765\u63d0\u9ad8\u6a21\u5757\u5316\u7684\u7f16\u7a0b\u6a21\u5f0f\u3002\u8fd9\u4e9b\u8de8\u90e8\u95e8\u5173\u6ce8\u70b9\u4e0e\u4e3b\u8981\u4e1a\u52a1\u903b\u8f91\u4e0d\u540c\u3002\u6211\u4eec\u53ef\u4ee5\u5728\u4e0d\u4fee\u6539\u4ee3\u7801\u672c\u8eab\u7684\u60c5\u51b5\u4e0b\u5411\u73b0\u6709\u4ee3\u7801\u6dfb\u52a0\u5176\u4ed6\u884c\u4e3a\u3002\n <\/div>\n
\n \u65b9\u9762<\/strong>\u3002\n <\/div>\n
\n \u4e24\u4e2a<\/strong>\u7684\u4f18\u70b9:\n <\/div>\n
\n \u5176\u6b21\uff0c\u4e1a\u52a1\u6a21\u5757\u4ec5\u5305\u542b\u4e3b\u8981\u5173\u6ce8\u7684\u4ee3\u7801\u3002\u6b21\u8981\u5173\u6ce8\u70b9\u5df2\u79fb\u81f3\u65b9\u9762<\/strong>\u3002<\/span> <\/p>\n
\n Advice<\/strong>\u3002\u6211\u4eec\u53ef\u4ee5\u5728\u4e00\u4e2a\u6216\u591a\u4e2a\u8fde\u63a5\u70b9\u5c06\u65b9\u9762\u7684\u529f\u80fd\u5b9e\u73b0\u5230\u7a0b\u5e8f\u4e2d\u3002\n <\/div>\n AOP\u7684\u4f18\u70b9<\/h2>\n
\n \u4e0d\u9700\u8981\u7279\u6b8a\u7684\u7f16\u8bd1\u8fc7\u7a0b\u3002<\/span>
\n \u5b83\u4ec5\u652f\u6301\u65b9\u6cd5\u6267\u884c\u8fde\u63a5\u70b9\u3002<\/span>
\n \u4ec5\u63d0\u4f9b\u8fd0\u884c\u65f6\u7f16\u7ec7\u3002<\/span>
\n \u6709\u4e24\u79cd\u7c7b\u578b\u7684AOP\u4ee3\u7406: JDK\u52a8\u6001\u4ee3\u7406<\/strong>\u548c CGLIB\u4ee3\u7406\u3002<\/strong> <\/span> <\/p>\n\u8de8\u9886\u57df\u5173\u6ce8\u70b9<\/h2>\n
AOP\u672f\u8bed<\/h2>\n
\n Pointcut: <\/strong>\u5207\u5165\u70b9\u662f\u4e00\u79cd\u8868\u8fbe\u5f0f\uff0c\u5b83\u9009\u62e9\u4e00\u4e2a\u6216\u591a\u4e2a\u6267\u884cAdvice\u7684\u8fde\u63a5\u70b9\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528expressions<\/strong>\u6216patterns<\/strong>\u5b9a\u4e49\u5207\u5165\u70b9\u3002\u5b83\u4f7f\u7528\u4e0e\u8054\u63a5\u70b9\u5339\u914d\u7684\u4e0d\u540c\u7c7b\u578b\u7684\u8868\u8fbe\u5f0f\u3002\u5728Spring Framework\u4e2d\uff0c\u4f7f\u7528 AspectJ <\/strong>\u5207\u5165\u70b9\u8868\u8fbe\u8bed\u8a00\u3002<\/span>
\n Join point: <\/strong>\u8fde\u63a5\u70b9\u662f\u5e94\u7528\u7a0b\u5e8f\u4e2d\u5e94\u7528 AOP\u65b9\u9762<\/strong>\u7684\u70b9\u3002\u6216\u8005\u5b83\u662fAdvice\u7684\u7279\u5b9a\u6267\u884c\u5b9e\u4f8b\u3002\u5728AOP\u4e2d\uff0c\u8fde\u63a5\u70b9\u53ef\u4ee5\u662f\u65b9\u6cd5\u6267\u884c\uff0c\u5f02\u5e38\u5904\u7406\uff0c\u66f4\u6539\u5bf9\u8c61\u53d8\u91cf\u503c<\/strong>\u7b49\u3002<\/span>
\n Advice: <\/strong>Advice\u662f\u6211\u4eec\u5728\u65b9\u6cd5\u6267\u884c\u4e4b\u524dbefore<\/strong>\u6216after<\/strong>\u91c7\u53d6\u7684\u63aa\u65bd\u3002\u8be5\u52a8\u4f5c\u662f\u5728\u7a0b\u5e8f\u6267\u884c\u671f\u95f4\u8c03\u7528\u7684\u4e00\u6bb5\u4ee3\u7801\u3002SpringAOP\u6846\u67b6\u4e2d\u6709\u4e94\u79cd<\/strong>\u7c7b\u578b\u7684Advice: \u5728Advicebefore, after, after-returning, after-throwing\u548c<\/strong>around advice\u3002 \u662f\u9488\u5bf9\u7279\u5b9ajoin point\u7684Advice\u3002 <\/strong>\u6211\u4eec\u5c06\u5728\u672c\u8282\u4e2d\u8fdb\u4e00\u6b65\u8ba8\u8bba\u8fd9\u4e9bAdvice\u3002<\/span>
\n Target object: <\/strong>\u4e00\u4e2a\u5e94\u7528\u4e86Advice\u7684\u5bf9\u8c61\u79f0\u4e3atarget object<\/strong>\u3002\u76ee\u6807\u5bf9\u8c61\u59cb\u7ec8\u662fproxied<\/strong>\uff0c\u8fd9\u610f\u5473\u7740\u5728\u8fd0\u884c\u65f6\u5c06\u521b\u5efa\u4e00\u4e2a\u8986\u76d6\u76ee\u6807\u65b9\u6cd5\u7684\u5b50\u7c7b\uff0c\u5e76\u6839\u636e\u5176\u914d\u7f6e\u5305\u542bAdvice\u3002<\/span>
\n Weaving: <\/strong>\u8fd9\u662f\u5c06\u5404\u4e2a\u65b9\u9762\u4e0e\u5176\u4ed6\u5e94\u7528\u7a0b\u5e8f\u7c7b\u578b\u8fdb\u884clinking aspects<\/strong>\u7684\u8fc7\u7a0b\u3002\u6211\u4eec\u53ef\u4ee5\u5728\u8fd0\u884c\u65f6\uff0c\u52a0\u8f7d\u65f6\u95f4<\/strong>\u548c\u7f16\u8bd1\u65f6<\/strong>\u8fdb\u884c\u7ec7\u9020\u3002<\/span> <\/p>\n
\n proxy <\/strong>\u3002 Spring AOP\u5b9e\u73b0\u4e86
\n JDK\u52a8\u6001\u4ee3\u7406<\/strong>\uff0c\u4ee5\u4f7f\u7528\u76ee\u6807\u7c7b\u548cAdvice\u8c03\u7528\u521b\u5efa\u4ee3\u7406\u7c7b\u3002\u8fd9\u4e9b\u79f0\u4e3aAOP\u4ee3\u7406\u7c7b\u3002\n <\/div>\n AOP\u4e0eOOP <\/h2>\n
\n\n
\n AOP <\/td>\n OOP <\/td>\n<\/tr>\n \n Aspect: <\/strong>\u4e00\u4e2a\u5c01\u88c5\u5207\u5165\u70b9\uff0cAdvice\u548c\u5c5e\u6027\u7684\u4ee3\u7801\u5355\u5143\u3002<\/td>\n Class: <\/strong>\u4e00\u4e2a\u5c01\u88c5\u65b9\u6cd5\u548c\u5c5e\u6027\u7684\u4ee3\u7801\u5355\u5143\u3002<\/td>\n<\/tr>\n \n Pointcut: <\/strong>\u5b83\u5b9a\u4e49\u4e86\u6267\u884cAdvice\u7684\u4e00\u7ec4\u5165\u53e3\u70b9\u3002<\/td>\n Method signature: <\/strong>\u3002\u5b83\u5b9a\u4e49\u4e86\u6267\u884c\u65b9\u6cd5\u4e3b\u4f53\u7684\u5165\u53e3\u70b9\u3002<\/td>\n<\/tr>\n \n Advice: <\/strong>\u8fd9\u662f\u8de8\u9886\u57df\u5173\u6ce8\u70b9\u7684\u5b9e\u73b0\u3002<\/td>\n Method bodies: <\/strong>\u662f\u4e1a\u52a1\u903b\u8f91\u95ee\u9898\u7684\u5b9e\u73b0\u3002<\/td>\n<\/tr>\n \n Waver: \u501f\u52a9Advice\u6784\u9020\u4ee3\u7801(\u6e90\u6216\u5bf9\u8c61)\u3002<\/strong> <\/td>\n Compiler: \u5b83\u5c06\u6e90\u4ee3\u7801\u8f6c\u6362\u4e3a\u76ee\u6807\u4ee3\u7801\u3002<\/strong> <\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n \n <\/div>\n
Spring AOP\u4e0eAspectJ <\/h2>\n
\n\n
\n Spring AOP <\/td>\n AspectJ <\/td>\n<\/tr>\n \n \u9700\u8981\u5355\u72ec\u7684\u7f16\u8bd1\u8fc7\u7a0b\u3002<\/td>\n \u5b83\u9700\u8981AspectJ\u7f16\u8bd1\u5668\u3002<\/td>\n<\/tr>\n \n \u5b83\u4ec5\u652f\u6301\u65b9\u6cd5\u6267\u884c\u5207\u5165\u70b9\u3002<\/td>\n \u5b83\u652f\u6301\u6240\u6709\u5207\u5165\u70b9\u3002<\/td>\n<\/tr>\n \n \u5b83\u53ef\u4ee5\u5728Spring Container\u7ba1\u7406\u7684bean\u4e0a\u5b9e\u73b0\u3002<\/td>\n \u5b83\u53ef\u4ee5\u5728\u6240\u6709\u57df\u5bf9\u8c61\u4e0a\u5b9e\u73b0\u3002<\/td>\n<\/tr>\n \n \u5b83\u4ec5\u652f\u6301\u65b9\u6cd5\u7ea7\u7f16\u7ec7\u3002<\/td>\n \u5b83\u53ef\u4ee5\u6ce2\u52a8\u5b57\u6bb5\uff0c\u65b9\u6cd5\uff0c\u6784\u9020\u51fd\u6570\uff0c\u9759\u6001\u521d\u59cb\u5316\u5668\uff0c\u6700\u7ec8\u7c7b\u7b49\u3002<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n AOP Advice\u7684\u7c7b\u578b<\/h2>\n
\n After Advice<\/span>
\n Around Advice<\/span>
\n After Throwing<\/span>
\n After Returning<\/span> <\/p>\n
\n @Before <\/strong>\u6279\u6ce8\u5c06Advice\u6807\u8bb0\u4e3aBefore\u901a\u77e5\u3002\n <\/div>\n
\n @After <\/strong>\u6ce8\u89e3\u5c06Advice\u6807\u8bb0\u4e3aAfter\u901a\u77e5\u3002\n <\/div>\n
\n Spring AOP <\/strong>\u4f9d\u8d56\u9879\u3002\n <\/div>\n Spring Boot Starter AOP <\/h2>\n
\r\n<dependency>\r\n<groupId>org.springframework.boot<\/groupId>\r\n<artifactId>spring-boot-starter-aop<\/artifactId>\r\n<version>2.2.2.RELEASE<\/version>\r\n<\/dependency>\r\n<\/pre>\n
\n
\n SpringBoot AOP @After\u6ce8\u89e3\u8bf4\u660e\u548c\u793a\u4f8b
\n SpringBoot AOP @Around\u6ce8\u89e3\u8bf4\u660e\u548c\u793a\u4f8b
\n SpringBoot AOP @AfterReturning\u6ce8\u89e3\u8bf4\u660e\u548c\u793a\u4f8b
\n SpringBoot AOP @AfterThrowing\u6ce8\u89e3\u8bf4\u660e\u548c\u793a\u4f8b\n <\/div>\n
\n<\/html><\/p>\n","protected":false},"excerpt":{"rendered":"SpringBoot AOPzh-cn","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[104],"tags":[],"class_list":["post-590","post","type-post","status-publish","format-standard","hentry","category-bc_springboot_jc"],"_links":{"self":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/posts\/590"}],"collection":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/comments?post=590"}],"version-history":[{"count":0,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/posts\/590\/revisions"}],"wp:attachment":[{"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/media?parent=590"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/categories?post=590"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bianchenghao6.com\/wp-json\/wp\/v2\/tags?post=590"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}