{"id":112,"date":"2020-02-10T11:53:46","date_gmt":"2020-02-10T03:53:46","guid":{"rendered":"https:\/\/www.zeroheart.xyz\/wordpress\/?p=112"},"modified":"2020-06-19T10:58:14","modified_gmt":"2020-06-19T02:58:14","slug":"spring%e4%ba%8b%e5%8a%a1%e7%ae%80%e5%8d%95%e8%ae%b0%e5%bd%95","status":"publish","type":"post","link":"https:\/\/www.zeroheart.xyz\/wordpress\/?p=112","title":{"rendered":"Spring\u4e8b\u52a1\u7b80\u5355\u8bb0\u5f55"},"content":{"rendered":"\n<p>\u5199\u4ee3\u7801\u90a3\u79cd\u53eb\u7f16\u7a0b\u5f0f\u4e8b\u52a1\uff0c\u5199\u6ce8\u89e3\u662f\u58f0\u660e\u5f0f\u4e8b\u7269\uff0c\u8fd8\u53ef\u4ee5\u5728xml\u4e2d\u914d\u7f6e<\/p>\n\n\n\n<p>@Transactional<\/p>\n\n\n\n<p>Spring\u7684\u4e8b\u52a1\u662f\u901a\u8fc7\u4ee3\u7406\u5b9e\u73b0\u7684<\/p>\n\n\n\n<p>JDK\u7684\u4ee3\u7406\u9700\u8981\u5b9e\u73b0\u63a5\u53e3\uff0cCGlib\u4e0d\u9700\u8981<\/p>\n\n\n\n<p>\u4e24\u4e2a\u65b9\u6cd5\uff0ca\u5e26\u6709\u4e8b\u52a1\uff0cb\u4e0d\u5e26\uff0c\u5728b\u4e2d\u8c03\u7528a\uff0c\u4e0d\u4f1a\u6709\u4e8b\u52a1\u6548\u679c\u3002\u56e0\u4e3a\u4e8b\u52a1\u662f\u4ee3\u7406\u4e2d\u5b9e\u73b0\u589e\u5f3a\u7684\uff0cb\u4e2d\u8c03\u7528a\uff0c\u662f\u5728\u4ee3\u7406\u7c7b\u4e2d\u8c03\u7528\u4e86\u76ee\u6807\u5bf9\u8c61\u7684\u65b9\u6cd5\uff0c\u5c31\u662f\u6ca1\u6709\u4ee3\u7406\u589e\u5f3a\u7684\u65b9\u6cd5\u3002<\/p>\n\n\n\n<p> \u53c2\u8003\uff1ahttps:\/\/blog.csdn.net\/qq_37221991\/java\/article\/details\/90409001<\/p>\n\n\n\n<p>spring \u5728\u626b\u63cfbean\u7684\u65f6\u5019\u4f1a\u626b\u63cf\u65b9\u6cd5\u4e0a\u662f\u5426\u5305\u542b@Transactional\u6ce8\u89e3\uff0c\u5982\u679c\u5305\u542b\uff0cspring\u4f1a\u4e3a\u8fd9\u4e2abean\u52a8\u6001\u5730\u751f\u6210\u4e00\u4e2a\u5b50\u7c7b\uff08\u5373\u4ee3\u7406\u7c7b\uff0cproxy\uff09\uff0c\u4ee3\u7406\u7c7b\u662f\u7ee7\u627f\u539f\u6765\u90a3\u4e2abean\u7684\u3002<br> \u6b64\u65f6\uff0c\u5f53\u8fd9\u4e2a\u6709\u6ce8\u89e3\u7684\u65b9\u6cd5\u88ab\u8c03\u7528\u7684\u65f6\u5019\uff0c\u5b9e\u9645\u4e0a\u662f\u7531\u4ee3\u7406\u7c7b\u6765\u8c03\u7528\u7684\uff0c\u4ee3\u7406\u7c7b\u5728\u8c03\u7528\u4e4b\u524d\u5c31\u4f1a\u542f\u52a8transaction\u3002\u7136\u800c\uff0c\u5982\u679c\u8fd9\u4e2a\u6709\u6ce8\u89e3\u7684\u65b9\u6cd5\u662f\u88ab\u540c\u4e00\u4e2a\u7c7b\u4e2d\u7684\u5176\u4ed6\u65b9\u6cd5\u8c03\u7528\u7684\uff0c\u90a3\u4e48\u8be5\u65b9\u6cd5\u7684\u8c03\u7528\u5e76\u6ca1\u6709\u901a\u8fc7\u4ee3\u7406\u7c7b\uff0c\u800c\u662f\u76f4\u63a5\u901a\u8fc7\u539f\u6765\u7684\u90a3\u4e2abean\uff0c\u6240\u4ee5\u5c31\u4e0d\u4f1a\u542f\u52a8transaction\uff0c\u6211\u4eec\u770b\u5230\u7684\u73b0\u8c61\u5c31\u662f@Transactional\u6ce8\u89e3\u65e0\u6548\u3002<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><em>\/\/\u63a5\u53e3<br><\/em><strong>interface <\/strong>Service {<br>    <strong>void <\/strong>A();<br><br>    <strong>void <\/strong>B();<br>}<br><br><em>\/\/\u76ee\u6807\u7c7b\uff0c\u5b9e\u73b0\u63a5\u53e3<br><\/em><strong>class <\/strong>ServiceImpl <strong>implements <\/strong>Service {<br><br>    <em>\/\/no annotation here<br>    <\/em>@Override<br>    <strong>public void <\/strong>A() {<br>        <strong>this<\/strong>.B();<br>    }<br>    <br>   @Transactional<br>    @Override<br>    <strong>public void <\/strong>B() {<br>        System.<strong><em>out<\/em><\/strong>.println(<strong>\"execute doNeedTx in ServiceImpl\"<\/strong>);<br>    }<br>}<br><br><em>\/\/\u4ee3\u7406\u7c7b\uff0c\u4e5f\u8981\u5b9e\u73b0\u76f8\u540c\u7684\u63a5\u53e3<br><\/em><strong>class <\/strong>ProxyByJdkDynamic <strong>implements <\/strong>Service {<br><br>    <em>\/\/\u5305\u542b\u76ee\u6807\u5bf9\u8c61<br>    <\/em><strong>private <\/strong>Service <strong>target<\/strong>;<br><br>    <strong>public <\/strong>ProxyByJdkDynamic(Service target) {<br>        <strong>this<\/strong>.<strong>target <\/strong>= target;<br>    }<br><br>    <em>\/\/\u76ee\u6807\u7c7b\u4e2d\u6b64\u65b9\u6cd5\u5e26\u6ce8\u89e3\uff0c\u8fdb\u884c\u7279\u6b8a\u5904\u7406<br>    <\/em>@Override<br>    <strong>public void <\/strong>B() {<br>        <em>\/\/\u5f00\u542f\u4e8b\u52a1<br>        <\/em>System.<strong><em>out<\/em><\/strong>.println(<strong>\"-> create Tx here in Proxy\"<\/strong>);<br>        <em>\/\/\u8c03\u7528\u76ee\u6807\u5bf9\u8c61\u7684\u65b9\u6cd5\uff0c\u8be5\u65b9\u6cd5\u5df2\u5728\u4e8b\u52a1\u4e2d\u4e86<br>        <\/em><strong>target<\/strong>.B();<br>        <em>\/\/\u63d0\u4ea4\u4e8b\u52a1<br>        <\/em>System.<strong><em>out<\/em><\/strong>.println(<strong>\"&lt;- commit Tx here in Proxy\"<\/strong>);<br>    }<br><br>    <em>\/\/\u76ee\u6807\u7c7b\u4e2d\u6b64\u65b9\u6cd5\u6ca1\u6709\u6ce8\u89e3\uff0c\u53ea\u505a\u7b80\u5355\u7684\u8c03\u7528<br>    <\/em>@Override<br>    <strong>public void <\/strong>A() {<br>        <em>\/\/\u76f4\u63a5\u8c03\u7528\u76ee\u6807\u5bf9\u8c61\u65b9\u6cd5<br>        <\/em>target.A();<br>    }<br>}<\/pre>\n\n\n\n<p><\/p>\n\n\n\n<p style=\"color:#d34a18\" class=\"has-text-color\">\u4ee3\u7406\u76f8\u5173\u7684\u540e\u9762\u5728\u603b\u7ed3\u3002jdk\u662f\u5b9e\u73b0\u63a5\u53e3\uff0ccglib\u662f\u751f\u6210\u5b50\u7c7b\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5199\u4ee3\u7801\u90a3\u79cd\u53eb\u7f16\u7a0b\u5f0f\u4e8b\u52a1\uff0c\u5199\u6ce8\u89e3\u662f\u58f0\u660e\u5f0f\u4e8b\u7269\uff0c\u8fd8\u53ef\u4ee5\u5728xml\u4e2d\u914d\u7f6e @Transactional Spring\u7684 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[],"_links":{"self":[{"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/112"}],"collection":[{"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=112"}],"version-history":[{"count":6,"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/112\/revisions"}],"predecessor-version":[{"id":510,"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/112\/revisions\/510"}],"wp:attachment":[{"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=112"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=112"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.zeroheart.xyz\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=112"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}