使用注解版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環繞通知
執行順序按照
二、使用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 框架為我們提供的一種可以在代碼中手動控制增強代碼什么時候執行的方式。
注意: 通常情況下,環繞通知都是獨立使用的
總結
以上是生活随笔為你收集整理的使用注解版AOP解决事务问题的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ddos反向代理(ddos反向)
- 下一篇: @Param注解注意事项(小的细节)