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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

spring事务管理-注解配置aop事务(重点)

發布時間:2024/4/13 编程问答 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 spring事务管理-注解配置aop事务(重点) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
導包,導入新的約束,通知都不用配了,這兩個是一樣的,導包這一步,新的約束,tx這些都是一樣的,第三步開始不同,這個注解配置大概一分鐘就講完了,第三步咱們可以開啟注解,管理事務,那這塊的話,咱們的配置文件,這個配置文件再來一份新的吧,在這里把剛才寫的,這里開啟使用注解管理事務,這里使用的是annotaion-driven,就配這么多就可以了,這個爽,這個就開了,第四步使用注解,使用注解的話看著,咱們要在transfer方法加注解,加一個@Transactional,事務就加上了,isolation等于REPEATABLE_READ,propagation等于REQUIRED,然后逗號還有什么,readOnly等于false,完事了,這樣的話事務就配完了,喜歡哪種,這種太多了,咱們試一下成功不成功,接下來咱們轉賬,咱們怎么試事務成功了沒有,把這個打開,這樣的話就報錯,還是800 1200,咱們的注解事務是不是也OK了,這兩種都是可以的,接下來還有個問題,有人的說這個注解細想一下也挺麻煩的,10個方法就要加10個注解,每一個方法加一個注解,如果你覺得這個麻煩的話你可以加載類上,這樣的話類中所有的業務方法,都應用這個策略了,下面的某個方法比如read的不是only,readOnly不是false,等于true,那怎么辦呢,咱們類上是true,如果不是true你再額外加一個,看明白啥意思不,有的在類上加也可以,在方法上加也可以,看明白啥意思不,這就是咱們的注解,你們喜歡注解的,那這樣的話咱們的AOP事務,這兩種方式,就講完了,把今天敲的AOP事務這個,回去以后你們要做任務,你們要把AOP這個配置,你們兩種方式,XML配置,和注解配置,這兩種分別實驗一下,實驗成果就算OK,甚至昨天你們AOP都能整完,這個東西敲完你們AOP就OK了,因為學AOP就是為了今天AOP事務,步驟給你們列出來了 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" 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-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd "><!-- 指定spring讀取db.properties配置 --> <context:property-placeholder location="classpath:db.properties" /><!-- 事務核心管理器,封裝了所有事務操作. 依賴于連接池 --> <bean name="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager" ><property name="dataSource" ref="dataSource" ></property> </bean><!-- 事務模板對象 --> <bean name="transactionTemplate" class="org.springframework.transaction.support.TransactionTemplate" ><property name="transactionManager" ref="transactionManager" ></property> </bean><!-- 開啟使用注解管理aop事務 --> <tx:annotation-driven/><!-- 1.連接池 --> <bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" ><property name="jdbcUrl" value="${jdbc.jdbcUrl}" ></property><property name="driverClass" value="${jdbc.driverClass}" ></property><property name="user" value="${jdbc.user}" ></property><property name="password" value="${jdbc.password}" ></property> </bean><!-- 2.Dao--> <bean name="accountDao" class="com.learn.dao.AccountDaoImpl" ><property name="dataSource" ref="dataSource" ></property> </bean> <!-- 3.Service--> <bean name="accountService" class="com.learn.service.AccountServiceImpl" ><property name="ad" ref="accountDao" ></property><property name="tt" ref="transactionTemplate" ></property> </bean> </beans> package com.learn.dao;public interface AccountDao {//加錢void increaseMoney(Integer id,Double money);//減錢void decreaseMoney(Integer id,Double money); } package com.learn.dao;import org.springframework.jdbc.core.support.JdbcDaoSupport;public class AccountDaoImpl extends JdbcDaoSupport implements AccountDao {@Overridepublic void increaseMoney(Integer id, Double money) {getJdbcTemplate().update("update t_account set money = money+? where id = ? ", money,id);}@Overridepublic void decreaseMoney(Integer id, Double money) {getJdbcTemplate().update("update t_account set money = money-? where id = ? ", money,id);}} package com.learn.service;public interface AccountService {//轉賬方法void transfer(Integer from,Integer to,Double money);} package com.learn.service;import org.springframework.transaction.annotation.Isolation; import org.springframework.transaction.annotation.Propagation; import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.support.TransactionTemplate;import com.learn.dao.AccountDao;@Transactional(isolation=Isolation.REPEATABLE_READ,propagation=Propagation.REQUIRED,readOnly=true) public class AccountServiceImpl implements AccountService {private AccountDao ad ;private TransactionTemplate tt;@Override@Transactional(isolation=Isolation.REPEATABLE_READ,propagation=Propagation.REQUIRED,readOnly=false)public void transfer(final Integer from,final Integer to,final Double money) {//減錢ad.decreaseMoney(from, money);int i = 1/0;//加錢ad.increaseMoney(to, money);}/* @Overridepublic void transfer(final Integer from,final Integer to,final Double money) {tt.execute(new TransactionCallbackWithoutResult() {@Overrideprotected void doInTransactionWithoutResult(TransactionStatus arg0) {//減錢ad.decreaseMoney(from, money);int i = 1/0;//加錢ad.increaseMoney(to, money);}});} */public void setAd(AccountDao ad) {this.ad = ad;}public void setTt(TransactionTemplate tt) {this.tt = tt;}} package com.learn.tx;import javax.annotation.Resource;import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;import com.learn.service.AccountService;@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("classpath:applicationContext.xml") public class Demo {@Resource(name="accountService")private AccountService as;@Testpublic void fun1(){as.transfer(1, 2, 100d);} } jdbc.jdbcUrl=jdbc:mysql:///day20 jdbc.driverClass=com.mysql.jdbc.Driver jdbc.user=root jdbc.password=123456

?

總結

以上是生活随笔為你收集整理的spring事务管理-注解配置aop事务(重点)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。