{"id":12418,"date":"2023-11-28T22:52:59","date_gmt":"2023-11-28T14:52:59","guid":{"rendered":"https:\/\/wx.kaifamiao.info\/?p=12418"},"modified":"2023-12-11T17:10:16","modified_gmt":"2023-12-11T09:10:16","slug":"spring-shi-wu-shi-xian-fang-shi-you-na-xie","status":"publish","type":"post","link":"http:\/\/wx.kaifamiao.info\/index.php\/2023\/11\/28\/spring-shi-wu-shi-xian-fang-shi-you-na-xie\/","title":{"rendered":"Spring\u4e8b\u52a1\u5b9e\u73b0\u65b9\u5f0f\u6709\u54ea\u4e9b\uff1f"},"content":{"rendered":"<p>Spring \u63d0\u4f9b\u4e86\u591a\u79cd\u4e8b\u52a1\u7ba1\u7406\u7684\u5b9e\u73b0\u65b9\u5f0f\uff0c\u4ee5\u6ee1\u8db3\u4e0d\u540c\u7684\u9700\u6c42\u548c\u73af\u5883\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u89c1\u7684 Spring \u4e8b\u52a1\u7ba1\u7406\u7684\u5b9e\u73b0\u65b9\u5f0f\uff1a<\/p>\n<ol>\n<li>\n<strong>\u7f16\u7a0b\u5f0f\u4e8b\u52a1\u7ba1\u7406\uff1a<\/strong><\/p>\n<ul>\n<li>\u5728\u7f16\u7a0b\u5f0f\u4e8b\u52a1\u7ba1\u7406\u4e2d\uff0c\u5f00\u53d1\u8005\u901a\u8fc7\u7f16\u5199\u4ee3\u7801\u663e\u5f0f\u5730\u7ba1\u7406\u4e8b\u52a1\u7684\u5f00\u59cb\u3001\u63d0\u4ea4\u3001\u56de\u6eda\u7b49\u64cd\u4f5c\u3002\u4f7f\u7528 <code>TransactionTemplate<\/code> \u6216\u8005 <code>PlatformTransactionManager<\/code> \u63a5\u53e3\u53ef\u4ee5\u8fdb\u884c\u7f16\u7a0b\u5f0f\u4e8b\u52a1\u7ba1\u7406\u3002<\/li>\n<\/ul>\n<pre><code class=\"language-java\">TransactionTemplate transactionTemplate = new TransactionTemplate(transactionManager);\ntransactionTemplate.execute(status -&gt; {\n    \/\/ \u6267\u884c\u4e8b\u52a1\u64cd\u4f5c\n    \/\/ \u5982\u679c\u629b\u51fa\u5f02\u5e38\uff0c\u4e8b\u52a1\u4f1a\u56de\u6eda\n    return result;\n});\n<\/code><\/pre>\n<\/li>\n<li>\n<strong>\u58f0\u660e\u5f0f\u4e8b\u52a1\u7ba1\u7406\uff1a<\/strong><\/p>\n<ul>\n<li>\n\u5728\u58f0\u660e\u5f0f\u4e8b\u52a1\u7ba1\u7406\u4e2d\uff0c\u5f00\u53d1\u8005\u901a\u8fc7\u914d\u7f6e\u5143\u6570\u636e\uff08\u901a\u5e38\u662f XML \u6216\u8005\u6ce8\u89e3\uff09\u6765\u6307\u5b9a\u4e8b\u52a1\u7684\u5c5e\u6027\uff0c\u800c\u4e0d\u662f\u5728\u4ee3\u7801\u4e2d\u663e\u5f0f\u7ba1\u7406\u3002Spring \u63d0\u4f9b\u4e86\u57fa\u4e8e XML \u548c\u57fa\u4e8e\u6ce8\u89e3\u7684\u58f0\u660e\u5f0f\u4e8b\u52a1\u7ba1\u7406\u3002\n<\/li>\n<li>\n<strong>XML \u914d\u7f6e\u65b9\u5f0f\uff1a<\/strong>\n<\/li>\n<\/ul>\n<pre><code class=\"language-xml\">&lt;beans xmlns=&quot;http:\/\/www.springframework.org\/schema\/beans&quot;\n       xmlns:xsi=&quot;http:\/\/www.w3.org\/2001\/XMLSchema-instance&quot;\n       xmlns:tx=&quot;http:\/\/www.springframework.org\/schema\/tx&quot;\n       xsi:schemaLocation=&quot;http:\/\/www.springframework.org\/schema\/beans\n           http:\/\/www.springframework.org\/schema\/beans\/spring-beans.xsd\n           http:\/\/www.springframework.org\/schema\/tx\n           http:\/\/www.springframework.org\/schema\/tx\/spring-tx.xsd&quot;&gt;\n\n    &lt;bean id=&quot;myService&quot; class=&quot;com.example.MyServiceImpl&quot;\/&gt;\n\n    &lt;tx:advice id=&quot;txAdvice&quot; transaction-manager=&quot;transactionManager&quot;&gt;\n        &lt;tx:attributes&gt;\n            &lt;tx:method name=&quot;*&quot; propagation=&quot;REQUIRED&quot;\/&gt;\n        &lt;\/tx:attributes&gt;\n    &lt;\/tx:advice&gt;\n\n    &lt;aop:config&gt;\n        &lt;aop:pointcut id=&quot;serviceMethods&quot; expression=&quot;execution(* com.example.MyService.*(..))&quot;\/&gt;\n        &lt;aop:advisor advice-ref=&quot;txAdvice&quot; pointcut-ref=&quot;serviceMethods&quot;\/&gt;\n    &lt;\/aop:config&gt;\n&lt;\/beans&gt;\n<\/code><\/pre>\n<ul>\n<li><strong>\u6ce8\u89e3\u914d\u7f6e\u65b9\u5f0f\uff1a<\/strong><\/li>\n<\/ul>\n<pre><code class=\"language-java\">@Service\n@Transactional\npublic class MyServiceImpl implements MyService {\n    \/\/ \u65b9\u6cd5\u5b9e\u73b0\n}\n<\/code><\/pre>\n<\/li>\n<li>\n<strong>\u6df7\u5408\u4e8b\u52a1\u7ba1\u7406\uff1a<\/strong><\/p>\n<ul>\n<li>\u53ef\u4ee5\u5728\u540c\u4e00\u4e2a\u5e94\u7528\u7a0b\u5e8f\u4e2d\u540c\u65f6\u4f7f\u7528\u7f16\u7a0b\u5f0f\u4e8b\u52a1\u7ba1\u7406\u548c\u58f0\u660e\u5f0f\u4e8b\u52a1\u7ba1\u7406\u3002\u8fd9\u79cd\u65b9\u5f0f\u5141\u8bb8\u5f00\u53d1\u8005\u5728\u67d0\u4e9b\u573a\u666f\u4e0b\u4f7f\u7528\u7f16\u7a0b\u5f0f\u4e8b\u52a1\u7ba1\u7406\uff0c\u5728\u53e6\u4e00\u4e9b\u573a\u666f\u4e0b\u4f7f\u7528\u58f0\u660e\u5f0f\u4e8b\u52a1\u7ba1\u7406\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\n<strong>\u57fa\u4e8e\u6ce8\u89e3\u7684\u4e8b\u52a1\u7ba1\u7406\uff1a<\/strong><\/p>\n<ul>\n<li>Spring \u63d0\u4f9b\u4e86 <code>@Transactional<\/code> \u6ce8\u89e3\uff0c\u53ef\u4ee5\u7528\u4e8e\u5c06\u65b9\u6cd5\u6216\u7c7b\u6807\u8bb0\u4e3a\u4e8b\u52a1\u6027\u7684\uff0c\u5e76\u63d0\u4f9b\u4e00\u4e9b\u5c5e\u6027\u6765\u914d\u7f6e\u4e8b\u52a1\u7684\u5c5e\u6027\uff0c\u5982\u9694\u79bb\u7ea7\u522b\u3001\u4f20\u64ad\u884c\u4e3a\u3001\u8d85\u65f6\u7b49\u3002<\/li>\n<\/ul>\n<pre><code class=\"language-java\">@Service\n@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, timeout = 30, readOnly = false)\npublic class MyServiceImpl implements MyService {\n    \/\/ \u65b9\u6cd5\u5b9e\u73b0\n}\n<\/code><\/pre>\n<\/li>\n<\/ol>\n<p>\u8fd9\u4e9b\u4e8b\u52a1\u7ba1\u7406\u65b9\u5f0f\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u7684\u9700\u6c42\u548c\u573a\u666f\u8fdb\u884c\u9009\u62e9\u548c\u7ec4\u5408\u4f7f\u7528\u3002\u58f0\u660e\u5f0f\u4e8b\u52a1\u7ba1\u7406\u662f Spring \u4e2d\u6700\u5e38\u7528\u7684\u4e00\u79cd\u65b9\u5f0f\uff0c\u56e0\u4e3a\u5b83\u53ef\u4ee5\u901a\u8fc7\u914d\u7f6e\u7684\u65b9\u5f0f\u6765\u5b9a\u4e49\u4e8b\u52a1\u89c4\u5219\uff0c\u4f7f\u5f97\u5f00\u53d1\u8005\u53ef\u4ee5\u66f4\u65b9\u4fbf\u5730\u8fdb\u884c\u4e8b\u52a1\u7ba1\u7406\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Spring \u63d0\u4f9b\u4e86\u591a\u79cd\u4e8b\u52a1\u7ba1\u7406\u7684\u5b9e\u73b0\u65b9\u5f0f\uff0c\u4ee5\u6ee1\u8db3\u4e0d\u540c\u7684\u9700\u6c42\u548c\u73af\u5883\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u89c1\u7684 Spring \u4e8b\u52a1\u7ba1\u7406\u7684 [&hellip;]<\/p>\n","protected":false},"author":9,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[121],"tags":[],"class_list":["post-12418","post","type-post","status-publish","format-standard","hentry","category-spring"],"_links":{"self":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/12418","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/users\/9"}],"replies":[{"embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/comments?post=12418"}],"version-history":[{"count":2,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/12418\/revisions"}],"predecessor-version":[{"id":47904,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/posts\/12418\/revisions\/47904"}],"wp:attachment":[{"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/media?parent=12418"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/categories?post=12418"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/wx.kaifamiao.info\/index.php\/wp-json\/wp\/v2\/tags?post=12418"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}