{"id":4,"date":"2013-03-28T16:40:06","date_gmt":"2013-03-28T16:40:06","guid":{"rendered":"http:\/\/www.yeetrack.com\/post\/2013-03-28\/java%20spring%20%E4%BA%8B%E5%8A%A1%E7%AE%A1%E7%90%86%E5%99%A8"},"modified":"2013-09-27T22:36:45","modified_gmt":"2013-09-27T14:36:45","slug":"javaspring%e4%ba%8b%e5%8a%a1%e7%ae%a1%e7%90%86%e5%99%a8","status":"publish","type":"post","link":"https:\/\/www.yeetrack.com\/?p=4","title":{"rendered":"java spring \u4e8b\u52a1\u7ba1\u7406\u5668"},"content":{"rendered":"<p>spring\u6846\u67b6\u6709\u81ea\u5df1\u7684\u4e8b\u52a1\u7ba1\u7406\u673a\u5236\uff0cspring\u4f7f\u7528DataSourceTransactionManager\u4f5c\u4e3aJdbc\u7684\u4e8b\u52a1\u7ba1\u7406\u5668\uff0c\u540c\u65f6\u628a\u88ab\u7ba1\u7406\u7684\u5bf9\u8c61\u4f7f\u7528TransactionProxyFactoryBean\u914d\u7f6e\uff0cTransactionProxyFactoryBean\u662f\u4e2a\u4e8b\u52a1\u4ee3\u7406\u7684Bean\uff0c\u80fd\u591f\u4f7f\u7528IOC\u3001AOP\u7b49\u6ce8\u5165\u4e8b\u52a1\u7ba1\u7406\u4ee3\u7801<\/p>\n<p><!--more--><\/p>\n<p>\u3002<br \/> demo\u4ee3\u7801\u4f7f\u7528<a href=\"https:\/\/www.yeetrack.com\/post\/2013-03-27\/spring%20DAO%E6%A8%A1%E5%9D%97%E6%94%AF%E6%8C%81jdbc\">https:\/\/www.yeetrack.com\/post\/2013-03-27\/spring%20DAO%E6%A8%A1%E5%9D%97%E6%94%AF%E6%8C%81jdbc<\/a>\u8fd9\u91cc\u7684\u4ee3\u7801\uff0c\u65b0\u5efaTransactionRun.java\u6587\u4ef6\uff1a<\/p>\n<pre config=\"brush:java;toolbar:false;\">\npackage com.yeetrack.springDAO;\n\nimport java.util.Date;\n\n\/**\n *\n * @author youthflies\n *\n *\/\npublic class TransactionRun\n{\n\n    private IPersonDAO personDao;\n\n    public IPersonDAO getPersonDao()\n    {\n        return personDao;\n    }\n\n    public void setPersonDao(IPersonDAO personDao)\n    {\n        this.personDao = personDao;\n    }\n\n    \/**\n     * runsql\u4e2d\u7684\u4ee3\u7801\u4e0eTestDao\u4e2d\u7684\u4e00\u6837\uff0c\u4e0d\u8fc7\u5176\u4e2dgetPersonName()\u65b9\u6cd5\u4f1a\u62a5\u5f02\u5e38\n     * \u5982\u679c\u6ca1\u6709\u4e8b\u52a1\u63a7\u5236\uff0cpersonDao.addPerson()\u4f1a\u6267\u884c\u6210\u529f\n     * \u5982\u679c\u6709\u4e8b\u52a1\u63a7\u5236\u5668\uff0c\u56e0\u4e3arunSql\u4e2d\u7206\u51fa\u5f02\u5e38\uff0c\u6574\u4e2a\u4e8b\u52a1\u90fd\u4f1a\u56de\u6eda\n     *\/\n    public void run()\n    {\n        Person person =new Person();\n        person.setName(&quot;youthflies--1&quot;);\n        person.setAge(22);\n        person.setSex(&quot;M&quot;);\n        person.setBirthday(new Date());\n        personDao.addPerson(person);\n\n        System.out.println(personDao.getPersonName(1212312312389));\/\/\u8fd9\u91cc\u56e0\u4e3a\u5f02\u5e38\uff0c\u4f1a\u5bfc\u81f4\u6574\u4e2a\u4e8b\u52a1\u6267\u884c\u5931\u8d25\n\n        System.out.println(personDao.getPersionCount());\n\n        System.out.println(personDao.listPerson());\n    }\n\n}\n<\/pre>\n<p>\u5728applicationContext.xml\u4e2d\u914d\u7f6e\u4e8b\u52a1\uff1a<\/p>\n<pre config=\"brush:html;toolbar:false;\">\n&lt;!-- jdbc \u4e8b\u52a1\u7ba1\u7406\u5668 --&gt;\n    &lt;bean id=&quot;jdbcTransactionManager&quot; class=&quot;org.springframework.jdbc.datasource.DataSourceTransactionManager&quot;&gt;\n        &lt;property name=&quot;dataSource&quot; ref=&quot;dataSource&quot;&gt;&lt;\/property&gt;\n    &lt;\/bean&gt;\n    &lt;!-- \u4e3a\u65b9\u6cd5\u914d\u7f6e\u4e8b\u52a1 --&gt;\n    &lt;bean id=&quot;transactionAttributeSource&quot; class=&quot;org.springframework.transaction.interceptor.NameMatchTransactionAttributeSource&quot;&gt;\n        &lt;property name=&quot;properties&quot;&gt;\n            &lt;props&gt;\n                &lt;prop key=&quot;*&quot;&gt;PROPAGATION_REQUIRED&lt;\/prop&gt;   &lt;!-- \u4e8b\u52a1\u7c7b\u578b,\u5982\u679c\u5f53\u524d\u6ca1\u6709\u4e8b\u52a1\uff0c\u5c31\u65b0\u5efa\u4e00\u4e2a\u4e8b\u52a1\uff0c\u5982\u679c\u5df2\u7ecf\u5b58\u5728\u4e00\u4e2a\u4e8b\u52a1\u4e2d\uff0c\u52a0\u5165\u5230\u8fd9\u4e2a\u4e8b\u52a1\u4e2d\u3002\u8fd9\u662f\u6700\u5e38\u89c1\u7684\u9009\u62e9\u3002 --&gt;\n            &lt;\/props&gt;\n        &lt;\/property&gt;\n    &lt;\/bean&gt;\n\n    &lt;!-- \u4f7f\u7528proxy\uff0c\u5c06\u4e8b\u52a1\u7ba1\u7406\u6ce8\u5165\u5230\u76ee\u6807\u65b9\u6cd5\u4e2d --&gt;\n    &lt;bean id=&quot;transactionRun&quot; class=&quot;org.springframework.transaction.interceptor.TransactionProxyFactoryBean&quot;&gt;\n        &lt;property name=&quot;transactionManager&quot; ref=&quot;jdbcTransactionManager&quot;&gt;&lt;\/property&gt;\n        &lt;property name=&quot;target&quot;&gt;\n            &lt;bean class=&quot;com.yeetrack.springDAO.TransactionRun&quot;&gt;\n                &lt;property name=&quot;personDao&quot; ref=&quot;personDao&quot;&gt;&lt;\/property&gt;\n            &lt;\/bean&gt;\n        &lt;\/property&gt;\n        &lt;property name=&quot;transactionAttributeSource&quot; ref=&quot;transactionAttributeSource&quot;&gt;&lt;\/property&gt;\n    &lt;\/bean&gt;\n<\/pre>\n<p>spring\u4f1a\u5728\u6307\u5b9a\u65b9\u6cd5\u6267\u884c\u524d\u5f00\u542f\u4e8b\u52a1\uff0c\u5728\u6307\u5b9a\u65b9\u6cd5\u7ed3\u675f\u540e\u63d0\u4ea4\u4e8b\u52a1\uff0c\u5982\u679c\u4e2d\u95f4\u6709\u5f02\u5e38\uff0c\u4e8b\u52a1\u4f1a\u56de\u6eda\u3002<br \/> \u4e0b\u9762\u662f\u6d4b\u8bd5\u4ee3\u7801\uff1a<\/p>\n<pre config=\"brush:java;toolbar:false;\">\npackage com.yeetrack.springDAO;\n\nimport org.springframework.context.ApplicationContext;\nimport org.springframework.context.support.ClassPathXmlApplicationContext;\n\npublic class TestTransction\n{\n\n    \/**\n     * @param args\n     *\/\n    public static void main(String[] args)\n    {\n        \/\/ TODO Auto-generated method stub\n        ApplicationContext context = new ClassPathXmlApplicationContext(&quot;applicationContext.xml&quot;);\n        TransactionRun transaction = (TransactionRun) context.getBean(&quot;transactionRun&quot;);\n        transaction.run();\n\n    }\n\n}\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>spring\u6846\u67b6\u6709\u81ea\u5df1\u7684\u4e8b\u52a1\u7ba1\u7406\u673a\u5236\uff0cspring\u4f7f\u7528DataSourceTransactionManager\u4f5c\u4e3aJdbc\u7684\u4e8b\u52a1\u7ba1\u7406\u5668\uff0c\u540c\u65f6\u628a\u88ab\u7ba1\u7406\u7684\u5bf9\u8c61\u4f7f\u7528TransactionProxyFacto&#46;&#46;&#46;<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"pgc_sgb_lightbox_settings":"","footnotes":""},"categories":[33],"tags":[8,7,5],"class_list":["post-4","post","type-post","status-publish","format-standard","hentry","category-coding","tag-java","tag-7","tag-5"],"views":2537,"_links":{"self":[{"href":"https:\/\/www.yeetrack.com\/index.php?rest_route=\/wp\/v2\/posts\/4","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.yeetrack.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.yeetrack.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.yeetrack.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.yeetrack.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=4"}],"version-history":[{"count":1,"href":"https:\/\/www.yeetrack.com\/index.php?rest_route=\/wp\/v2\/posts\/4\/revisions"}],"predecessor-version":[{"id":184,"href":"https:\/\/www.yeetrack.com\/index.php?rest_route=\/wp\/v2\/posts\/4\/revisions\/184"}],"wp:attachment":[{"href":"https:\/\/www.yeetrack.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.yeetrack.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.yeetrack.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}