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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > asp.net >内容正文

asp.net

Spring框架的事务管理之基于AspectJ的XML方式(重点掌握)

發布時間:2025/6/17 asp.net 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Spring框架的事务管理之基于AspectJ的XML方式(重点掌握) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1. 步驟一:恢復轉賬開發環境(轉賬開發環境見“https://www.cnblogs.com/wyhluckdog/p/10137283.html”) 2.步驟二:引入AOP的開發包
3.步驟三:引入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:jdbc="http://www.springframework.org/schema/jdbc"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/tx http://www.springframework.org/schema/tx/spring-tx.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"></beans>

  ?*?管理C3P0連接池

* 先引入C3P0的jar包* com.springsource.com.mchange.v2.c3p0-0.9.1.2.jar* 編寫配置文件<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"><property name="driverClass" value="com.mysql.jdbc.Driver"/><property name="jdbcUrl" value="jdbc:mysql:///spring_day03"/><property name="user" value="root"/><property name="password" value="root"/></bean>
4.步驟四:創建對應的包結構和類(具體內容見“https://www.cnblogs.com/wyhluckdog/p/10137283.html”) * com.huida.demo1* AccountService* AccountServlceImpl* AccountDao* AccountDaoImpl
5.步驟五:引入Spring的配置文件,將類配置到Spring中
<bean id="accountDao" class="com.huida.demo1.AccountDaoImpl"><property name="dataSource" ref="dataSource"/></bean><bean id="accountService" class="com.huida.demo1.AccountServiceImpl"><property name="accountDao" ref="accountDao"/> </bean>
6.步驟六:配置事務管理器 <!-- 配置事務管理器 --><bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><property name="dataSource" ref="dataSource"/></bean> 7.步驟七:配置事務增強 <!-- 配置事務增強 --><tx:advice id="txAdvice" transaction-manager="transactionManager"><tx:attributes><!--name :綁定事務的方法名,可以使用通配符,可以配置多個。propagation :傳播行為isolation :隔離級別read-only :是否只讀timeout :超時信息rollback-for:發生哪些異常回滾.no-rollback-for:發生哪些異常不回滾.--><!-- 哪些方法加事務 --><tx:method name="pay" propagation="REQUIRED"/></tx:attributes></tx:advice> 8.步驟八:書寫切面類MyAdvice:
package com.huida.demo1;public class MyAdvice {public void log(){System.out.println("添加日志");}}

? ?9.步驟九:配置AOP的切面

<bean id="myAdvice" class="com.huida.demo1.MyAdvice"></bean>
<
aop:config><aop:aspect ref="myAdvice"><aop:before method="log" pointcut="execution(* com.huida.demo1.AccountServiceImpl.pay(..))"/></aop:aspect></aop:config>

? 10.完整的配置文件的配置信息為:

<?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:jdbc="http://www.springframework.org/schema/jdbc"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/tx http://www.springframework.org/schema/tx/spring-tx.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"><bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"><property name="driverClass" value="com.mysql.jdbc.Driver"/><property name="jdbcUrl" value="jdbc:mysql:///spring-day03"/><property name="user" value="root"/><property name="password" value="root"/></bean><!-- 配置事務管理器 --><bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><property name="dataSource" ref="dataSource"/></bean><!-- 配置事務增強 --><tx:advice id="txAdvice" transaction-manager="transactionManager"><tx:attributes><tx:method name="pay" propagation="REQUIRED"/></tx:attributes></tx:advice><bean id="myAdvice" class="com.huida.demo1.MyAdvice"></bean><aop:config><aop:aspect ref="myAdvice"><aop:before method="log" pointcut="execution(* com.huida.demo1.AccountServiceImpl.pay(..))"/></aop:aspect></aop:config><bean id="accountDao" class="com.huida.demo1.AccountDaoImpl"><property name="dataSource" ref="dataSource"/></bean><bean id="accountService" class="com.huida.demo1.AccountServiceImpl"><property name="accountDao" ref="accountDao"/><!-- <property name="transactionTemplate" ref="transactionTemplate"/> --></bean></beans>

11.步驟十:編寫測試類

package com.huida.demo1;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;@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("classpath:applicationContext.xml") public class Demo1 {@Resource(name="accountService")private AccountService accountService;@Testpublic void run1(){accountService.pay("小明","小紅",1000);} }

12.單元測試run1方法,刷新spring-day03數據庫中的user表,可以看到小明的money減少了1000,而小紅的money增加了1000.



轉載于:https://www.cnblogs.com/wyhluckdog/p/10137712.html

總結

以上是生活随笔為你收集整理的Spring框架的事务管理之基于AspectJ的XML方式(重点掌握)的全部內容,希望文章能夠幫你解決所遇到的問題。

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