{"id":2124,"date":"2017-10-11T15:23:31","date_gmt":"2017-10-11T07:23:31","guid":{"rendered":"http:\/\/www.xttblog.com\/?p=2124"},"modified":"2017-10-11T15:23:31","modified_gmt":"2017-10-11T07:23:31","slug":"spring-aop%e5%8e%9f%e7%90%86%e5%92%8c%e5%ae%9e%e6%88%98","status":"publish","type":"post","link":"https:\/\/www.xttblog.com\/?p=2124","title":{"rendered":"Spring AOP\u539f\u7406\u548c\u5b9e\u6218"},"content":{"rendered":"<p>\n\tAOP\u7684\u5b9e\u73b0\uff0c\u76f8\u6bd4xml\u914d\u7f6e\uff0c\u57fa\u4e8e\u6ce8\u89e3\u7684\u65b9\u5f0f\u66f4\u52a0\u7b80\u6d01\u65b9\u4fbf\u3002\u4e5f\u662f\u65e5\u5e38\u5f00\u53d1\u4e2d\u5e38\u7528\u7684\u505a\u6cd5\u3002\n<\/p>\n<p>\n\t\u4e0b\u9762\u6211\u4eec\u4e00\u8d77\u6765\u770b\u4e00\u4e2a\u4f8b\u5b50\u3002\n<\/p>\n<pre class=\"brush:java;\">\r\n@Aspect\r\npublic class TransactionDemo {\r\n    @Pointcut(value=&quot;execution(* com.yangxin.core.service.*.*.*(..))&quot;)\r\n    public void point(){\r\n        \/\/ \u4e1a\u4f59\u8349\uff1awww.xttblog.com\r\n    }\r\n    @Before(value=&quot;point()&quot;)\r\n    public void before(){\r\n        System.out.println(&quot;transaction begin&quot;);\r\n    }\r\n    @AfterReturning(value = &quot;point()&quot;)\r\n    public void after(){\r\n        System.out.println(&quot;transaction commit&quot;);\r\n    }\r\n    @Around(&quot;point()&quot;)\r\n    public void around(ProceedingJoinPoint joinPoint) throws Throwable{\r\n        System.out.println(&quot;transaction begin&quot;);\r\n        joinPoint.proceed();\r\n        System.out.println(&quot;transaction commit&quot;);\r\n    }\r\n}<\/pre>\n<p>\n\t\u5728applicationContext.xml\u4e2d\u914d\u7f6e\u3002\n<\/p>\n<pre class=\"brush:xml;\">\r\n&lt;aop:aspectj-autoproxy \/&gt;\r\n&lt;bean id = &quot;transactionDemo&quot; class = &quot;com.yangxin.core.transaction.TransactionDemo&quot; \/&gt;<\/pre>\n<p>\n\t\u5c31\u662f\u4ee5\u4e0a\u8fd9\u4e48\u7b80\u5355\u7684\u914d\u7f6e\uff0c\u5c31\u53ef\u4ee5\u5b9e\u73b0AOP\u7684\u529f\u80fd\u3002\n<\/p>\n<h2>\n\tSpring Aop\u539f\u7406<br \/>\n<\/h2>\n<p>\n\t\u901a\u8fc7\u524d\u9762\u4ecb\u7ecd\u53ef\u4ee5\u77e5\u9053\uff1aAOP \u4ee3\u7406\u5176\u5b9e\u662f\u7531 AOP \u6846\u67b6\u52a8\u6001\u751f\u6210\u7684\u4e00\u4e2a\u5bf9\u8c61\uff0c\u8be5\u5bf9\u8c61\u53ef\u4f5c\u4e3a\u76ee\u6807\u5bf9\u8c61\u4f7f\u7528\u3002AOP \u4ee3\u7406\u5305\u542b\u4e86\u76ee\u6807\u5bf9\u8c61\u7684\u5168\u90e8\u65b9\u6cd5\uff0c\u4f46 AOP \u4ee3\u7406\u4e2d\u7684\u65b9\u6cd5\u4e0e\u76ee\u6807\u5bf9\u8c61\u7684\u65b9\u6cd5\u5b58\u5728\u5dee\u5f02\uff1aAOP \u65b9\u6cd5\u5728\u7279\u5b9a\u5207\u5165\u70b9\u6dfb\u52a0\u4e86\u589e\u5f3a\u5904\u7406\uff0c\u5e76\u56de\u8c03\u4e86\u76ee\u6807\u5bf9\u8c61\u7684\u65b9\u6cd5\u3002\n<\/p>\n<p>\n\t<img decoding=\"async\" alt=\"Spring \u7684 AOP \u4ee3\u7406\" src=\"http:\/\/img.bbs.csdn.net\/upload\/201710\/11\/1507703672_34982.png\" style=\"width: 300px;height: 246px\" \/>\n<\/p>\n<p>\n\tSpring \u7684 AOP \u4ee3\u7406\u7531 Spring \u7684 IoC \u5bb9\u5668\u8d1f\u8d23\u751f\u6210\u3001\u7ba1\u7406\uff0c\u5176\u4f9d\u8d56\u5173\u7cfb\u4e5f\u7531 IoC \u5bb9\u5668\u8d1f\u8d23\u7ba1\u7406\u3002\u56e0\u6b64\uff0cAOP \u4ee3\u7406\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u5bb9\u5668\u4e2d\u7684\u5176\u4ed6 Bean \u5b9e\u4f8b\u4f5c\u4e3a\u76ee\u6807\uff0c\u8fd9\u79cd\u5173\u7cfb\u53ef\u7531 IoC \u5bb9\u5668\u7684\u4f9d\u8d56\u6ce8\u5165\u63d0\u4f9b\u3002\n<\/p>\n<p>\n\tAop\u5f00\u53d1\u65f6\uff0c\u5176\u4e2d\u9700\u8981\u7a0b\u5e8f\u5458\u53c2\u4e0e\u7684\u53ea\u6709 3 \u4e2a\u90e8\u5206\uff1a\n<\/p>\n<ul>\n<li>\n\t\t\u5b9a\u4e49\u666e\u901a\u4e1a\u52a1\u7ec4\u4ef6\u3002\n\t<\/li>\n<li>\n\t\t\u5b9a\u4e49\u5207\u5165\u70b9\uff0c\u4e00\u4e2a\u5207\u5165\u70b9\u53ef\u80fd\u6a2a\u5207\u591a\u4e2a\u4e1a\u52a1\u7ec4\u4ef6\u3002\n\t<\/li>\n<li>\n\t\t\u5b9a\u4e49\u589e\u5f3a\u5904\u7406\uff0c\u589e\u5f3a\u5904\u7406\u5c31\u662f\u5728 AOP \u6846\u67b6\u4e3a\u666e\u901a\u4e1a\u52a1\u7ec4\u4ef6\u7ec7\u5165\u7684\u5904\u7406\u52a8\u4f5c\u3002\n\t<\/li>\n<\/ul>\n<p>\n\t\u4e3a\u4e86\u7406\u6e05\u5173\u7cfb\uff0c\u5148\u6765\u4e2a\u7c7b\u5173\u7cfb\u56fe\u3002\n<\/p>\n<p>\n\t<img decoding=\"async\" alt=\"Spring \u7684 AOP\" src=\"http:\/\/img.bbs.csdn.net\/upload\/201710\/11\/1507706297_945647.png\" style=\"width: 1240px;height: 995px\" \/>\n<\/p>\n<h2>\n\t\u4e24\u79cd\u52a8\u6001\u4ee3\u7406\u65b9\u5f0f<br \/>\n<\/h2>\n<p>\n\tSpring\u9ed8\u8ba4\u91c7\u53d6\u7684\u52a8\u6001\u4ee3\u7406\u673a\u5236\u5b9e\u73b0AOP\uff0c\u5f53\u52a8\u6001\u4ee3\u7406\u4e0d\u53ef\u7528\u65f6\uff08\u4ee3\u7406\u7c7b\u65e0\u63a5\u53e3\uff09\u4f1a\u4f7f\u7528CGlib\u673a\u5236\u3002\n<\/p>\n<p>\n\tSpring\u63d0\u4f9b\u4e86\u4e24\u79cd\u65b9\u5f0f\u6765\u751f\u6210\u4ee3\u7406\u5bf9\u8c61: JDKProxy\u548cCglib\uff0c\u5177\u4f53\u4f7f\u7528\u54ea\u79cd\u65b9\u5f0f\u751f\u6210\u7531AopProxyFactory\u6839\u636eAdvisedSupport\u5bf9\u8c61\u7684\u914d\u7f6e\u6765\u51b3\u5b9a\u3002\u9ed8\u8ba4\u7684\u7b56\u7565\u662f\u5982\u679c\u76ee\u6807\u7c7b\u662f\u63a5\u53e3\uff0c\u5219\u4f7f\u7528JDK\u52a8\u6001\u4ee3\u7406\u6280\u672f\uff0c\u5426\u5219\u4f7f\u7528Cglib\u6765\u751f\u6210\u4ee3\u7406\u3002\n<\/p>\n<h2>\n\tJDK\u52a8\u6001\u4ee3\u7406<br \/>\n<\/h2>\n<ul>\n<li>\n\t\tJDK\u52a8\u6001\u4ee3\u7406\u4e3b\u8981\u6d89\u53ca\u5230java.lang.reflect\u5305\u4e2d\u7684\u4e24\u4e2a\u7c7b\uff1aProxy\u548cInvocationHandler\u3002InvocationHandler\u662f\u4e00\u4e2a\u63a5\u53e3\uff0c\u901a\u8fc7\u5b9e\u73b0\u8be5\u63a5\u53e3\u5b9a\u4e49\u6a2a\u5207\u903b\u8f91\uff0c\u5e76\u901a\u8fc7\u53cd\u5c04\u673a\u5236\u8c03\u7528\u76ee\u6807\u7c7b\u7684\u4ee3\u7801\uff0c\u52a8\u6001\u5c06\u6a2a\u5207\u903b\u8f91\u548c\u4e1a\u52a1\u903b\u8f91\u7f16\u5236\u5728\u4e00\u8d77\u3002\n\t<\/li>\n<li>\n\t\tProxy\u5229\u7528InvocationHandler\u52a8\u6001\u521b\u5efa\u4e00\u4e2a\u7b26\u5408\u67d0\u4e00\u63a5\u53e3\u7684\u5b9e\u4f8b\uff0c\u751f\u6210\u76ee\u6807\u7c7b\u7684\u4ee3\u7406\u5bf9\u8c61\u3002\n\t<\/li>\n<\/ul>\n<h2>\n\tCGLib\u52a8\u6001\u4ee3\u7406<br \/>\n<\/h2>\n<p>\n\tCGLib\u5168\u79f0\u4e3aCode Generation Library\uff0c\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u9ad8\u6027\u80fd\uff0c\u9ad8\u8d28\u91cf\u7684\u4ee3\u7801\u751f\u6210\u7c7b\u5e93\uff0c\u53ef\u4ee5\u5728\u8fd0\u884c\u671f\u6269\u5c55Java\u7c7b\u4e0e\u5b9e\u73b0Java\u63a5\u53e3\uff0cCGLib\u5c01\u88c5\u4e86asm\uff0c\u53ef\u4ee5\u518d\u8fd0\u884c\u671f\u52a8\u6001\u751f\u6210\u65b0\u7684class\u3002\u548cJDK\u52a8\u6001\u4ee3\u7406\u76f8\u6bd4\u8f83\uff1aJDK\u521b\u5efa\u4ee3\u7406\u6709\u4e00\u4e2a\u9650\u5236\uff0c\u5c31\u662f\u53ea\u80fd\u4e3a\u63a5\u53e3\u521b\u5efa\u4ee3\u7406\u5b9e\u4f8b\uff0c\u800c\u5bf9\u4e8e\u6ca1\u6709\u901a\u8fc7\u63a5\u53e3\u5b9a\u4e49\u4e1a\u52a1\u65b9\u6cd5\u7684\u7c7b\uff0c\u5219\u53ef\u4ee5\u901a\u8fc7CGLib\u521b\u5efa\u52a8\u6001\u4ee3\u7406\u3002<\/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=2124\">Spring AOP\u539f\u7406\u548c\u5b9e\u6218<\/a><\/p><!-- <div class=\"bd-reward-stl\"><button id=\"bdRewardBtn\"><span><\/span><\/button><\/div> -->","protected":false},"excerpt":{"rendered":"<p>AOP\u7684\u5b9e\u73b0\uff0c\u76f8\u6bd4xml\u914d\u7f6e\uff0c\u57fa\u4e8e\u6ce8\u89e3\u7684\u65b9\u5f0f\u66f4\u52a0\u7b80\u6d01\u65b9\u4fbf\u3002\u4e5f\u662f\u65e5\u5e38\u5f00\u53d1\u4e2d\u5e38\u7528\u7684\u505a\u6cd5\u3002 \u4e0b\u9762\u6211\u4eec\u4e00\u8d77\u6765\u770b\u4e00\u4e2a\u4f8b\u5b50 [&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,69,551,1041],"class_list":["post-2124","post","type-post","status-publish","format-standard","hentry","category-java","tag-aop","tag-java","tag-spring","tag-1041"],"_links":{"self":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/2124","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=2124"}],"version-history":[{"count":1,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/2124\/revisions"}],"predecessor-version":[{"id":2125,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/2124\/revisions\/2125"}],"wp:attachment":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2124"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2124"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2124"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}