Spring配置事務在Dao層和Service層

來源:互聯網
上載者:User
【轉載自 http://www.blogjava.net/hk2000c/archive/2007/11/01/157508.html 】 
【感謝原作者:hk2000c】 
Spring通過AOP實現聲明式交易管理。通常通過TransactionProxyFactoryBean設定Spring事務代理。我們需要一個目標對象封裝在事務代理中。這個目標對象一般是一個普通Java對象的bean。當我們定義TransactionProxyFactoryBean時,必須提供一個相關的 PlatformTransactionManager的引用和事務屬性。 事務屬性含有上面描述的事務定義。 
PlatformTransactionManager: 
HibernateTransactionManager需要一個SessionFactory的引用 
JtaTransactionManager 
一.把事務放置在了DAO層: 
Java代碼   <!—hibernateTransactionManager-->   <bean id="transactionManager"          class="org.springframework.orm.hibernate3.HibernateTransactionManager">          <property name="sessionFactory">                 <ref local="sessionFactory" />          </property>   </bean>   <!—DAO層介面實現-->   <bean id="companyDAOTarget"          class="com.vstsoft.querycompany.dao.impl.CompanyDAOImpl">          <property name="sessionFactory">                 <ref local="sessionFactory" />          </property>   </bean>   <!—spring對DAO層的事務代理-->   <bean id="companyDAOProxy"          class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">          <property name="transactionManager">                 <ref bean="transactionManager" />          </property>          <property name="target">                 <ref local="companyDAOTarget" />          </property>          <property name="transactionAttributes">                 <props>                 <prop key="insert*">PROPAGATION_REQUIRED</prop>                 <prop key="delete*">PROPAGATION_REQUIRED</prop>                 <prop key="find*">                 PROPAGATION_REQUIRED,readOnly                 </prop>                 </props>          </property>   </bean>   <!—業務層介面實現,把DAO注入到Service裡面-->   <bean name="companyManageTarget"          class="com.vstsoft.querycompany.service.impl.CompanyManageTarget">          <property name="companyDAO">                 <ref bean="companyDAOProxy" />          </property>   </bean>   <!—spring對Service層的代理-->   <bean id="companyManageProxy"          class="org.springframework.aop.framework.ProxyFactoryBean">          <property name="proxyInterfaces">                 <value>com.vstsoft.querycompany.service.CompanyManage</value>          </property>          <property name="target">                 <ref bean="companyManageTarget" />          </property>   </bean>   <!—配置struts訪問,把service層注入到action裡面-->   <bean name="/company"          class="com.vstsoft.querycompany.web.action.CompanyAction" singleton="false">          <property name="companyManage">                 <ref local="companyManageProxy" />          </property>   </bean>  

二.把事務放置在了Service層: 
Java代碼   <!—jtaTransactionManager-->   <bean id="jtaTransactionManager"          class="org.springframework.transaction.jta.JtaTransactionManager" />   <!—DAO層介面實現-->   <bean id="companyDAOTarget"          class="com.vstsoft.querycompany.dao.impl.CompanyDAOImpl">          <property name="sessionFactory">                 <ref local="sessionFactory" />          </property>   </bean>   <!—spring對DAO層的代理-->   <bean id="companyDAOProxy"          class="org.springframework.aop.framework.ProxyFactoryBean">          <property name="proxyInterfaces">                 <value>com.vstsoft.querycompany.dao.CompanyDAO</value>  

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.