生活随笔
收集整理的這篇文章主要介紹了
Spring基于Annotation实现事务管理
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
在 Spring 中,除了使用基于 XML 的方式可以實現聲明式事務管理以外,還可以通過 Annotation 注解的方式實現聲明式事務管理。
使用 Annotation 的方式非常簡單,只需要在項目中做兩件事,具體如下。
1 在 Spring 容器中注冊驅動,代碼如下所示:
<tx
:annotation
-driven transaction
-manager
="txManager"/>
2 在需要使用事務的業務類或者方法中添加注解 @Transactional,并配置 @Transactional 的參數。關于 @Transactional 的參數如下圖所示。
下面通過修改Spring聲明式事務管理(點擊跳轉至Spring聲明式事務管理)中銀行轉賬的案例講解如何使用 Annotation 注解的方式實現 Spring 聲明式事務管理。
1. 注冊驅動
修改 Spring 配置文件 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
:tx
="http://www.springframework.org/schema/tx"xmlns
:aop
="http://www.springframework.org/schema/aop"xsi
:schemaLocation
="http
://www
.springframework
.org
/schema
/beanshttp
://www
.springframework
.org
/schema
/beans
/spring
-beans
-2.5.xsd http
://www
.springframework
.org
/schema
/contexthttp
://www
.springframework
.org
/schema
/context
/spring
-context
.xsdhttp
://www
.springframework
.org
/schema
/txhttp
://www
.springframework
.org
/schema
/tx
/spring
-tx
-2.5.xsdhttp
://www
.springframework
.org
/schema
/aophttp
://www
.springframework
.org
/schema
/aop
/spring
-aop
-2.5.xsd"
><!-- 加載properties文件
--><context
:property
-placeholder location
="classpath:c3p0-db.properties" /><!-- 配置數據源,讀取properties文件信息
--><bean id
="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"><property name
="driverClass" value
="${jdbc.driverClass}" /><property name
="jdbcUrl" value
="${jdbc.jdbcUrl}" /><property name
="user" value
="${jdbc.user}" /><property name
="password" value
="${jdbc.password}" /></bean
><!-- 配置jdbc模板
--><bean id
="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"><property name
="dataSource" ref
="dataSource" /></bean
><!-- 配置dao
--><bean id
="accountDao" class="com.mengma.dao.impl.AccountDaoImpl"><property name
="jdbcTemplate" ref
="jdbcTemplate" /></bean
><!-- 配置service
--><bean id
="accountService" class="com.mengma.service.impl.AccountServiceImpl"><property name
="accountDao" ref
="accountDao" /></bean
><!-- 事務管理器,依賴于數據源
--><bean id
="txManager"class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><property name
="dataSource" ref
="dataSource" /></bean
><!-- 注冊事務管理驅動
--><tx
:annotation
-driven transaction
-manager
="txManager"/>
</beans
>
上述代碼中可以看出,與原來的配置文件相比,這里只修改了事務管理器部分,新添加并注冊了事務管理器的驅動。
注意,在學習 AOP 注解方式開發時,需要在配置文件中開啟注解處理器,指定掃描哪些包下的注解,這里沒有開啟注解處理器是因為在第 33~35 行手動配置了 AccountServiceImpl,而 @Transactional 注解就配置在該類中,所以會直接生效。
2. 添加 @Transactional 注解
修改 AccountServiceImpl,在文件中添加 @Transactional 注解及參數,添加后如下所示。
package com
.mengma
.service
.impl
;import org
.springframework
.transaction
.annotation
.Isolation
;
import org
.springframework
.transaction
.annotation
.Propagation
;
import org
.springframework
.transaction
.annotation
.Transactional
;import com
.mengma
.dao
.AccountDao
;@Transactional(propagation
= Propagation
.REQUIRED
, isolation
= Isolation
.DEFAULT
, readOnly
= false)
public class AccountServiceImpl {private AccountDao accountDao
;public void setAccountDao(AccountDao accountDao
) {this.accountDao
= accountDao
;}public void transfer(String outUser
, String inUser
, int money
) {this.accountDao
.out(outUser
, money
);int i
= 1 / 0;this.accountDao
.in(inUser
, money
);}
}
注意,在使用 @Transactional 注解時,參數之間用“,”進行分隔。
使用 JUnit 測試再次運行 test() 方法時,控制臺同樣會輸出如下圖所示的異常信息,這說明使用基于 Annotation 注解的方式同樣實現了 Spring 的聲明式事務管理。如果注釋掉模擬斷電的代碼進行測試,則轉賬操作可以正常完成。
總結
以上是生活随笔為你收集整理的Spring基于Annotation实现事务管理的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。