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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

使用注解版AOP解决事务问题

發布時間:2023/12/3 编程问答 20 豆豆
生活随笔 收集整理的這篇文章主要介紹了 使用注解版AOP解决事务问题 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

一、注解版和xml版的區別

1、 通知的四種常用類型

(1)aop:before

作用: 用于配置前置通知。指定增強的方法在切入點方法之前執行 屬性: method:用于指定通知類中的增強方法名稱 ponitcut-ref:用于指定切入點的表達式的引用 poinitcut:用于指定切入點表達式 執行時間點: 切入點方法執行之前執行
<aop:before method=“beginPrintLog” pointcut-ref=“pt1”/>

(2)aop:after-returning

作用: 用于配置后置通知 屬性: method:指定通知中方法的名稱。 pointct:定義切入點表達式 pointcut-ref:指定切入點表達式的引用 執行時間點: 切入點方法正常執行之后。它和異常通知只能有一個執行
<aop:after-returning method=“afterReturningPrintLog” pointcut-ref=“pt1”/>

(3)aop:after-throwing

作用: 用于配置異常通知 屬性: method:指定通知中方法的名稱。 pointct:定義切入點表達式 pointcut-ref:指定切入點表達式的引用 執行時間點: 切入點方法執行產生異常后執行。它和后置通知只能執行一個
<aop:after-throwing method=“afterThrowingPringLog” pointcut-ref=“pt1”/>

(4) aop:after

作用: 用于配置最終通知 屬性: method:指定通知中方法的名稱。 pointct:定義切入點表達式 pointcut-ref:指定切入點表達式的引用 執行時間點: 無論切入點方法執行時是否有異常,它都會在其后面執行。
<aop:after method=“afterPringLog” pointcut-ref=“pt1”/>

2、四種常用類型通知的執行順序

(1)xml的織入順序是按照xml里的寫的順序進行執行

(2)注解版織入的順序則和運行時出現的情況進行分兩種分析
Ⅰ.正常運行

注解通知正常執行時:
正常執行時會先執行 @before 然后再執行 @After 最后執行@ After-returning

所以正常情況下要在 TransactionManager里的標注事務的狀態時對應的通知
如果一開始讓@After對應為release()則事務將提前釋放資源,造成無法提交,所以要把@After對應為Commit(),這樣就可以先提交再釋放資源

Ⅱ.拋出異常時 After 的執行順序

如果一開始讓@After對應為release()則事務將提前釋放資源,造成無法提交,所以要把@After對應為rollBack(),這樣就可以先拋異常再釋放資源

1.需要改動是文件

1.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"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/aop http://www.springframework.org/schema/aop/spring-aop.xsd"><!--開啟注解掃描--><context:component-scan base-package="com.william"></context:component-scan><!--創建QueryRunner--><bean id="QueryRunner" class="org.apache.commons.dbutils.QueryRunner"></bean><!--創建dataSource--><bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"><property name="driverClass" value="${jdbc.driverClass}"></property><property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property><property name="user" value="${jdbc.user}"></property><property name="password" value="${jdbc.password}"></property></bean><!--引入屬性文件--><context:property-placeholder location="classpath:db.properties"></context:property-placeholder><aop:aspectj-autoproxy proxy-target-class="true"></aop:aspectj-autoproxy> </beans>

2.在TransactionManager進行注解

代碼:使用的是四種的執行順序,沒使用around環繞通知
執行順序按照

package com.william.utils;import org.aspectj.lang.annotation.*; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component;import java.sql.SQLException;/*** @author :lijunxuan* @date :Created in 2019/5/27 16:50* @description :* @version: 1.0*/ @Component @Aspect public class TransactionManager {@Pointcut("execution(* com.william.service.Impl.*.*(..))")public void pc(){}@AutowiredConnectionUtils connectionUtils;@Before("pc()")public void beganTransaction(){try {connectionUtils.getThreadConnection().setAutoCommit(false);System.out.println(" beganTransaction "+connectionUtils.getThreadConnection());} catch (SQLException e) {e.printStackTrace();}}@After("pc()")public void Commit(){try {System.out.println(" Commit "+connectionUtils.getThreadConnection());connectionUtils.getThreadConnection().commit();} catch (SQLException e) {e.printStackTrace();}}@AfterThrowing("pc()")public void rollBack(){try {System.out.println(" rollBack "+connectionUtils.getThreadConnection());connectionUtils.getThreadConnection().rollback();} catch (SQLException e) {e.printStackTrace();}}@AfterReturning("pc()")public void release(){try {System.out.println(" release "+connectionUtils.getThreadConnection());connectionUtils.getThreadConnection().setAutoCommit(true);connectionUtils.getThreadConnection().close();connectionUtils.remove();} catch (SQLException e) {e.printStackTrace();}} }

二、使用around環繞通知可以解決上面的問題

配置方式:

<aop:config> <aop:pointcut expression="execution(* com.william.service.impl.*.*(..))" id="pt1"/> <aop:aspect id="txAdvice" ref="txManager"> <!-- 配置環繞通知 --> <aop:around method="transactionAround" pointcut-ref="pt1"/> </aop:aspect> </aop:config>

aop:around: 作用: 用于配置環繞通知
屬性:
method:指定通知中方法的名稱。
pointct:定義切入點表達式
pointcut-ref:指定切入點表達式的引用
說明: 它是 spring 框架為我們提供的一種可以在代碼中手動控制增強代碼什么時候執行的方式。
注意: 通常情況下,環繞通知都是獨立使用的

package com.william.utils;import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Pointcut; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component;import java.sql.SQLException;/*** @author :lijunxuan* @date :Created in 2019/5/27 16:50* @description :* @version: 1.0*/ @Component @Aspect public class TransactionManager {@Pointcut("execution(* com.william.service.impl.*.*(..))")public void pc(){}@Around("pc()")public Object around(ProceedingJoinPoint joinPoint){try {beginTransaction();//執行原始的方法Object result = joinPoint.proceed();commit();return result;} catch (Throwable throwable) {throwable.printStackTrace();rollback();} finally {release();}return null;}@AutowiredConnectionUtils connectionUtils;public void beginTransaction(){try {connectionUtils.getThreadConnection().setAutoCommit(false);} catch (SQLException e) {e.printStackTrace();}}public void commit(){try {connectionUtils.getThreadConnection().commit();} catch (SQLException e) {e.printStackTrace();}}public void rollback(){try {connectionUtils.getThreadConnection().rollback();} catch (SQLException e) {e.printStackTrace();}}public void release(){try {connectionUtils.getThreadConnection().setAutoCommit(true);connectionUtils.getThreadConnection().close();connectionUtils.remove();} catch (SQLException e) {e.printStackTrace();}} }

總結

以上是生活随笔為你收集整理的使用注解版AOP解决事务问题的全部內容,希望文章能夠幫你解決所遇到的問題。

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