{"id":2719,"date":"2018-04-29T08:38:47","date_gmt":"2018-04-29T00:38:47","guid":{"rendered":"http:\/\/www.xttblog.com\/?p=2719"},"modified":"2018-04-29T08:38:47","modified_gmt":"2018-04-29T00:38:47","slug":"spring-aop-%e9%9d%a2%e8%af%95%e9%9a%be%e7%82%b9%e6%80%bb%e7%bb%93","status":"publish","type":"post","link":"https:\/\/www.xttblog.com\/?p=2719","title":{"rendered":"Spring AOP \u9762\u8bd5\u96be\u70b9\u603b\u7ed3"},"content":{"rendered":"<p>\n\tSpring AOP \u7ecf\u5e38\u662f\u5404\u5927\u9762\u8bd5\u5b98\u5fc5\u63d0\u7684\u77e5\u8bc6\u70b9\u3002\u6211\u8fd9\u91cc\u603b\u7ed3\u4e86 AOP \u7684\u5404\u79cd\u5173\u952e\u77e5\u8bc6\u70b9\uff0c\u5e0c\u671b\u80fd\u5e2e\u52a9\u5230\u5927\u5bb6\u3002\n<\/p>\n<h2>\n\tAOP<br \/>\n<\/h2>\n<p>\n\t\u5b83\u80fd\u505a\u4ec0\u4e48\uff1a<strong>\u4e8b\u52a1<\/strong>\u3001<strong>\u5b89\u5168<\/strong>\u3001<strong>\u65e5\u5fd7<\/strong>\u3001<strong>\u7f13\u5b58<\/strong>.\n<\/p>\n<p>\n\t\u4ee5\u524d\u90fd\u662fOOP\u7684\u601d\u60f3\u4e0b\u7f16\u7a0b\u6a21\u578b\uff0c\u4e00\u5207\u90fd\u662f\u9762\u5411\u5bf9\u8c61\uff0c\u663e\u5f97\u4e95\u7136\u6709\u5e8f\uff0c\u4f46\u67d0\u4e00\u5929\u53d1\u73b0\u8bb8\u591a\u5bf9\u8c61\u903b\u8f91\u91cc\u6709\u76f8\u540c\u7684\u4ee3\u7801\u903b\u8f91\u6216\u7247\u6bb5\uff0c\u6bd4\u5982\u8bb0\u5f55\u65e5\u5fd7\u3001\u7528\u6237\u6743\u9650\u9a8c\u8bc1\uff0c\u4ed6\u4eec\u90fd\u662f\u505a\u540c\u6837\u7684\u4e8b\uff0c\u8fd9\u6837\uff0c\u7b2c\u4e00\u662f\u89c9\u5f97\u8fd9\u6837\u7684\u4ee3\u7801\u5bf9\u4e8e\u6709\u6d01\u7656\u7684\u4eba\u770b\u8d77\u6765\u662f\u5f88\u53cd\u611f\u7684\uff0c\u518d\u8005\u82e5\u8fd9\u4e9b\u4ee3\u7801\u6d89\u53ca\u6539\u52a8\u65f6\u6240\u6709\u7528\u5230\u7684\u70b9\u90fd\u8981\u6539\u3002\u4f46\u53c8\u53d1\u73b0\u8fd9\u4e9b\u9700\u8981\u690d\u5165\u7684\u5730\u65b9\u53ea\u662f\u4e00\u4e9b\u5173\u952e\u5730\u65b9\u3002\n<\/p>\n<p>\n\t\u90a3\u4e48\uff0c\u662f\u5426\u6709\u4e00\u79cd\u65b9\u6cd5\u4f7f\u8fd9\u4e9b\u65b9\u6cd5\u6267\u884c\u524d\u6216\u540e\u518d\u505a\u70b9\u4ec0\u4e48\u5462\u3002\u65b9\u6cd5\u662f\u6709\u7684\uff0c\u76ee\u524d\u6bd4\u5982\u6709\u76f4\u63a5\u5b57\u8282\u7801\u64cd\u4f5c\u3001\u4ee3\u7406\u6280\u672f\u7b49\u6765\u5b9e\u73b0\uff0c\u5b57\u8282\u7801\u6280\u672f\u5c31\u662f\u5728\u7f16\u8bd1\u65f6\u901a\u8fc7\u4fee\u6539\u5b57\u8282\u7801\u7684\u65b9\u5f0f\u5c06\u9700\u8981\u690d\u5165\u7684\u903b\u8f91\u7f16\u8bd1\u8fdb\u53bb\uff0c\u53ea\u662f\u6211\u4eec\u7f16\u5199\u65f6\u65e0\u9700\u7f16\u5199\u8fd9\u4e9b\u903b\u8f91\uff0c\u8fd9\u6837\u7684\u7f16\u8bd1\u690d\u5165\u8fc7\u7a0b\u4ea4\u7531\u4e00\u4e2a\u7f16\u8bd1\u5de5\u5177\u53bb\u505a\u3002\u4ee3\u7406\u6280\u672f\u4e0e\u76f4\u63a5\u5b57\u8282\u7801\u64cd\u4f5c\u6280\u672f\u7684\u4e0d\u540c\u70b9\u5728\u4e8e\u5b83\u4f1a\u53e6\u5916\u751f\u6210\u4e00\u4e2a\u4ee3\u7406\u7c7b\u5bf9\u76ee\u6807\u7c7b\u8fdb\u884c\u4ee3\u7406\uff0c\u7136\u540e\u5728\u65b9\u6cd5\u524d\u6216\u540e\u52a0\u5165\u5e0c\u671b\u7684\u903b\u8f91\u3002\n<\/p>\n<p>\n\tAspect\n<\/p>\n<p>\n\t\u5207\u9762\u5b9a\u4e49\uff0c\u5207\u9762\u91cc\u9762\u5305\u542b\u4e86\u5bf9\u690d\u5165\u70b9\uff0c\u690d\u5165\u903b\u8f91\u7684\u5b9a\u4e49\u3002Aspect\u662f\u4e00\u4e2a\u5207\u9762\u7684\u5b9a\u4e49\uff0c\u4e00\u4e2a\u5207\u9762\u91cc\u9762\u5b9a\u4e49\u4e86\u5207\u5165\u70b9\uff08JoinPoint\uff09\u3001\u5207\u5165\u65b9\u5f0f\uff08PointCut\uff09\u3001\u5207\u5165\u70b9\u7684\u903b\u8f91\uff08Advice\uff09\uff0c\u4e00\u4e2a@Aspect\u7c7b\u88ab\u89e3\u6790\u6210\u4e00\u4e2a\u5207\u9762\u3002\n<\/p>\n<p>\n\tAdvisor\n<\/p>\n<p>\n\tPointCut\u548cAdvice\u7684holder\uff0cPointCut\u662f\u7528\u6765\u7ec4\u7ec7\u5207\u9762\u7684\uff0cAdvice\u662f\u7528\u6765\u5728\u5207\u9762\u4e0a\u6267\u884c\u52a8\u4f5c\u7684\uff0cAdvice\u6709\u591a\u79cd\u52a8\u4f5c\u65b9\u5f0f\u5982\u524d\u7f6e\u3001\u540e\u7f6e\u3001\u524d\u540e\u7f6e\u5904\u7406\u7b49\u65b9\u5f0f\u3002\n<\/p>\n<p>\n\tn&#039;gPointcut\n<\/p>\n<p>\n\t\u5bf9\u65b9\u6cd5\u7684\u5339\u914d\uff0c\u5728Spring\u4e2d\u4e00\u822c\u7528\u8868\u8fbe\u5f0f\u8868\u8fbe\u662f\u8868\u793a [return type] [class and method name]([args])\uff0c\u5339\u914d\u8868\u8fbe\u5f0f\u6307\u5b9a\u4e86\u54ea\u4e9b\u5730\u65b9\u4f1a\u88ab\u690d\u5165\u5207\u9762\u903b\u8f91\n<\/p>\n<p>\n\tJoinpoint\n<\/p>\n<p>\n\t\u5177\u4f53\u7684\u903b\u8f91\u70b9\u3002\n<\/p>\n<p>\n\tAdvice\n<\/p>\n<p>\n\t\u662f\u5bf9\u5177\u4f53\u690d\u5165\u52a8\u4f5c\u884c\u4e3a\u7684\u5b9a\u4e49\uff0c\u4e5f\u5305\u62ec\u62e6\u622a\u7684\u65b9\u5f0f<br \/>\n\t\u901a\u77e5\uff0c\u8fd9\u91cc\u5c31\u662f\u5b9a\u4e49\u8981\u690d\u5165\u903b\u8f91\u7684\u5730\u65b9\uff0c\u690d\u5165\u4e5f\u6309\u6267\u884c\u65f6\u95f4\u5206\u4e3a\u8bb8\u591a\u79cd\n<\/p>\n<ol>\n<li>\n\t\tBeforeAdivce \u524d\u7f6e\u901a\u77e5\n\t<\/li>\n<li>\n\t\tAfterAdivce \u540e\u7f6e\u901a\u77e5\n\t<\/li>\n<li>\n\t\tAround \u73af\u7ed5\u901a\u77e5\n\t<\/li>\n<li>\n\t\tThrowAdvice \u5f02\u5e38\u901a\u77e5\n\t<\/li>\n<\/ol>\n<h2>\n\tAOP\u690d\u5165\u539f\u7406<br \/>\n<\/h2>\n<ol>\n<li>\n\t\t\u521d\u59cb\u5316BeanDefinition\n\t<\/li>\n<li>\n\t\tcreateBean\n\t<\/li>\n<li>\n\t\tinitBean\n\t<\/li>\n<li>\n\t\tfind Advisors\n\t<\/li>\n<li>\n\t\tmatch JoinPoint\n\t<\/li>\n<li>\n\t\tcreate Proxy\n\t<\/li>\n<\/ol>\n<p>\n\t\u627e\u5230 Advisors\u3002\n<\/p>\n<pre class=\"brush:java;\">\r\nBeanFactoryAdvisorRetrivelHelper.findAdvisorBeans();<\/pre>\n<p>\n\tAopUtils\n<\/p>\n<pre class=\"brush:java;\">\r\n\/\/ \u627e\u5230\u5339\u914d\u7684\u5207\u9762\r\nfindAdvisorsThatCanApply(advisors, target);\r\n\r\n\/\/ \u5224\u65ad beanClass \u662f\u5426\u53ef\u4ee5\u88ab\u8fd9\u4e2a\u5207\u9762\u4ee3\u7406\r\nAopUtils.canApply(pointCut, beanClass);\r\n    MethodMatcher.matches\r\n    AnnotationMethodMatcher   \/\/ \u6ce8\u89e3\u6807\u8bb0\u7684\u89e3\u6790\uff0c\u6709\u76ee\u6807\u6ce8\u89e3\u7684\r\n    AspectJExpressionPointcut \/\/ \u8868\u8fbe\u5f0f\u6807\u8bb0\u7684\u5207\u9762\u5339\u914d<\/pre>\n<h3 id=\"\u8868\u8fbe\u5f0f\">\n\t\u8868\u8fbe\u5f0f<br \/>\n<\/h3>\n<p>\n\tSpring\u4f7f\u7528AspectJ\u5b9a\u4e49\u7684\u5207\u9762\u8868\u8fbe\u5f0f\uff0c\u867d\u7136\u4f7f\u7528\u4e86AspectJ\u7684AOP\u89c4\u8303\uff0c\u4f46\u5e76\u672a\u4f7f\u7528AspectJ\u7684\u5b9e\u73b0\uff0cAspectJ\u662f\u9759\u6001\u7f16\u8bd1\uff0c\u6709\u81ea\u5df1\u7684DSL\uff0c\u4f7f\u7528\u8005\u9700\u8981\u5b66\u4e60\u5176AOP\u8bed\u8a00\uff0c\u4f46\u662f\u5b66\u4e60\u6210\u672c\u5e76\u4e0d\u5927\uff0c\u53ea\u662f\u4e00\u4e9b\u5bf9\u5207\u5165\u70b9\u7684\u63cf\u8ff0\u5b9a\u4e49\uff0c\u8be6\u7ec6\u7f16\u5199\u8bed\u6cd5\u53ef\u89c1AspectJ\u6587\u6863\n<\/p>\n<h3 id=\"\u4ee3\u7406\">\n\t\u4ee3\u7406<br \/>\n<\/h3>\n<p>\n\t\u4ee3\u7406\u5206\u4e3a\u52a8\u6001\u4ee3\u7406\u548c\u9759\u6001\u4ee3\u7406\uff0c\u52a8\u6001\u4ee3\u7406\u6307\u7684\u662fjdk\u52a8\u6001\u4ee3\u7406\uff0c\u4f46jdk\u52a8\u6001\u4ee3\u7406\u5fc5\u987b\u662f\u57fa\u4e8e\u63a5\u53e3\u7684\uff0c\u5229\u7528\u4e86jdk\u52a8\u6001\u751f\u6210\u5185\u5b58\u5b57\u8282\u7801\u4ee3\u7406\u7c7b\u7684\u65b9\u5f0f\uff1b\u53e6\u4e00\u53ea\u5c31\u662fCGLIB\u9759\u6001\u4ee3\u7406\uff0c\u8fd9\u79cd\u4ee3\u7406\u9002\u7528\u4e8e\u5bf9\u4efb\u4f55\u7c7b\u7684\u4ee3\u7406\uff0c\u5b83\u662f\u5229\u7528\u7ee7\u627f\u7684\u80fd\u529b\uff0c\u4ee3\u7406\u539f\u6709\u7c7b\u7684\u65b9\u6cd5\uff0c\u5e76\u690d\u5165\u903b\u8f91<\/p>\n<!-- <audio autoplay=\"autoplay\" height=\"100\" width=\"100\" hidden=\"true\"><source src=\"https:\/\/c.coka.la\/orAvPR.mp3\" type=\"audio\/mp3\"\/><\/audio><img decoding=\"async\" src=\"https:\/\/images.weserv.nl\/?url=https:\/\/ws3.sinaimg.cn\/large\/8fa5dcfcgy1g4em3rzar5j20ib089ae0.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/> --><p><!-- <img decoding=\"async\" src=\"https:\/\/raw.githubusercontent.com\/xmt1139057136\/xttblog\/master\/8fa5dcfcgy1g4em3rzar5j20ib089ae0.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/>--><img decoding=\"async\" src=\"https:\/\/saas.51autoparts.cn\/api\/admin\/sys-file\/oss\/file?fileName=84db51f8561e448f9a4fda2eff0811f9.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/><\/p><p>\u6700\u540e\uff0c\u6b22\u8fce\u5173\u6ce8\u6211\u7684\u4e2a\u4eba\u5fae\u4fe1\u516c\u4f17\u53f7\uff1a\u4e1a\u4f59\u8349\uff08yyucao\uff09\uff01<b>\u53ef\u52a0\u4f5c\u8005\u5fae\u4fe1\u53f7\uff1a<font color=\"red\">xttblog2<\/font>\u3002\u5907\u6ce8\uff1a\u201c1\u201d\uff0c\u6dfb\u52a0\u535a\u4e3b\u5fae\u4fe1\u62c9\u4f60\u8fdb\u5fae\u4fe1\u7fa4\u3002\u5907\u6ce8\u9519\u8bef\u4e0d\u4f1a\u540c\u610f\u597d\u53cb\u7533\u8bf7\u3002<\/b>\u518d\u6b21\u611f\u8c22\u60a8\u7684\u5173\u6ce8\uff01\u540e\u7eed\u6709\u7cbe\u5f69\u5185\u5bb9\u4f1a\u7b2c\u4e00\u65f6\u95f4\u53d1\u7ed9\u60a8\uff01\u539f\u521b\u6587\u7ae0\u6295\u7a3f\u8bf7\u53d1\u9001\u81f3532009913@qq.com\u90ae\u7bb1\u3002\u5546\u52a1\u5408\u4f5c\u4e5f\u53ef\u6dfb\u52a0\u4f5c\u8005\u5fae\u4fe1\u8fdb\u884c\u8054\u7cfb\uff01<\/p><p>\u672c\u6587\u539f\u6587\u51fa\u5904\uff1a<a href=\"https:\/\/www.xttblog.com\">\u4e1a\u4f59\u8349<\/a>\uff1a &raquo; <a href=\"https:\/\/www.xttblog.com\/?p=2719\">Spring AOP \u9762\u8bd5\u96be\u70b9\u603b\u7ed3<\/a><\/p><!-- <div class=\"bd-reward-stl\"><button id=\"bdRewardBtn\"><span><\/span><\/button><\/div> -->","protected":false},"excerpt":{"rendered":"<p>Spring AOP \u7ecf\u5e38\u662f\u5404\u5927\u9762\u8bd5\u5b98\u5fc5\u63d0\u7684\u77e5\u8bc6\u70b9\u3002\u6211\u8fd9\u91cc\u603b\u7ed3\u4e86 AOP \u7684\u5404\u79cd\u5173\u952e\u77e5\u8bc6\u70b9\uff0c\u5e0c\u671b\u80fd\u5e2e\u52a9\u5230\u5927\u5bb6\u3002 [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[1511,1625,713,551,587],"class_list":["post-2719","post","type-post","status-publish","format-standard","hentry","category-java","tag-aop","tag-aspectj","tag-maven","tag-spring","tag-spring-boot"],"_links":{"self":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/2719","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2719"}],"version-history":[{"count":1,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/2719\/revisions"}],"predecessor-version":[{"id":2720,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/2719\/revisions\/2720"}],"wp:attachment":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2719"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2719"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2719"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}