詳解Spring學(xué)習(xí)之聲明式事務(wù)管理
前言
在前面的小節(jié)中,我們學(xué)習(xí)了關(guān)于事務(wù)的概念以及事務(wù)管理的重要性,并且通過編程使用Spring的編程式事務(wù)管理進(jìn)行操作,加深對事務(wù)管理的重要性的學(xué)習(xí),不過,由于編程式的事務(wù)管理使用起來不是很方便,所以在日常的開發(fā)中基本不怎么使用,接下來的內(nèi)容我們將學(xué)習(xí)使用Spring的聲明式事務(wù)管理,這里有一個地方需要明白的是,Spring的聲明式事務(wù)管理的實(shí)現(xiàn)方式其實(shí)是通過AOP的方式來實(shí)現(xiàn)的,也就是為原始的事務(wù)管理對象創(chuàng)建代理對象,從而實(shí)現(xiàn)事務(wù)管理增強(qiáng)的
基于TransactionProxyFactoryBean的事務(wù)管理配置
經(jīng)過前面的學(xué)習(xí),可以知道,Spring中配置AOP有三種方式,分別是通過ProxyFactoryBean創(chuàng)建代理,通過XML的方式以及通過注解的方式,既然Spring事務(wù)管理是通過AOP來實(shí)現(xiàn)的,那么對應(yīng)的就有三種不同的方式,首先來看下基于TransactionProxyFactoryBean的管理方式
首先是Spring的配置文件
<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!--開啟自動掃描-->
<context:component-scan base-package="cn.xuhuanfeng.transaction"/>
<!--配置數(shù)據(jù)源,這里采用dbcp-->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="url" value="jdbc:mysql://localhost:3306/spring"/>
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="username" value="root"/>
<property name="password" value="huanfeng"/>
</bean>
<!--配置JdbcTemplate-->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<!--注入數(shù)據(jù)源-->
<property name="dataSource" ref="dataSource"/>
</bean>
<!--配置事務(wù)管理-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!--注入數(shù)據(jù)源-->
<property name="dataSource" ref="dataSource"/>
</bean>
<!--為AccountService創(chuàng)建代理類-->
<bean id="accountServiceProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<!--注入事務(wù)管理-->
<property name="transactionManager" ref="transactionManager"/>
<!--注入目標(biāo)類,也就是所要增強(qiáng)的類-->
<property name="target" ref="accountService"/>
<!--配置相應(yīng)的事務(wù)屬性-->
<property name="transactionAttributes">
<props>
<!--指定不同的事務(wù)的處理方式
配置格式:事務(wù)傳播方式,隔離級別,readOnly,-Exception,+Exception
傳播行為是唯一必須配置的,其他的如果不配置則使用默認(rèn)
-Exception表示如果發(fā)生對應(yīng)的異常,則回滾事務(wù)
+Exception表示即使發(fā)生對應(yīng)的異常,也依舊提交事務(wù)
-->
<prop key="transfer">PROPAGATION_REQUIRED,ISOLATION_DEFAULT</prop>
</props>
</property>
</bean>
</beans>
對應(yīng)的持久層代碼
@Repository
public class AccountDao {
@Autowired
private JdbcTemplate jdbcTemplate;
public void transferIn(String name, double money){
String sql = "update account set money = money + ? where name = ?";
jdbcTemplate.update(sql, money, name);
}
public void transferOut(String name, double money){
String sql = "update account set money = money - ? where name = ?";
jdbcTemplate.update(sql, money, name);
}
}
業(yè)務(wù)層代碼
@Service
public class AccountService {
@Autowired
private AccountDao accountDao;
public void transfer(final String fromName,final String toName,final double money){
accountDao.transferOut(fromName, money);
int d = 1/0; // 除0異常
accountDao.transferIn(toName, money);
}
}
通過上面的配置之后,當(dāng)我們在使用AccountService的時候,由于獲取的對象的代理后的對象,所以Spring會自動進(jìn)行事務(wù)的監(jiān)管,而我們需要做的就是配置對應(yīng)的事務(wù)傳播類型以及事務(wù)管理級別等的信息,這種方式明顯對代碼以及沒有什么侵入了,但是使用這種方式意味著沒有都需要為不同的服務(wù)對象創(chuàng)建對應(yīng)的代理對象,這其實(shí)是不太方便的,接下來我們來看下使用aop/tx命名空間來進(jìn)行配置的方式。
基于aop/tx命名空間的事務(wù)管理配置
由于是對上面的業(yè)務(wù)操作進(jìn)行事務(wù)管理,而且經(jīng)過上一小節(jié)的學(xué)習(xí),我們也基本熟悉了該業(yè)務(wù),所以這里直接演示配置的代碼
<?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:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<!--
這里配置同前,故省略
-->
<!--aop配置-->
<aop:config>
<!--配置切點(diǎn)-->
<aop:pointcut id="serviceMethod" expression="execution(* cn.xuhuanfeng.transaction.AccountService.*(..))"/>
<!--對應(yīng)的切面-->
<aop:advisor advice-ref="txAdvice" pointcut-ref="serviceMethod"/>
</aop:config>
<!--配置事務(wù)增強(qiáng)-->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<!--配置對應(yīng)的事務(wù)管理,其中name為與事務(wù)相關(guān)的方法名,可以使用通配符-->
<tx:method name="transfer*" isolation="DEFAULT" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
</beans>
可以看到,通過XML配置的方式,可以更加靈活地進(jìn)行事務(wù)管理
基于注解的事務(wù)管理配置
基于注解的配置方式提供了更加簡單的配置方式,只需要使用@Transactional注解進(jìn)行標(biāo)注,并且開啟對應(yīng)的掃描即可。
// 配置相應(yīng)的隔離級別、事務(wù)傳播等
@Transactional(isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED)
@Service
public class AccountService {
// 省略其他內(nèi)容
}
Spring配置文件也相對比較簡單了
<?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:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!--數(shù)據(jù)源配置等同上-->
<!--通過tx命名空間,開啟主機(jī)自動掃描,并且注入事務(wù)管理器-->
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>
可以看到,通過注解配置的方式是最簡單的配置方式,在日常的開發(fā)中,這種方式的使用的頻率也比較高
總結(jié)
本小節(jié)主要學(xué)習(xí)了Spring聲明式事務(wù)管理的配置,包括了使用TransactionProxyFactoryBean、通過aop/tx命名空間的XML配置以及基于注解的配置方式,其中,基于注解的配置方式是比較簡單的,也是使用頻率比較高的一種
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Spring的編程式事務(wù)和聲明式事務(wù)詳解
- Spring聲明式事務(wù)和@Aspect的攔截順序問題的解決
- spring聲明式事務(wù)管理解析
- Spring編程式和聲明式事務(wù)實(shí)例講解小結(jié)
- spring聲明式事務(wù)解析
- spring 聲明式事務(wù)實(shí)現(xiàn)過程解析
- Spring實(shí)戰(zhàn)之使用TransactionProxyFactoryBean實(shí)現(xiàn)聲明式事務(wù)操作示例
- Spring實(shí)戰(zhàn)之使用注解實(shí)現(xiàn)聲明式事務(wù)操作示例
- 關(guān)于Spring中聲明式事務(wù)的使用詳解
相關(guān)文章
學(xué)習(xí)Java之如何對時間進(jìn)行格式化
當(dāng)我們在默認(rèn)情況下構(gòu)造出來的時間對象,它的時間格式并不適合我們閱讀,并且在開發(fā)時,pc端、Android端、iOS端等展示的時間格式可能也并不完全一樣,本文就從這幾個問題給大家介紹如何對時間進(jìn)行格式化,感興趣的同學(xué)可以借鑒一下2023-05-05
監(jiān)控Spring Boot 項目運(yùn)行情況操作方法
在實(shí)際開發(fā)中,經(jīng)常會遇到想要獲取到服務(wù)器應(yīng)用的運(yùn)行情況的場景,在微服務(wù)架構(gòu)下對于每個應(yīng)用運(yùn)行情況的監(jiān)控是保證系統(tǒng)高可用的關(guān)鍵,本文給大家介紹如何實(shí)現(xiàn)在Spring Boot的jar包中對系統(tǒng)的運(yùn)行情況進(jìn)行監(jiān)控操作,感興趣的朋友跟隨小編一起看看吧2024-08-08
解決IDEA報錯,無效的源發(fā)行版 無效的目標(biāo)發(fā)行版:22問題
在項目編譯過程中,可能會出現(xiàn)“無效的源發(fā)行版”或“無效的目標(biāo)發(fā)行版”的報錯信息,原因通常是編譯使用的JDK版本與項目設(shè)置的發(fā)布版本不一致,解決這類問題的辦法是統(tǒng)一JDK版本,具體操作為:在IDE的項目設(shè)置中(如File->ProjectStructure->ProjectSettings)2024-10-10
Java數(shù)據(jù)結(jié)構(gòu)與算法之單鏈表深入理解
這篇文章主要介紹了Java數(shù)據(jù)結(jié)構(gòu)與算法之單鏈表深入理解,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-09-09
解決ObjectMapper.convertValue() 遇到的一些問題
這篇文章主要介紹了解決ObjectMapper.convertValue() 遇到的一些問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-06-06
SpringCloud?分布式微服務(wù)架構(gòu)操作步驟
SpringCloud是一種微服務(wù)的框架,利用它我們可以去做分布式服務(wù)開發(fā),這篇文章主要介紹了SpringCloud?分布式微服務(wù)架構(gòu),需要的朋友可以參考下2022-07-07
java使用鏈表實(shí)現(xiàn)約瑟夫環(huán)
這篇文章主要為大家詳細(xì)介紹了java使用鏈表實(shí)現(xiàn)約瑟夫環(huán),具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-05-05
SpringBoot解決BigDecimal傳到前端后精度丟失問題
這篇文章將通過示例詳細(xì)為大家介紹SpringBoot如何解決BigDecimal傳到前端后精度丟失問題,文中的示例代碼講解詳細(xì),感興趣的可以了解一下2022-06-06

