{"id":10008425,"date":"2024-03-05T11:20:38","date_gmt":"2024-03-05T03:20:38","guid":{"rendered":"https:\/\/blog.itmax.cn\/?p=10008425"},"modified":"2024-03-05T11:20:38","modified_gmt":"2024-03-05T03:20:38","slug":"%e9%9d%a2%e8%af%95%e5%ae%98%ef%bc%9a%e8%af%b4%e8%af%b4springaop%e5%ae%9e%e7%8e%b0%e5%8e%9f%e7%90%86%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/itmax.cn\/?p=10008425","title":{"rendered":"\u9762\u8bd5\u5b98\uff1a\u8bf4\u8bf4SpringAOP\u5b9e\u73b0\u539f\u7406\uff1f"},"content":{"rendered":"<p>AOP\uff08Aspect-Oriented Programming\uff0c\u9762\u5411\u5207\u9762\u7f16\u7a0b\uff09\u662f\u4e00\u79cd\u7f16\u7a0b\u6280\u672f\uff0c\u5b83\u5141\u8bb8\u5f00\u53d1\u8005\u5728\u4e0d\u6539\u53d8\u73b0\u6709\u4ee3\u7801\u7684\u60c5\u51b5\u4e0b\uff0c\u589e\u52a0\u65b0\u7684\u529f\u80fd\u6216\u884c\u4e3a\uff0c\u8fd9\u4e9b\u529f\u80fd\u6216\u884c\u4e3a\u88ab\u79f0\u4e3a\u201c\u5207\u9762\u201d\u3002<\/p>\n<p>AOP \u53ef\u4ee5\u901a\u8fc7\u9884\u7f16\u8bd1\u65b9\u5f0f\u548c\u8fd0\u884c\u671f\u52a8\u6001\u4ee3\u7406\u7684\u65b9\u5f0f\u6765\u5b9e\u73b0\uff0c\u5b83\u7684\u4e3b\u8981\u76ee\u7684\u662f\u964d\u4f4e\u4e1a\u52a1\u903b\u8f91\u7684\u8026\u5408\u6027\uff0c\u63d0\u9ad8\u7a0b\u5e8f\u7684\u53ef\u91cd\u7528\u6027\u548c\u5f00\u53d1\u6548\u7387\u3002<\/p>\n<p>AOP \u5e38\u7528\u4e8e\u7edf\u4e00\u529f\u80fd\u7684\u5904\u7406\uff0c\u4f8b\u5982\uff1a\u4e8b\u52a1\u7ba1\u7406\u3001\u65e5\u5fd7\u8bb0\u5f55\u3001\u6743\u9650\u68c0\u67e5\u7b49\u529f\u80fd\u3002<\/p>\n<h2>1.AOP\u4f18\u70b9\u5206\u6790<\/h2>\n<p>\u4f7f\u7528 AOP \u7684\u4e3b\u8981\u539f\u56e0\u6709\u4ee5\u4e0b\u51e0\u70b9\uff1a<\/p>\n<ol>\n<li><strong>\u6a21\u5757\u5316<\/strong>\uff1a\u901a\u8fc7\u5c06\u516c\u5171\u884c\u4e3a\uff08\u5982\u65e5\u5fd7\u8bb0\u5f55\u3001\u4e8b\u52a1\u7ba1\u7406\uff09\u63d0\u53d6\u4e3a\u72ec\u7acb\u7684\u5207\u9762\uff0c\u53ef\u4ee5\u4f7f\u4ee3\u7801\u66f4\u52a0\u6a21\u5757\u5316\uff0c\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u7ef4\u62a4\u6027\u548c\u53ef\u8bfb\u6027\u3002<\/li>\n<li><strong>\u51cf\u5c11\u91cd\u590d\u4ee3\u7801<\/strong>\uff1a\u901a\u8fc7\u4f7f\u7528 AOP\uff0c\u53ef\u4ee5\u5c06\u91cd\u590d\u7684\u4ee3\u7801\uff08\u5982\u65e5\u5fd7\u8bb0\u5f55\u3001\u6743\u9650\u68c0\u67e5\uff09\u63d0\u53d6\u5230\u4e00\u4e2a\u5207\u9762\u4e2d\uff0c\u907f\u514d\u5728\u591a\u4e2a\u5730\u65b9\u91cd\u590d\u7f16\u5199\u76f8\u540c\u7684\u4ee3\u7801\u3002<\/li>\n<li>\n<p><strong>\u89e3\u8026<\/strong>\uff1aAOP \u5141\u8bb8\u5f00\u53d1\u8005\u5c06\u4e1a\u52a1\u903b\u8f91\u4e0e\u6a2a\u5207\u5173\u6ce8\u70b9\uff08\u5982\u65e5\u5fd7\u8bb0\u5f55\u3001\u4e8b\u52a1\u7ba1\u7406\uff09\u5206\u79bb\uff0c\u4ece\u800c\u964d\u4f4e\u4e1a\u52a1\u903b\u8f91\u7684\u8026\u5408\u6027\uff0c\u63d0\u9ad8\u7a0b\u5e8f\u7684\u53ef\u91cd\u7528\u6027\u548c\u53ef\u6269\u5c55\u6027\u3002<\/p>\n<h2>2.AOP\u6280\u672f\u5b9e\u73b0<\/h2>\n<p>AOP \u5b9e\u73b0\u6280\u672f\u4e3b\u8981\u5206\u4e3a\u4e24\u5927\u7c7b\uff1a\u9759\u6001\u4ee3\u7406\u548c\u52a8\u6001\u4ee3\u7406\u3002<\/p>\n<\/li>\n<li><strong>\u9759\u6001\u4ee3\u7406<\/strong>\uff1a\u901a\u8fc7 AOP \u6846\u67b6\u63d0\u4f9b\u7684\u547d\u4ee4\u8fdb\u884c\u7f16\u8bd1\uff0c\u4ece\u800c\u5728\u7f16\u8bd1\u9636\u6bb5\u751f\u6210 AOP \u4ee3\u7406\u7c7b\u3002\u8fd9\u79cd\u65b9\u5f0f\u4e5f\u88ab\u79f0\u4e3a\u7f16\u8bd1\u65f6\u589e\u5f3a\u3002\u9759\u6001\u4ee3\u7406\u5305\u62ec\u7f16\u8bd1\u65f6\u7f16\u7ec7\u548c\u7c7b\u52a0\u8f7d\u65f6\u7f16\u7ec7\u4e24\u79cd\u65b9\u5f0f\u3002<\/li>\n<li>\n<p><strong>\u52a8\u6001\u4ee3\u7406<\/strong>\uff1a\u5728\u8fd0\u884c\u65f6\u5728\u5185\u5b58\u4e2d\u201c\u4e34\u65f6\u201d\u751f\u6210 AOP \u52a8\u6001\u4ee3\u7406\u7c7b\uff0c\u56e0\u6b64\u4e5f\u88ab\u79f0\u4e3a\u8fd0\u884c\u65f6\u589e\u5f3a\u3002\u52a8\u6001\u4ee3\u7406\u4e3b\u8981\u6709\u4e24\u79cd\u5b9e\u73b0\u65b9\u5f0f\uff1a<\/p>\n<ol>\n<li><strong>JDK \u52a8\u6001\u4ee3\u7406<\/strong>\uff1aJDK \u52a8\u6001\u4ee3\u7406\u8981\u6c42\u88ab\u4ee3\u7406\u7684\u7c7b\u5fc5\u987b\u5b9e\u73b0\u4e00\u4e2a\u63a5\u53e3\uff0c\u5b83\u901a\u8fc7\u53cd\u5c04\u6765\u63a5\u6536\u88ab\u4ee3\u7406\u7684\u7c7b\uff0c\u5e76\u4f7f\u7528 InvocationHandler \u63a5\u53e3\u548c Proxy \u7c7b\u5b9e\u73b0\u4ee3\u7406\u3002<\/li>\n<li>\n<p><strong>CGLIB \u52a8\u6001\u4ee3\u7406<\/strong>\uff1aCGLIB \u5219\u662f\u4e00\u4e2a\u4ee3\u7801\u751f\u6210\u7684\u7c7b\u5e93\uff0c\u5b83\u53ef\u4ee5\u5728\u8fd0\u884c\u65f6\u52a8\u6001\u5730\u751f\u6210\u67d0\u4e2a\u7c7b\u7684\u5b50\u7c7b\uff0c\u901a\u8fc7\u7ee7\u627f\u7684\u65b9\u5f0f\u5b9e\u73b0\u4ee3\u7406\u3002\u5982\u679c\u76ee\u6807\u7c7b\u6ca1\u6709\u5b9e\u73b0\u63a5\u53e3\uff0cSpring AOP \u4f1a\u9009\u62e9\u4f7f\u7528 CGLIB \u6765\u52a8\u6001\u4ee3\u7406\u76ee\u6807\u7c7b\u3002<\/p>\n<h2>3.AOP\u5b9e\u73b0\u539f\u7406<\/h2>\n<p>Spring AOP\uff08\u9762\u5411\u5207\u9762\u7f16\u7a0b\uff09\u7684\u5b9e\u73b0\u539f\u7406\u4e3b\u8981\u57fa\u4e8e\u52a8\u6001\u4ee3\u7406\u6280\u672f\uff0c\u5b83\u63d0\u4f9b\u4e86\u5bf9\u4e1a\u52a1\u903b\u8f91\u5404\u4e2a\u65b9\u9762\u7684\u5173\u6ce8\u70b9\u5206\u79bb\u548c\u6a21\u5757\u5316\uff0c\u4f7f\u5f97\u975e\u529f\u80fd\u6027\u9700\u6c42\uff08\u5982\u65e5\u5fd7\u8bb0\u5f55\u3001\u4e8b\u52a1\u7ba1\u7406\u3001\u5b89\u5168\u68c0\u67e5\u7b49\uff09\u53ef\u4ee5\u96c6\u4e2d\u7ba1\u7406\u548c\u7ef4\u62a4\uff0c\u800c\u4e0d\u662f\u5206\u6563\u5728\u5404\u4e2a\u4e1a\u52a1\u6a21\u5757\u4e2d\u3002<\/p>\n<\/li>\n<\/ol>\n<\/li>\n<\/ol>\n<p>Spring AOP \u5b9e\u73b0\u539f\u7406\u7684\u5173\u952e\u8981\u70b9\u5982\u4e0b\uff1a<\/p>\n<ol>\n<li>\n<p><strong>\u4ee3\u7406\u673a\u5236<\/strong>\uff1a<\/p>\n<ul>\n<li><strong>JDK \u52a8\u6001\u4ee3\u7406<\/strong>\uff1a\u5bf9\u4e8e\u5b9e\u73b0\u4e86\u63a5\u53e3\u7684\u76ee\u6807\u7c7b\uff0cSpring AOP \u9ed8\u8ba4\u4f7f\u7528 JDK \u7684 java.lang.reflect.Proxy \u7c7b\u6765\u521b\u5efa\u4ee3\u7406\u5bf9\u8c61\u3002\u4ee3\u7406\u5bf9\u8c61\u4f1a\u5728\u8fd0\u884c\u65f6\u5b9e\u73b0\u4ee3\u7406\u63a5\u53e3\uff0c\u5e76\u8986\u76d6\u5176\u4e2d\u7684\u65b9\u6cd5\uff0c\u5728\u65b9\u6cd5\u8c03\u7528\u524d\u540e\u6267\u884c\u5207\u9762\u903b\u8f91\uff08\u5373\u901a\u77e5\uff0cadvice\uff09\u3002<\/li>\n<li><strong>CGLIB \u52a8\u6001\u4ee3\u7406<\/strong>\uff1a\u5bf9\u4e8e\u672a\u5b9e\u73b0\u63a5\u53e3\u7684\u7c7b\uff0cSpring AOP \u4f1a\u9009\u62e9\u4f7f\u7528 CGLIB \u5e93\u6765\u751f\u6210\u4ee3\u7406\u5bf9\u8c61\u3002CGLIB \u901a\u8fc7\u5b57\u8282\u7801\u6280\u672f\u521b\u5efa\u76ee\u6807\u7c7b\u7684\u5b50\u7c7b\uff0c\u5728\u5b50\u7c7b\u4e2d\u91cd\u5199\u76ee\u6807\u65b9\u6cd5\u5e76\u5728\u65b9\u6cd5\u8c03\u7528\u524d\u540e\u63d2\u5165\u5207\u9762\u903b\u8f91\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><strong>\u5173\u952e\u6982\u5ff5<\/strong>\uff1a<\/p>\n<ul>\n<li><strong>\u5207\u9762\uff08Aspect\uff09<\/strong>\uff1a\u5207\u9762\u662f\u4e00\u4e2a\u5305\u542b\u4e86\u6a2a\u5207\u5173\u6ce8\u70b9\u58f0\u660e\u7684\u6a21\u5757\u5316\u5355\u5143\uff0c\u5b83\u53ef\u4ee5\u6709\u591a\u4e2a\u5207\u5165\u70b9\u548c\u901a\u77e5\u7ec4\u6210\u3002<\/li>\n<li><strong>\u5207\u5165\u70b9\uff08Pointcut\uff09<\/strong>\uff1a\u5207\u5165\u70b9\u5b9a\u4e49\u4e86\u5339\u914d\u901a\u77e5\u5e94\u8be5\u88ab\u7ec7\u5165\u7684\u65b9\u6cd5\u6216\u65b9\u6cd5\u6267\u884c\u70b9\u7684\u89c4\u5219\u8868\u8fbe\u5f0f\u3002<\/li>\n<li><strong>\u901a\u77e5\uff08Advice\uff09<\/strong>\uff1a\u901a\u77e5\u662f\u5728\u7279\u5b9a\u5207\u5165\u70b9\u5904\u6267\u884c\u7684\u4ee3\u7801\u7247\u6bb5\uff0c\u5206\u4e3a\u591a\u79cd\u7c7b\u578b\uff0c\u5982\u524d\u7f6e\u901a\u77e5\uff08Before advice\uff09\u3001\u540e\u7f6e\u901a\u77e5\uff08After returning advice\uff09\u3001\u5f02\u5e38\u540e\u901a\u77e5\uff08After throwing advice\uff09\u3001\u6700\u7ec8\u901a\u77e5\uff08After (finally) advice\uff09\u4ee5\u53ca\u73af\u7ed5\u901a\u77e5\uff08Around advice\uff09\u3002<\/li>\n<\/ul>\n<\/li>\n<li><strong>\u7ec7\u5165\uff08Weaving\uff09<\/strong>\uff1a\u7ec7\u5165\u662f\u6307\u5c06\u5207\u9762\u5e94\u7528\u5230\u76ee\u6807\u5bf9\u8c61\u6765\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u4ee3\u7406\u5bf9\u8c61\u7684\u8fc7\u7a0b\u3002\u5728 Spring AOP \u4e2d\uff0c\u7ec7\u5165\u53d1\u751f\u5728\u8fd0\u884c\u65f6\uff0c\u901a\u8fc7\u4ee3\u7406\u5bf9\u8c61\u7684\u65b9\u5f0f\u5b9e\u73b0\u3002<\/li>\n<li><strong>\u4ee3\u7406\u5de5\u5382<\/strong>\uff1aSpring \u5185\u90e8\u901a\u8fc7 ProxyFactory \u6216\u76f8\u5173\u7684 AOP \u57fa\u7840\u8bbe\u65bd\uff08\u5982 Advisor\u3001AdvisorChainFactory \u7b49\uff09\u6765\u521b\u5efa\u548c\u7ba1\u7406\u4ee3\u7406\u5bf9\u8c61\u3002<\/li>\n<li><strong>\u6267\u884c\u6d41\u7a0b<\/strong>\uff1a\u5f53\u5ba2\u6237\u7aef\u901a\u8fc7\u4ee3\u7406\u5bf9\u8c61\u8c03\u7528\u76ee\u6807\u65b9\u6cd5\u65f6\uff0c\u4ee3\u7406\u5bf9\u8c61\u4f1a\u62e6\u622a\u8fd9\u4e2a\u8c03\u7528\uff0c\u6839\u636e\u5207\u9762\u914d\u7f6e\u627e\u5230\u5bf9\u5e94\u7684\u901a\u77e5\uff0c\u5e76\u6309\u7167\u901a\u77e5\u7c7b\u578b\u7684\u4e0d\u540c\u6267\u884c\u76f8\u5e94\u7684\u589e\u5f3a\u903b\u8f91\u3002\u4f8b\u5982\uff0c\u5982\u679c\u662f\u73af\u7ed5\u901a\u77e5\uff0c\u5b83\u4f1a\u5b8c\u5168\u63a7\u5236\u539f\u59cb\u65b9\u6cd5\u7684\u8c03\u7528\u8fc7\u7a0b\uff0c\u53ef\u4ee5\u5728\u8c03\u7528\u524d\u540e\u63d2\u5165\u81ea\u5b9a\u4e49\u903b\u8f91\uff0c\u751a\u81f3\u51b3\u5b9a\u662f\u5426\u6267\u884c\u539f\u65b9\u6cd5\u3002<\/li>\n<\/ol>\n<p>\u901a\u8fc7\u4e0a\u8ff0\u65b9\u5f0f\uff0cSpring AOP \u5de7\u5999\u5730\u5b9e\u73b0\u4e86\u5bf9\u76ee\u6807\u5bf9\u8c61\u65b9\u6cd5\u7684\u62e6\u622a\u548c\u589e\u5f3a\uff0c\u4ece\u800c\u5b9e\u73b0\u4e86\u9762\u5411\u5207\u9762\u7f16\u7a0b\u7684\u529f\u80fd\u3002<\/p>\n<h2>\u8bfe\u540e\u601d\u8003<\/h2>\n<p>Spring \u52a8\u6001\u4ee3\u7406\u5931\u6548\u7684\u573a\u666f\u6709\u54ea\u4e9b\uff1fSpring Boot \u52a8\u6001\u4ee3\u7406\u9ed8\u8ba4\u5b9e\u73b0\u662f JDK \u52a8\u6001\u4ee3\u7406\u8fd8\u662f CGLIB\uff1f\u600e\u4e48\u8bc1\u660e\uff1f<\/p>\n<blockquote><p>\u672c\u6587\u5df2\u6536\u5f55\u5230\u6211\u7684\u9762\u8bd5\u5c0f\u7ad9 <a target=\"_blank\" href=\"https:\/\/link.segmentfault.com\/?enc=cjRFHwmM0p01cD0bT58y1A%3D%3D.jPG6mBunr2z7Y0WhUPa%2BIHR8EKjpzjeMCTjPh0aMGfA%3D\" rel=\"noopener noreferrer\">www.javacn.site<\/a>\uff0c\u5176\u4e2d\u5305\u542b\u7684\u5185\u5bb9\u6709\uff1aRedis\u3001JVM\u3001\u5e76\u53d1\u3001\u5e76\u53d1\u3001MySQL\u3001Spring\u3001Spring MVC\u3001Spring Boot\u3001Spring Cloud\u3001MyBatis\u3001\u8bbe\u8ba1\u6a21\u5f0f\u3001\u6d88\u606f\u961f\u5217\u7b49\u6a21\u5757\u3002<\/p><\/blockquote>\n<p><a href=\"https:\/\/segmentfault.com\/a\/1190000044682416\">https:\/\/segmentfault.com\/a\/1190000044682416<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>AOP\uff08Aspect-Oriented Programming\uff0c\u9762\u5411\u5207\u9762\u7f16\u7a0b\uff09\u662f\u4e00\u79cd\u7f16\u7a0b\u6280\u672f\uff0c\u5b83\u5141\u8bb8\u5f00\u53d1\u8005\u5728\u4e0d [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[4],"tags":[],"class_list":["post-10008425","post","type-post","status-publish","format-standard","hentry","category-share"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10008425","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10008425"}],"version-history":[{"count":0,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10008425\/revisions"}],"wp:attachment":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10008425"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10008425"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10008425"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}