熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java開源技術 >> 正文

Spring配置事務在DAO層和業務邏輯層

2022-06-13   來源: Java開源技術 
Spring通過AOP實現聲明式事務管理通常通過TransactionProxyFactoryBean設置Spring事務代理我們需要一個目標對象包裝在事務代理中這個目標對象一般是一個普通Java對象的bean當我們定義TransactionProxyFactoryBean時必須提供一個相關的 PlatformTransactionManager的引用和事務屬性 事務屬性含有上面描述的事務定義
    PlatformTransactionManager
    HibernateTransactionManager需要一個SessionFactory的引用
    JtaTransactionManager
    一.把事務放置在了DAO層
     <!—hibernateTransactionManager>
<bean id=transactionManager
    class=orgspringframeworkormhibernateHibernateTransactionManager>
    <property name=sessionFactory>
        <ref local=sessionFactory />
    </property>
</bean>
<!—DAO層接口實現>
<bean id=companyDAOTarget
    class=comvstsoftquerycompanydaoimplCompanyDAOImpl>
    <property name=sessionFactory>
        <ref local=sessionFactory />
    </property>
</bean>
<!—spring對DAO層的事務代理>
<bean id=companyDAOProxy
    class=orgspringframeworktransactioninterceptorTransactionProxyFactoryBean>
    <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_REQUIREDreadOnly
        </prop>
        </props>
    </property>
</bean>
<!—業務層接口實現把DAO注入到Service裡面>
<bean name=companyManageTarget
    class=comvstsoftquerycompanyserviceimplCompanyManageTarget>
    <property name=companyDAO>
        <ref bean=companyDAOProxy />
    </property>
</bean>
<!—spring對Service層的代理>
<bean id=companyManageProxy
    class=orgspringframeworkaopframeworkProxyFactoryBean>
    <property name=proxyInterfaces>
        <value>comvstsoftquerycompanyserviceCompanyManage</value>
    </property>
    <property name=target>
        <ref bean=companyManageTarget />
    </property>
</bean>
<!—配置struts訪問把service層注入到action裡面>
<bean name=/company
    class=comvstsoftquerycompanywebactionCompanyAction singleton=false>
    <property name=companyManage>
        <ref local=companyManageProxy />
    </property>
</bean>
二.把事務放置在了Service層
<!—jtaTransactionManager>
<bean id=jtaTransactionManager
    class=orgspringframeworktransactionjtaJtaTransactionManager />
<!—DAO層接口實現>
<bean id=companyDAOTarget
    class=comvstsoftquerycompanydaoimplCompanyDAOImpl>
    <property name=sessionFactory>
        <ref local=sessionFactory />
    </property>
</bean>
<!—spring對DAO層的代理>
<bean id=companyDAOProxy
    class=orgspringframeworkaopframeworkProxyFactoryBean>
    <property name=proxyInterfaces>
        <value>comvstsoftquerycompanydaoCompanyDAO</value>
    </property>
    <property name=target>
        <ref bean=companyDAOTarget />
    </property>
</bean>
<!—業務層接口實現把DAO注入到Service裡面>
<bean name=companyManageTarget
    class=comvstsoftquerycompanyserviceimplCompanyManageTarget>
    <property name=companyDAO>
        <ref bean=companyDAOProxy />
    </property>
</bean>
<!—spring代理業務層的事務管理>
<bean id=companyManageProxy
    class=orgspringframeworktransactioninterceptorTransactionProxyFactoryBean>
    <property name=transactionManager>
        <ref local=jtaTransactionManager />
    </property>
    <property name=transactionAttributes>
        <props>
              <prop key=set*>PROPAGATION_REQUIRED</prop>
              <prop key=find*>PROPAGATION_REQUIREDreadOnly</prop>
        </props>
    </property>
    <property name=target>
        <ref bean=companyManageTarget />
    </property>
</bean>
<!—配置struts訪問把service層注入到action裡面>
<bean name=/company
    class=comvstsoftquerycompanywebactionCompanyAction singleton=false>
    <property name=companyManage>
        <ref local=companyManageProxy />
    </property>
</bean>
  在service層的接口實現CompanyManageImpl裡面有個setData方法按順序執行數據查詢數據刪除數據插入數據庫行為如果哪一步出異常(運行時異常)事務回滾只有所有行為都沒成功事務提交

From:http://tw.wingwit.com/Article/program/Java/ky/201311/28057.html
  • 上一篇文章:

  • 下一篇文章:
  • 推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.