{"id":58,"date":"2021-01-13T09:13:04","date_gmt":"2021-01-13T01:13:04","guid":{"rendered":"http:\/\/mengstudy.com\/?p=58"},"modified":"2021-01-15T08:55:50","modified_gmt":"2021-01-15T00:55:50","slug":"spring-boot%e5%bc%80%e5%90%af%e5%a3%b0%e6%98%8e%e5%bc%8f%e4%ba%8b%e5%8a%a1","status":"publish","type":"post","link":"https:\/\/fugary.com\/?p=58","title":{"rendered":"Spring Boot\u5f00\u542f\u58f0\u660e\u5f0f\u4e8b\u52a1"},"content":{"rendered":"<h2>Spring Boot\u5f00\u542f\u58f0\u660e\u5f0f\u4e8b\u52a1<\/h2>\n<p>\u5728\u4ee5\u524d\u65e9\u671f\u7684Spring\u4f7f\u7528xml\u65b9\u5f0f\u7684\u65f6\u5019\uff0c\u914d\u7f6e\u58f0\u660e\u5f0f\u4e8b\u52a1\u901a\u5e38\u7528xml\u65b9\u5f0f\uff0c\u4f7f\u7528Spring boot\u5f00\u59cb\uff0cxml\u57fa\u672c\u7b97\u662f\u6dd8\u6c70\u4e86\uff0c\u56e0\u6b64\u600e\u4e48\u914d\u7f6e\u58f0\u660e\u5f0f\u4e8b\u52a1\u5462\uff1f\u5f53\u7136\u8fd8\u662f\u53ef\u4ee5\u7528xml\u6216\u8005JavaConfig\u65b9\u5f0f\u3002<\/p>\n<h3>\u5f00\u542fSpring Boot\u4e8b\u52a1\uff08\u6ce8\u89e3\u5f0f\uff09<\/h3>\n<p>\u8981\u5f00\u542fSpring Boot\u4e8b\u52a1\u6bd4\u8f83\u7b80\u5355\uff0c\u5728\u4e00\u4e2a\u914d\u7f6e\u7c7b\u4e2d\u4f7f\u7528\u4e00\u4e2a\u6ce8\u89e3\u5c31\u53ef\u4ee5\u4e86<\/p>\n<pre><code class=\"language-java\">@EnableTransactionManagement<\/code><\/pre>\n<p>\u5f00\u542f\u540e\u53ef\u4ee5\u7528<code>@Transactional<\/code>\u6ce8\u89e3\u7ed9\u65b9\u6cd5\u589e\u52a0\u4e8b\u52a1\u5373\u53ef<\/p>\n<p>\u58f0\u660e\u5f0f\u4e8b\u52a1\u914d\u7f6e\u65b9\u5f0f\u5c31\u4e0d\u592a\u4e00\u6837\u4e86\uff0c\u4e0d\u9700\u8981\u6bcf\u4e2a\u65b9\u6cd5\u4e0a\u53bb\u914d\u7f6e\uff0c\u53ef\u4ee5\u4f7f\u7528<code>aspectj<\/code>\u8bed\u6cd5\u8868\u8fbe\u5f0f\u6307\u5b9a\u5305\u4e0b\u9762\u7684\u7c7b\u6839\u636e\u65b9\u6cd5\u540d\u5b57\u524d\u7f00\u914d\u7f6e\uff0c\u53ea\u8981\u540d\u5b57\u5339\u914d\u5c31\u53ef\u4ee5\u5b9e\u73b0\u4e8b\u52a1\uff0c\u76f8\u5bf9\u6765\u8bf4\u6bd4\u8f83\u65b9\u4fbf\u3002<\/p>\n<pre><code>execution (* com..service..*.*(..))<\/code><\/pre>\n<h3>\u5224\u65ad\u4e8b\u52a1\u72b6\u6001<\/h3>\n<p>\u5982\u4f55\u5224\u65ad\u4e00\u4e2a\u65b9\u6cd5\u6267\u884c\u7684\u65f6\u5019\u662f\u5426\u5728Spring\u7684\u4e8b\u52a1\u4e2d\u5462\uff1f\u5176\u5b9eSpring\u542f\u52a8\u4e00\u4e2a\u4e8b\u52a1\u4e4b\u540e\uff0c\u4f1a\u628a\u76f8\u5173\u4fe1\u606f\u5199\u5165\u5230<code>ThreadLocal<\/code>\u4e2d\uff0c\u7136\u540eSpring\u63d0\u4f9b\u4e00\u4e2a\u5de5\u5177\u7c7b\uff08<code>TransactionSynchronizationManager<\/code>\uff09\uff0c\u53ef\u4ee5\u67e5\u770b\u4e8b\u52a1\u6267\u884c\u8fc7\u7a0b\u4e2d\u7684\u72b6\u6001\u7b49\u4fe1\u606f\u3002<\/p>\n<pre><code class=\"language-java\">if (TransactionSynchronizationManager.isActualTransactionActive()) {\n   TransactionStatus status = TransactionAspectSupport.currentTransactionStatus();\n   \/\/ \u67e5\u770b\u72b6\u6001\u4fe1\u606f\n} else {\n    \/\/ \u6ca1\u6709\u5f00\u542f\u4e8b\u52a1\uff08\u6216\u8005\u6ca1\u6709\u914d\u7f6e\u6210\u529f\uff09\n}<\/code><\/pre>\n<p>\u8c03\u8bd5\u7684\u65f6\u5019\u53ef\u4ee5\u770b\u5230\u4e8b\u52a1\u72b6\u6001\u5bf9\u8c61\u5982\u4e0b\u56fe\uff1a<\/p>\n<p><div class='fancybox-wrapper' data-fancybox='post-images' href='https:\/\/git.mengqingpo.com:8888\/fugary\/blogpic1\/-\/raw\/master\/images\/20200514203505.png'><img src=\"https:\/\/git.mengqingpo.com:8888\/fugary\/blogpic1\/-\/raw\/master\/images\/20200514203505.png\" alt=\"\" \/><\/div><\/p>\n<h3>XML\u65b9\u5f0f\u914d\u7f6e\u58f0\u660e\u5f0f\u4e8b\u52a1<\/h3>\n<p>\u5728spring boot\u9879\u76ee\u4e2d\uff0c\u4f7f\u7528<code>@EnableTransactionManagement<\/code>\u5f00\u542f\u4e86\u4e8b\u52a1\u4e4b\u540e\uff0c\u4e8b\u52a1\u7ba1\u7406\u5668\u5df2\u7ecf\u5b58\u5728\u4e86\uff0c\u7136\u540e\u53ea\u9700\u8981\u914d\u7f6e\u4e8b\u52a1\u5207\u9762\u5373\u53ef\uff0cspring boot\u4e5f\u662f\u652f\u6301xml\u65b9\u5f0f\u914d\u7f6e\u7684\uff0c\u53ef\u4ee5\u7f16\u5199\u597dxml\uff08\u6211\u8fd9\u8d77\u540d\u53eb<code>spring-tx.xml<\/code>\uff09\uff0c\u7136\u540e\u4f7f\u7528\u6ce8\u89e3\u5f15\u5165\u3002<\/p>\n<pre><code class=\"language-java\">@ImportResource(&quot;classpath:spring-tx.xml&quot;)<\/code><\/pre>\n<pre><code class=\"language-xml\">&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;\n&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; xmlns:aop=&quot;http:\/\/www.springframework.org\/schema\/aop&quot;\n       xsi:schemaLocation=&quot;http:\/\/www.springframework.org\/schema\/beans http:\/\/www.springframework.org\/schema\/beans\/spring-beans.xsd\n         http:\/\/www.springframework.org\/schema\/tx http:\/\/www.springframework.org\/schema\/tx\/spring-tx.xsd\n         http:\/\/www.springframework.org\/schema\/aop http:\/\/www.springframework.org\/schema\/aop\/spring-aop.xsd&quot;&gt;\n    &lt;!-- \u81ea\u52a8\u6620\u5c04\u4e8b\u52a1, spring boot\u9879\u76ee\u4e2d@EnableTransactionManagement\u5df2\u7ecf\u6709transactionManager\uff0c\u5f15\u7528\u5373\u53ef --&gt;\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;save*&quot; propagation=&quot;REQUIRED&quot;\/&gt;\n            &lt;tx:method name=&quot;delete*&quot; propagation=&quot;REQUIRED&quot;\/&gt;\n            &lt;tx:method name=&quot;remove*&quot; propagation=&quot;REQUIRED&quot;\/&gt;\n            &lt;tx:method name=&quot;update*&quot; propagation=&quot;REQUIRED&quot;\/&gt;\n            &lt;tx:method name=&quot;add*&quot; propagation=&quot;REQUIRED&quot;\/&gt;\n            &lt;tx:method name=&quot;create*&quot; propagation=&quot;REQUIRED&quot;\/&gt;\n            &lt;tx:method name=&quot;insert*&quot; propagation=&quot;REQUIRED&quot;\/&gt;\n            &lt;tx:method name=&quot;mark*&quot; propagation=&quot;REQUIRED&quot;\/&gt;\n            &lt;tx:method name=&quot;get*&quot; read-only=&quot;true&quot; \/&gt;\n            &lt;tx:method name=&quot;query*&quot; read-only=&quot;true&quot; \/&gt;\n            &lt;tx:method name=&quot;list*&quot; read-only=&quot;true&quot; \/&gt;\n            &lt;tx:method name=&quot;count*&quot; read-only=&quot;true&quot; \/&gt;\n            &lt;tx:method name=&quot;page*&quot; read-only=&quot;true&quot; \/&gt;\n            &lt;tx:method name=&quot;find*&quot; read-only=&quot;true&quot; \/&gt;\n            &lt;tx:method name=&quot;load*&quot; read-only=&quot;true&quot; \/&gt;\n        &lt;\/tx:attributes&gt;\n    &lt;\/tx:advice&gt;\n    &lt;aop:aspectj-autoproxy\/&gt;\n    &lt;aop:config proxy-target-class=&quot;true&quot;&gt;\n        &lt;!-- \u5207\u5165\u70b9 --&gt;\n        &lt;aop:pointcut id=&quot;interceptorPointCuts&quot; expression=&quot;execution (* com..service..*.*(..))&quot;\/&gt;\n        &lt;!-- \u901a\u77e5 --&gt;\n        &lt;aop:advisor advice-ref=&quot;txAdvice&quot; pointcut-ref=&quot;interceptorPointCuts&quot; order=&quot;200&quot;\/&gt;\n    &lt;\/aop:config&gt;\n&lt;\/beans&gt;<\/code><\/pre>\n<h3>JavaConfig\u914d\u7f6e\u58f0\u660e\u5f0f\u4e8b\u52a1<\/h3>\n<p>\u5176\u5b9eJavaConfig\u65b9\u5f0f\u76f8\u5bf9\u6bd4\u8f83\u7b80\u5355\u4e00\u70b9\uff0c\u53ea\u9700\u8981\u5199\u4e00\u4e2a\u914d\u7f6e\u7c7b\u5373\u53ef\uff1a<\/p>\n<pre><code class=\"language-java\">@Configuration\npublic class GlobalTransactionConfig {\n\n    \/**\n     * \u6ce8\u5165\u5df2\u7ecf\u5b58\u5728\u7684\u4e8b\u52a1\u7ba1\u7406\u5668\n     *\/\n    @Autowired\n    private PlatformTransactionManager transactionManager;\n\n    @Bean\n    public TransactionInterceptor txAdvice() {\n        \/\/ \u5f00\u542f\u4e8b\u52a1\u65b9\u6cd5\n        TransactionAttribute requiredTransaction = new DefaultTransactionAttribute(TransactionDefinition.PROPAGATION_REQUIRED);\n        NameMatchTransactionAttributeSource nameMatchSource = new NameMatchTransactionAttributeSource();\n        nameMatchSource.addTransactionalMethod(&quot;save*&quot;, requiredTransaction);\n        nameMatchSource.addTransactionalMethod(&quot;delete*&quot;, requiredTransaction);\n        nameMatchSource.addTransactionalMethod(&quot;remove*&quot;, requiredTransaction);\n        nameMatchSource.addTransactionalMethod(&quot;update*&quot;, requiredTransaction);\n        nameMatchSource.addTransactionalMethod(&quot;add*&quot;, requiredTransaction);\n        nameMatchSource.addTransactionalMethod(&quot;create*&quot;, requiredTransaction);\n        nameMatchSource.addTransactionalMethod(&quot;insert*&quot;, requiredTransaction);\n        nameMatchSource.addTransactionalMethod(&quot;mark*&quot;, requiredTransaction);\n        \/\/ ReadOnly\u4e8b\u52a1\u65b9\u6cd5\n        DefaultTransactionAttribute readonlyTransaction = new DefaultTransactionAttribute(TransactionDefinition.PROPAGATION_REQUIRED);\n        readonlyTransaction.setReadOnly(true);\n        nameMatchSource.addTransactionalMethod(&quot;get*&quot;, readonlyTransaction);\n        nameMatchSource.addTransactionalMethod(&quot;query*&quot;, readonlyTransaction);\n        nameMatchSource.addTransactionalMethod(&quot;list*&quot;, readonlyTransaction);\n        nameMatchSource.addTransactionalMethod(&quot;count*&quot;, readonlyTransaction);\n        nameMatchSource.addTransactionalMethod(&quot;page*&quot;, readonlyTransaction);\n        nameMatchSource.addTransactionalMethod(&quot;find*&quot;, readonlyTransaction);\n        nameMatchSource.addTransactionalMethod(&quot;load*&quot;, readonlyTransaction);\n        return new TransactionInterceptor(transactionManager, nameMatchSource);\n    }\n\n    @Bean\n    public Advisor txAdviceAdvisor() {\n        AspectJExpressionPointcut pointcut = new AspectJExpressionPointcut();\n        pointcut.setExpression(&quot;execution (* com..service..*.*(..))&quot;);\n        return new DefaultPointcutAdvisor(pointcut, txAdvice());\n    }\n}\n<\/code><\/pre>\n<h3>\u58f0\u660e\u5f0f\u4e8b\u52a1\u4f9d\u8d56<\/h3>\n<p>\u53ea\u8981\u4f7f\u7528\u4e86\u58f0\u660e\u5f0f\u4e8b\u52a1\uff0c\u7531\u4e8e\u4f7f\u7528\u4e86\u5207\u9762\uff08aop\uff09\u529f\u80fd\uff0c\u5c31\u5fc5\u987b\u4f9d\u8d56<code>spring-aop<\/code>\u9879\u76ee\u548c<code>aspectjweaver<\/code>\u9879\u76ee\uff0c\u5426\u5219\u4f1a\u62a5\u9519\uff0cspring boot\u9879\u76ee\u76f4\u63a5\u5f15\u5165<code>spring-boot-starter-aop<\/code>\u5373\u53ef\u3002<\/p>\n<pre><code class=\"language-xml\">        &lt;dependency&gt;\n            &lt;groupId&gt;org.springframework.boot&lt;\/groupId&gt;\n            &lt;artifactId&gt;spring-boot-starter-aop&lt;\/artifactId&gt;\n        &lt;\/dependency&gt;<\/code><\/pre>\n<h3>\u603b\u7ed3<\/h3>\n<p>\u5176\u5b9espring boot\u9879\u76ee\u914d\u7f6e\u58f0\u660e\u5f0f\u4e8b\u52a1\uff0c\u65e0\u8bba\u4f7f\u7528xml\u8fd8\u662f\u6ce8\u89e3\u65b9\u5f0f\uff0c\u90fd\u662f\u4f7f\u7528aop\u65b9\u5f0f\u914d\u7f6e\u62e6\u622a\u65b9\u6cd5\u6267\u884c\u5373\u53ef\u3002<\/p>\n<p>\u5b9e\u9645\u9879\u76ee\u4e2d\u53ef\u4ee5\u8003\u8651\u4f7f\u7528\u6ce8\u89e3\u4f1a\u66f4\u52a0\u65b9\u4fbf\uff0c\u800c\u4e14\u76ee\u524dspring\u9879\u76ee\u90fd\u662f\u53bbxml\u5316\uff0cxml\u6ce8\u5b9a\u662f\u88ab\u629b\u5f03\u7684\uff0c\u65b0\u9879\u76ee\u57fa\u672c\u770b\u4e0d\u5230xml\u914d\u7f6e\u4e86\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Spring Boot\u5f00\u542f\u58f0\u660e\u5f0f\u4e8b\u52a1 \u5728\u4ee5\u524d\u65e9\u671f\u7684Spring\u4f7f\u7528xml\u65b9\u5f0f\u7684\u65f6\u5019\uff0c\u914d\u7f6e\u58f0\u660e\u5f0f\u4e8b\u52a1\u901a\u5e38\u7528xml [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[5],"tags":[],"_links":{"self":[{"href":"https:\/\/fugary.com\/index.php?rest_route=\/wp\/v2\/posts\/58"}],"collection":[{"href":"https:\/\/fugary.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fugary.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fugary.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fugary.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=58"}],"version-history":[{"count":2,"href":"https:\/\/fugary.com\/index.php?rest_route=\/wp\/v2\/posts\/58\/revisions"}],"predecessor-version":[{"id":99,"href":"https:\/\/fugary.com\/index.php?rest_route=\/wp\/v2\/posts\/58\/revisions\/99"}],"wp:attachment":[{"href":"https:\/\/fugary.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=58"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fugary.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=58"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fugary.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=58"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}