日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 前端技术 > javascript >内容正文

javascript

Spring声明式事务管理

發(fā)布時(shí)間:2025/1/21 javascript 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Spring声明式事务管理 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

事務(wù)管理方式

1.編碼方案 不建議使用,它具有侵入性。在原有的業(yè)務(wù)代碼基礎(chǔ)上去添加事務(wù)管理代碼
2. 聲明式事務(wù)控制,基于AOP對(duì)目標(biāo)進(jìn)行代理,添加around環(huán)繞通知。
這種方案,它不具有侵入性,不需要修改原來(lái)的業(yè)務(wù)代碼

基于xml配置聲明式事務(wù)管理方案

第一步:在applicationContext.xml文件中添加aop與tx的名稱空間

<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">

第二步:在applicationContext.xml文件中配置
Spring提供的advice是傳統(tǒng)的spring advice
1. 聲明事務(wù)管理器

<!-- 配置事務(wù)管理器 --><bean id="transactionManager"class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><property name="dataSource" ref="c3p0DataSource"></property></bean>

2.配置通知
Spring為我們提供了一個(gè)TransactionInterceptor來(lái)完成增強(qiáng)

對(duì)于這個(gè)增強(qiáng),我們可以使用spring為我們提供的一個(gè)標(biāo)簽< tx:advice>來(lái)完成操作

<!-- 配置通知 --><tx:advice id="txAdvice" transaction-manager="transactionManager"><tx:attributes><!-- name:必須的,對(duì)哪些方法進(jìn)行事務(wù)控制isolation 可選 設(shè)置事務(wù)隔離級(jí)別 默認(rèn)是DEFAULT propagation:可選 設(shè)置事務(wù)傳播 默認(rèn)值 REQUIREDtimeout 可選 超時(shí)時(shí)間 默認(rèn)值-1 read-only 可選 默認(rèn)值是false 如果不是只讀,它可以對(duì)insert update delete操作,如果是只讀不可以。rollback-for 可選 可以設(shè)置一個(gè)異常,如果產(chǎn)生這個(gè)異常,觸發(fā)事務(wù)回滾no-rolback-for 可選 可以設(shè)置一個(gè)異常,如果產(chǎn)生這個(gè)異常,不會(huì)觸發(fā)事務(wù)回滾--><tx:method name="account" /></tx:attributes></tx:advice>

3.配置切面
因?yàn)槭褂玫氖莻鹘y(tǒng)的spring的advice,需要使用

<!-- 配置切面 --><aop:config><aop:pointcut expression="execution(* cn.nwtxxb.service.IAccountService.account(..))" id="txPointcut"/><aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/></aop:config>

完整的applicationContext.xml配置

<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"><!-- 引入外部的properties文件 --><context:property-placeholder location="classpath:db.properties" /><!-- 創(chuàng)建c3p0連接 --><bean id="c3p0DataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"><property name="driverClass" value="${jdbc.driverClass}" /><property name="jdbcUrl" value="${jdbc.url}" /><property name="user" value="${jdbc.username}" /><property name="password" value="${jdbc.password}" /></bean><!-- service --><bean id="accountService" class="cn.nwtxxb.service.AccountServiceImpl"><property name="accountDao" ref="accountDao"></property></bean><!-- dao --><bean id="accountDao" class="cn.nwtxxb.dao.AccountDAOImpl"><!-- 當(dāng)注入dataSource后,底層會(huì)自動(dòng)創(chuàng)建一個(gè)JdbcTemplate --><property name="dataSource" ref="c3p0DataSource" /></bean><!-- 配置事務(wù)管理器 --><bean id="transactionManager"class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><property name="dataSource" ref="c3p0DataSource"></property></bean><!-- 配置通知 --><tx:advice id="txAdvice" transaction-manager="transactionManager"><tx:attributes><!-- name:必須的,對(duì)哪些方法進(jìn)行事務(wù)控制isolation 可選 設(shè)置事務(wù)隔離級(jí)別 默認(rèn)是DEFAULT propagation:可選 設(shè)置事務(wù)傳播 默認(rèn)值 REQUIREDtimeout 可選 超時(shí)時(shí)間 默認(rèn)值-1 read-only 可選 默認(rèn)值是false 如果不是只讀,它可以對(duì)insert update delete操作,如果是只讀不可以。rollback-for 可選 可以設(shè)置一個(gè)異常,如果產(chǎn)生這個(gè)異常,觸發(fā)事務(wù)回滾no-rolback-for 可選 可以設(shè)置一個(gè)異常,如果產(chǎn)生這個(gè)異常,不會(huì)觸發(fā)事務(wù)回滾--><tx:method name="account" /></tx:attributes></tx:advice><!-- 配置切面 --><aop:config><aop:pointcut expression="execution(* cn.nwtxxb.service.IAccountService.account(..))" id="txPointcut"/><aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/></aop:config><!--開啟事務(wù)管理注解驅(qū)動(dòng)--><tx:annotation-driven transaction-manager="transactionManager"/> </beans>

基于annotation聲明式事務(wù)管理方案

可以使用@Transaction來(lái)在類或方法上添加聲明式事務(wù)管理
注意:需要在applicationContext.xml文件中使用

<tx:annotation-driven transaction-manager="transactionManager"/>

相當(dāng)于開啟注解事務(wù)控制

問(wèn)題:關(guān)于xml方式與annotation方式的優(yōu)缺點(diǎn)?
從簡(jiǎn)單上來(lái)說(shuō),使用注解更方便。
使用配置的方案,可以對(duì)事務(wù)配置進(jìn)行集中維護(hù)。

與50位技術(shù)專家面對(duì)面20年技術(shù)見(jiàn)證,附贈(zèng)技術(shù)全景圖

總結(jié)

以上是生活随笔為你收集整理的Spring声明式事务管理的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。