{"id":1428,"date":"2016-10-27T21:28:27","date_gmt":"2016-10-27T13:28:27","guid":{"rendered":"http:\/\/coderbee.net\/?p=1428"},"modified":"2016-10-27T21:28:48","modified_gmt":"2016-10-27T13:28:48","slug":"spring-aop-%e4%b8%8e-%e4%ba%8b%e5%8a%a1%e5%ae%9e%e7%8e%b0","status":"publish","type":"post","link":"https:\/\/coderbee.net\/index.php\/framework\/20161027\/1428","title":{"rendered":"Spring AOP \u4e0e \u4e8b\u52a1\u5b9e\u73b0"},"content":{"rendered":"<h1>1.  AOP \u4e0e\u65b9\u6cd5\u8c03\u7528<\/h1>\n<p>\u5bf9\u4e8e \u63a5\u53e3 interface\uff0c\u53ef\u4ee5\u901a\u8fc7 <code>java.lang.reflect.Proxy<\/code> \u6765\u751f\u6210\u52a8\u6001\u4ee3\u7406\u5bf9\u8c61\uff1b<br \/>\n\u5bf9\u4e8e \u7c7b class\uff0c\u53ef\u4ee5\u901a\u8fc7\u5b57\u8282\u7801\u6280\u672f\uff0c\u5982 CBLIB \u6765\u751f\u6210\u4e00\u4e2a\u5b50\u7c7b\u4f5c\u4e3a\u4ee3\u7406\u5bf9\u8c61\uff0c\u6b64\u65f6\u7c7b\u4e0d\u80fd\u58f0\u660e\u4e3a final \u3002<\/p>\n<p>\u5f53 Spring \u628a Bean \u6ce8\u5165\u5230\u53e6\u4e00\u4e2a Bean \u65f6\uff0c\u5176\u5b9e\u6ce8\u5165\u7684\u662f\u5b83\u751f\u6210\u7684\u4ee3\u7406\u5bf9\u8c61\uff0c\u8fdb\u884c\u65b9\u6cd5\u8c03\u7528\u65f6\uff0c\u9996\u5148\u8c03\u7528\u7684\u662f\u4ee3\u7406\u5bf9\u8c61\u4e0a\u7684\u65b9\u6cd5\uff0c\u4ee3\u7406\u5bf9\u8c61\u7684\u65b9\u6cd5\u6700\u7ec8\u518d\u8c03\u76ee\u6807\u5bf9\u8c61\u7684\u65b9\u6cd5\uff0c\u4e5f\u5c31\u662f\u5f00\u53d1\u4eba\u5458\u7f16\u5199\u7684\u65b9\u6cd5\u3002Spring \u901a\u8fc7\u5728\u8c03\u7528\u76ee\u6807\u65b9\u6cd5\u524d\u540e\u505a\u5904\u7406\u6765\u5b9e\u73b0\u4e00\u4e9b\u7279\u6027\uff0c\u4f8b\u5982\u4e8b\u52a1\u7ba1\u7406\u3001\u7f13\u5b58\u7b49\u3002<\/p>\n<p><strong>Spring \u7684 AOP \u662f\u57fa\u4e8e\u5bf9\u4ee3\u7406\u5bf9\u8c61\u7684\u65b9\u6cd5\u8c03\u7528\u7684\u62e6\u622a\u7684\uff0c\u53ea\u80fd\u62e6\u622a\u5916\u90e8\u5bf9\u8c61 \u5bf9 \u67d0\u4e2a\u5bf9\u8c61\u7684\u65b9\u6cd5\u7684\u8c03\u7528\uff0c\u5bf9\u8c61\u7684\u65b9\u6cd5\u8c03\u7528\u540c\u4e00\u4e2a\u7c7b\u7684\u65b9\u6cd5\u662f\u4e0d\u4f1a\u88ab\u62e6\u622a\u7684\u3002<\/strong><\/p>\n<pre><code class=\"java\">@Service\npublic class SpringTxService {\n    private static final Logger LOGGER = LoggerFactory\n            .getLogger(SpringTxService.class);\n\n    @Transactional(propagation = Propagation.REQUIRED)\n    public void add() {\n        LOGGER.info(\"do add\");\n    }\n\n    @Transactional(propagation = Propagation.REQUIRES_NEW)\n    public void requireNew() {\n        LOGGER.info(\"do requireNew\");\n    }\n\n    public void composite() {\n        add();\n        requireNew();\n    }\n}\n<\/code><\/pre>\n<p>\u4e0a\u9762\u7684\u4ee3\u7801\u662f\u57fa\u4e8e\u6ce8\u89e3\u8fdb\u884c\u4e8b\u52a1\u63a7\u5236\u7684\uff0ccomposite \u65b9\u6cd5\u6ca1\u6709\u58f0\u660e\u4e8b\u52a1\u5c5e\u6027\uff0c\u5b83\u4f1a\u8c03\u7528 add\u3001 requireNew<br \/>\n \u65b9\u6cd5\u3002\u5f53\u628a SpringTxService \u6ce8\u5165\u5230\u53e6\u4e00\u4e2a BeanB\uff1a<br \/>\n1. \u5728 BeanB \u7684\u65b9\u6cd5\u91cc\u8c03\u7528\u4ee3\u7406\u5bf9\u8c61 composite \u65f6\uff0c\u6700\u7ec8\u6267\u884c add\u3001 requireNew \u662f\u6ca1\u6709\u5728\u4e8b\u52a1\u91cc\u6267\u884c\u7684\uff0c\u53ea\u662f\u666e\u901a\u7684\u65b9\u6cd5\u8c03\u7528\u3002<br \/>\n2. \u5728 BeanB \u7684\u65b9\u6cd5\u91cc\u8c03\u7528\u4ee3\u7406\u5bf9\u8c61\u7684 add\u3001 requireNew \u65b9\u6cd5\u65f6\uff0c\u8fd9\u4e24\u4e2a\u65b9\u6cd5\u90fd\u4f1a\u5206\u522b\u5728\u76f8\u5e94\u7684\u4e8b\u52a1\u91cc\u6267\u884c\u3002<\/p>\n<p><strong>\u5f53\u53d1\u73b0\u57fa\u4e8e AOP \u5b9e\u73b0\u7684\u7279\u6027\u6ca1\u6709\u9884\u671f\u7684\u6548\u679c\u65f6\uff0c\u4e00\u5b9a\u8981\u770b\u770b\u662f\u4e0d\u662f\u5728\u4ee3\u7406\u5bf9\u8c61\u4e0a\u8c03\u7528\u8fd8\u662f\u76ee\u6807\u7c7b\u5185\u90e8\u7684\u65b9\u6cd5\u8c03\u7528\u3002<\/strong><\/p>\n<p><!--more--><\/p>\n<h1>2.  \u57fa\u4e8e JDBC \u7684 Spring \u4e8b\u52a1\u5b9e\u73b0<\/h1>\n<p>\u4ee3\u7801\u903b\u8f91\u5728<br \/>\norg.springframework.transaction.support.AbstractPlatformTransactionManager \u7c7b\u7684\u65b9\u6cd5\uff1a private TransactionStatus handleExistingTransaction(TransactionDefinition definition, Object transaction, boolean debugEnabled) throws TransactionException;<\/p>\n<ul>\n<li>\n<p>PROPAGATION_REQUIRED<\/p>\n<blockquote>\n<p>\u7279\u70b9\uff1a \u5982\u679c\u5f53\u524d\u7ebf\u7a0b\u5173\u8054\u4e86\u4e8b\u52a1\uff0c\u5219\u5728\u5f53\u524d\u4e8b\u52a1\u91cc\u6267\u884c\uff0c\u5426\u5219\u65b0\u5efa\u4e00\u4e2a\u4e8b\u52a1\u3002<br \/>\n   \u5b9e\u73b0\uff1a \u5171\u7528\u4e00\u4e2a\u6570\u636e\u5e93\u8fde\u63a5\u3001\u4e8b\u52a1\u3002<\/p>\n<\/blockquote>\n<\/li>\n<li>\n<p>PROPAGATION_REQUIRES_NEW<\/p>\n<blockquote>\n<p>\u7279\u70b9\uff1a \u5982\u679c\u5f53\u524d\u7ebf\u7a0b\u5df2\u5173\u8054\u4e86\u4e8b\u52a1\uff0c\u5219\u628a\u5f53\u524d\u4e8b\u52a1\u6302\u8d77\uff0c\u7136\u540e\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u4e8b\u52a1\uff0c\u5728\u65b0\u7684\u4e8b\u52a1\u91cc\u6267\u884c\u65b9\u6cd5\u3002<br \/>\n   \u5b9e\u73b0\uff1a Spring \u7528 TransactionSynchronizationManager \u6301\u6709\u4e00\u4e9b ThreadLocal \u5c5e\u6027\uff0c\u7528\u4e8e\u4fdd\u5b58\u5f53\u524d\u7ebf\u7a0b\u6240\u6301\u6709\u4e8b\u52a1\u7684\u5c5e\u6027\u3002 \u628a\u5f53\u524d\u4e8b\u52a1\u5728 TransactionSynchronizationManager \u91cc\u7684\u5c5e\u6027\u4fdd\u5b58\u8d77\u6765\uff0c\u7136\u540e\u83b7\u53d6\u4e00\u4e2a\u65b0\u7684\u6570\u636e\u5e93\u8fde\u63a5\uff0c\u5f00\u542f\u4e00\u4e2a\u65b0\u7684\u4e8b\u52a1\uff0c\u8fd9\u4e2a\u65b0\u4e8b\u7269\u6267\u884c\u5b8c\u540e\u518d\u6062\u590d\u539f\u6709\u7684\u4e8b\u52a1\u3002<\/p>\n<\/blockquote>\n<\/li>\n<li>\n<p>PROPAGATION_NESTED<\/p>\n<blockquote>\n<p>\u7279\u70b9\uff1a \u5982\u679c\u5f53\u524d\u5b58\u5728\u4e8b\u52a1\uff0c\u5219\u5728\u5d4c\u5957\u4e8b\u52a1\u5185\u6267\u884c\u3002\u5982\u679c\u5f53\u524d\u6ca1\u6709\u4e8b\u52a1\uff0c\u5219\u8fdb\u884c\u4e0e PROPAGATION_REQUIRED \u7c7b\u4f3c\u7684\u64cd\u4f5c\u3002<br \/>\n   \u5b9e\u73b0\uff1a \u901a\u8fc7\u5728\u5f53\u524d\u4e8b\u52a1\u7684 JDBC \u8fde\u63a5\u4e0a\u521b\u5efa SavePoint \u6765\u5b9e\u73b0\uff0c\u8fd9\u4e2a SavePoint  \u53ef\u4ee5\u5355\u72ec\u56de\u6eda\uff0c\u4e5f\u53ef\u4ee5\u8ddf\u5916\u90e8\u4e8b\u52a1\u4e00\u8d77\u56de\u6eda\u6216\u63d0\u4ea4\uff0c\u4f46\u4e0d\u80fd\u5355\u72ec\u63d0\u4ea4\u3002<\/p>\n<\/blockquote>\n<\/li>\n<li>\n<p>PROPAGATION_NEVER<\/p>\n<blockquote>\n<p>\u7279\u70b9\uff1a \u5982\u679c\u5f53\u524d\u7ebf\u7a0b\u5173\u8054\u4e86\u4e8b\u52a1\uff0c\u5219\u629b\u51fa IllegalTransactionStateException \u3002<br \/>\n   \u5b9e\u73b0\uff1a \u68c0\u6d4b\u5230\u65b9\u6cd5\u7684\u4e8b\u52a1\u5c5e\u6027\u662f PROPAGATION_NEVER \u65f6\uff0c\u76f4\u63a5\u629b\u51fa\u5f02\u5e38\u3002<\/p>\n<\/blockquote>\n<\/li>\n<li>\n<p>PROPAGATION_SUPPORTS \uff1a<\/p>\n<blockquote>\n<p>\u7279\u70b9\uff1a \u652f\u6301\u5f53\u524d\u4e8b\u52a1\uff0c\u5982\u679c\u5f53\u524d\u6ca1\u6709\u4e8b\u52a1\uff0c\u5c31\u4ee5\u975e\u4e8b\u52a1\u65b9\u5f0f\u6267\u884c\u3002<br \/>\n   \u5b9e\u73b0\uff1a \u5176\u5b9e\u662f\u4e0d\u9700\u8981\u505a\u7279\u6b8a\u5904\u7406\u3002<\/p>\n<\/blockquote>\n<\/li>\n<li>\n<p>PROPAGATION_NOT_SUPPORTED \uff1a<\/p>\n<blockquote>\n<p>\u7279\u70b9\uff1a \u4ee5\u975e\u4e8b\u52a1\u65b9\u5f0f\u6267\u884c\u64cd\u4f5c\uff0c\u5982\u679c\u5f53\u524d\u5b58\u5728\u4e8b\u52a1\uff0c\u5c31\u628a\u5f53\u524d\u4e8b\u52a1\u6302\u8d77\u3002<br \/>\n   \u5b9e\u73b0\uff1a \u4f1a\u628a\u5f53\u524d\u5173\u8054\u7684\u4e8b\u52a1\u6302\u8d77\uff0c\u7136\u540e\u7ee7\u7eed\u6267\u884c\uff0c\u6267\u884c\u5b8c\u540e\u518d\u6062\u590d\u6302\u8d77\u7684\u4e8b\u52a1\u3002<\/p>\n<\/blockquote>\n<\/li>\n<\/ul>\n<hr\/>\n<p>\u6b22\u8fce\u5173\u6ce8\u6211\u7684\u5fae\u4fe1\u516c\u4f17\u53f7: <strong>coderbee\u7b14\u8bb0<\/strong>\uff0c\u53ef\u4ee5\u66f4\u53ca\u65f6\u56de\u590d\u4f60\u7684\u8ba8\u8bba\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"258\" height=\"258\" src=\"https:\/\/coderbee.net\/wp-content\/uploads\/2019\/01\/coderbee-note.jpg\" class=\"alignnone size-full wp-image-1707\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>1. AOP \u4e0e\u65b9\u6cd5\u8c03\u7528 \u5bf9\u4e8e \u63a5\u53e3 interface\uff0c\u53ef\u4ee5\u901a\u8fc7 java.l &hellip; <a href=\"https:\/\/coderbee.net\/index.php\/framework\/20161027\/1428\">\u7ee7\u7eed\u9605\u8bfb <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[255],"tags":[256,172,208],"_links":{"self":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts\/1428"}],"collection":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/comments?post=1428"}],"version-history":[{"count":3,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts\/1428\/revisions"}],"predecessor-version":[{"id":1431,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/posts\/1428\/revisions\/1431"}],"wp:attachment":[{"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/media?parent=1428"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/categories?post=1428"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/coderbee.net\/index.php\/wp-json\/wp\/v2\/tags?post=1428"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}