{"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 <\/head>
\n <\/p>\n

\n

SpringBoot AOP<\/h1>\n<\/p><\/div>\n
\n \u8be5\u5e94\u7528\u7a0b\u5e8f\u901a\u5e38\u662f\u591a\u5c42\u5f00\u53d1\u7684\u3002\u5178\u578b\u7684Java\u5e94\u7528\u7a0b\u5e8f\u5177\u6709\u4ee5\u4e0b\u51e0\u5c42:\n <\/div>\n

Web\u5c42: <\/strong>\u5b83\u4f7f\u7528REST\u6216Web\u5e94\u7528\u7a0b\u5e8f\u516c\u5f00\u670d\u52a1<\/strong>\u3002<\/span>
\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 \u6bcf\u5c42\u7684\u804c\u8d23\u4e0d\u540c\uff0c\u4f46\u662f\u6709\u4e00\u4e9b\u9002\u7528\u4e8e\u6240\u6709\u5c42\u7684\u5e38\u89c1\u65b9\u9762\u662f
\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 \u5982\u679c\u6211\u4eec\u5206\u522b\u5728\u6bcf\u4e00\u5c42\u4e2d\u5b9e\u73b0\u8fd9\u4e9b\u5173\u6ce8\u70b9\uff0c\u5219\u4ee3\u7801\u5c06\u53d8\u5f97\u66f4\u52a0\u96be\u4ee5\u7ef4\u62a4\u3002\u4e3a\u4e86\u514b\u670d\u8fd9\u4e2a\u95ee\u9898\uff0c
\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

\u5c06\u8de8\u9886\u57df\u5173\u6ce8\u4f5c\u4e3a\u4e00\u4e2a\u65b9\u9762\u3002<\/span>
\n \u5b9a\u4e49\u5207\u5165\u70b9\u4ee5\u6307\u793a\u5fc5\u987b\u5728\u4f55\u5904\u5e94\u7528\u65b9\u9762\u3002<\/span> <\/p>\n

\n \u5b83\u786e\u4fdd\u8de8\u9886\u57df\u5173\u6ce8\u70b9\u5728\u4e00\u4e2a\u5185\u805a\u7684\u4ee3\u7801\u7ec4\u4ef6\u4e2d\u5b9a\u4e49\u3002\n <\/div>\n

AOP <\/h2>\n
\n AOP
\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 Spring\u7684AOP\u6846\u67b6\u53ef\u5e2e\u52a9\u6211\u4eec\u5b9e\u73b0\u8fd9\u4e9b\u8de8\u9886\u57df\u7684\u5173\u6ce8\u3002\n <\/div>\n
\n \u4f7f\u7528AOP\uff0c\u6211\u4eec\u5728\u4e00\u4e2a\u5730\u65b9\u3002\u6211\u4eec\u53ef\u4ee5\u81ea\u7531\u5b9a\u4e49\u5e94\u7528\u6b64\u529f\u80fd\u7684\u65b9\u5f0f\u548c\u4f4d\u7f6e\uff0c\u800c\u65e0\u9700\u4fee\u6539\u5e94\u7528\u65b0\u529f\u80fd\u7684\u7c7b\u3002\u73b0\u5728\uff0c\u53ef\u4ee5\u5c06\u6a2a\u5207\u5173\u6ce8\u70b9\u6a21\u5757\u5316\u4e3a\u7279\u6b8a\u7c7b\uff0c\u79f0\u4e3a
\n \u65b9\u9762<\/strong>\u3002\n <\/div>\n
\n \u65b9\u9762\u6709
\n \u4e24\u4e2a<\/strong>\u7684\u4f18\u70b9:\n <\/div>\n

\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>
\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 \u5404\u4e2a\u65b9\u9762\u90fd\u6709\u8981\u6267\u884c\u7684\u804c\u8d23\uff0c\u79f0\u4e3a
\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

\u5b83\u662f\u7528\u7eafJava\u5b9e\u73b0\u7684\u3002<\/span>
\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
\n \u8de8\u9886\u57df\u5173\u6ce8\u70b9\u662f\u6211\u4eec\u8981\u5728\u5e94\u7528\u7a0b\u5e8f\u4e2d\u7684\u591a\u4e2a\u4f4d\u7f6e\u5b9e\u73b0\u7684\u5173\u6ce8\u70b9\u3002\u5b83\u4f1a\u5f71\u54cd\u6574\u4e2a\u5e94\u7528\u7a0b\u5e8f\u3002\n <\/div>\n

AOP\u672f\u8bed<\/h2>\n

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>
\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>\u5b83\u662f\u5728\u5c06Advice\u5e94\u7528\u4e8e\u76ee\u6807\u5bf9\u8c61\u540e\u521b\u5efa\u7684\u5bf9\u8c61\uff0c\u79f0\u4e3a
\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 AOP\u4e0eOOP\u4e4b\u95f4\u7684\u533a\u522b\u5982\u4e0b:\n <\/div>\n\n\n\n\n\n\n\n
AOP <\/td>\n OOP <\/td>\n<\/tr>\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
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
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
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 SpringBoot AOP_https:\/\/bianchenghao6.com_\u3010SpringBoot \u6559\u7a0b\u3011_\u7b2c1\u5f20\n <\/div>\n

Spring AOP\u4e0eAspectJ <\/h2>\n
\n AOP\u548cOOP\u4e4b\u95f4\u7684\u533a\u522b\u5982\u4e0b:\n <\/div>\n\n\n\n\n\n\n\n
Spring AOP <\/td>\n AspectJ <\/td>\n<\/tr>\n
\u9700\u8981\u5355\u72ec\u7684\u7f16\u8bd1\u8fc7\u7a0b\u3002<\/td>\n\u5b83\u9700\u8981AspectJ\u7f16\u8bd1\u5668\u3002<\/td>\n<\/tr>\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
\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
\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 AOP Advice\u6709\u4e94\u79cd\u7c7b\u578b\uff0c\u5982\u4e0b\u6240\u793a:\n <\/div>\n

Before Advice<\/span>
\n After Advice<\/span>
\n Around Advice<\/span>
\n After Throwing<\/span>
\n After Returning<\/span> <\/p>\n

\n Before Advice: <\/strong>\u5728\u8fde\u63a5\u70b9\u4e4b\u524d\u6267\u884c\u7684Advice\u5728\u901a\u77e5\u4e4b\u524d\u88ab\u8c03\u7528\u3002\u6211\u4eec\u4f7f\u7528
\n @Before <\/strong>\u6279\u6ce8\u5c06Advice\u6807\u8bb0\u4e3aBefore\u901a\u77e5\u3002\n <\/div>\n
\n After Advice: <\/strong>\u5728\u8fde\u63a5\u70b9\u4e4b\u540e\u6267\u884c\u7684Advice\u88ab\u79f0\u4e3aafter notification\u3002\u6211\u4eec\u4f7f\u7528
\n @After <\/strong>\u6ce8\u89e3\u5c06Advice\u6807\u8bb0\u4e3aAfter\u901a\u77e5\u3002\n <\/div>\n
\n Around Advice: <\/strong>\u5728\u8fde\u63a5\u70b9\u4e4b\u524d\u548c\u4e4b\u540e\u6267\u884c\u7684Advice\u662f\n <\/div>\n
\n After Throwing: <\/strong>\u5728\u8fde\u63a5\u70b9\u629b\u51fa\u5f02\u5e38\u65f6\u6267\u884c\u7684Advice\u3002\n <\/div>\n
\n After Returning: <\/strong> \u5f53\u65b9\u6cd5\u6210\u529f\u6267\u884c\u65f6\u6267\u884c\u7684Advice\u3002\n <\/div>\n
\n \u5728\u5e94\u7528\u7a0b\u5e8f\u4e2d\u5b9e\u73b0AOP\u4e4b\u524d\uff0c\u6211\u4eec\u9700\u8981\u5728pom.xml\u6587\u4ef6\u4e2d\u6dfb\u52a0
\n Spring AOP <\/strong>\u4f9d\u8d56\u9879\u3002\n <\/div>\n

Spring Boot Starter AOP <\/h2>\n
\n Spring Boot Starter AOP\u662f\u63d0\u4f9bSpring AOP\u548cAspectJ\u7684\u4f9d\u8d56\u9879\u3002 AOP\u63d0\u4f9b\u57fa\u672c\u7684AOP\u529f\u80fd\uff0c\u800cAspectJ\u63d0\u4f9b\u5b8c\u6574\u7684AOP\u6846\u67b6\u3002\n <\/div>\n
\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

<\/code>\n <\/div>\n

\n \u5728\u4e0b\u4e00\u8282\u4e2d\uff0c\u6211\u4eec\u5c06\u5728\u5e94\u7528\u7a0b\u5e8f\u4e2d\u5b9e\u73b0\u4e0d\u540c\u7684Advice\u3002\n <\/div>\n
\n
\n SpringBoot AOP @Before\u6ce8\u89e3\u8bf4\u660e\u548c\u793a\u4f8b
\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

<\/body>
\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}]}}