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

spring事物配置示例

2022-06-13   來源: Java開源技術 

  以前項目中經常用spring事務處理還沒有親自配置過 慚愧現在馬上上路.

  首先在spring容器中配置transactionManager這個有好多實現這裡以HibernateTransactionManager為例

    <bean id=transactionManager class=orgspringframeworkormhibernateHibernateTransactionManager>
            <property name=sessionFactory>
                <ref local=sessionFactory />
            </property>
        </bean> 

    然後再定義一個事務模板

 <bean id=txProxyTemplate abstract=true class=orgspringframeworktransactioninterceptorTransactionProxyFactoryBean>
            <property name=transactionManager>
                <ref bean=transactionManager />
            </property>
            <property name=transactionAttributes>
                <props>
                    <prop key=save*>PROPAGATION_REQUIREDException</prop>
                    <prop key=remove*>PROPAGATION_REQUIREDException</prop>
                    <prop key=update*>PROPAGATION_REQUIREDException</prop>
                    <prop key=incress*>PROPAGATION_REQUIREDException</prop>
                    <prop key=*>PROPAGATION_REQUIRED</prop>
                </props>
            </property>
        </bean>
    這個模板怎樣應用到業務方法上呢?請看下面的配置

    <!
            <bean id=userService class=comsclschserviceimplUserServiceImpl>
            <property name=userDao>
            <ref bean=BmUserDAO />
            </property>
            </bean>
        >
        <! 為userService配置事務>
        <bean id=userService parent=txProxyTemplate>
            <property name=target>
                <bean class=comsclschserviceimplUserServiceImpl>
                    <property name=userDao>
                        <ref bean=BmUserDAO />
                    </property>
                </bean>
            </property>
        </bean>

  注釋部分是原來沒有配置事務的service parent指定為這個service配置的事務模板.


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

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