1. 程式人生 > >使用spring的aop實現全域性的事務控制

使用spring的aop實現全域性的事務控制

1.springboot實現事務只需要 在頭上加上@Transactional註解

@Transactional 預設只捕獲RuntimeException.class

對Exception異常得需要 @Transactional(rollbackFor = {Exception.class}) 捕獲回滾。

2.當專案特別大的時候,對所有的service 都加上事務 ,顯得非常麻煩。可以通過aop 方式實現全域性異常。程式碼如下。

這樣就能專注寫業務邏輯然後注意一下方法名就行了,當然也可以用

source.addTransactionalMethod("*", txAttr_REQUIRED); 匹配所有方法,但是並不是所有的方法都需要事務的。所有推薦匹配方法名的方法


package test.spring.config;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import org.springframework.aop.Advisor;
import org.springframework.aop.aspectj.AspectJExpressionPointcut;
import org.springframework.aop.support.DefaultPointcutAdvisor;
import org.springframework.
beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.transaction.PlatformTransactionManager; import org.springframework.transaction.TransactionDefinition; import org.springframework.
transaction.interceptor.NameMatchTransactionAttributeSource; import org.springframework.transaction.interceptor.RollbackRuleAttribute; import org.springframework.transaction.interceptor.RuleBasedTransactionAttribute; import org.springframework.transaction.interceptor.TransactionAttribute; import org.springframework.transaction.interceptor.TransactionInterceptor; /** * @author yc * @data 2018年7月28日 * @description 通過AOP切面設定全域性事務,攔截service包下面所有方法 * AOP術語:通知(Advice)、連線點(Joinpoint)、切入點(Pointcut)、切面(Aspect)、目標(Target)、代理(Proxy)、織入(Weaving) */ @Configuration public class TransactionManagerConfig { private static final int TX_METHOD_TIMEOUT=5; /*定義切點變數:攔截test.spring包下所有類的所有方法,返回值型別任意的方法*/ private static final String AOP_POINTCUT_EXPRESSION="execution (* test.spring.service..*(..))"; @Autowired private PlatformTransactionManager transactionManager; /** * @author yc * @data 2018年7月28日 * @description springBoot事務配置 */ @Bean public TransactionInterceptor TxAdvice(){ /*事務管理規則,宣告具備事務管理的方法名*/ NameMatchTransactionAttributeSource source=new NameMatchTransactionAttributeSource(); /*只讀事物、不做更新刪除等*/ /*當前存在事務就用當前的事務,當前不存在事務就建立一個新的事務*/ RuleBasedTransactionAttribute readOnlyRule=new RuleBasedTransactionAttribute(); /*設定當前事務是否為只讀事務,true為只讀*/ readOnlyRule.setReadOnly(true); /* transactiondefinition 定義事務的隔離級別; * PROPAGATION_NOT_SUPPORTED事務傳播級別5,以非事務執行,如果當前存在事務,則把當前事務掛起*/ readOnlyRule.setPropagationBehavior(TransactionDefinition.PROPAGATION_NOT_SUPPORTED); RuleBasedTransactionAttribute requireRule=new RuleBasedTransactionAttribute(); /*丟擲異常後執行切點回滾*/ requireRule.setRollbackRules(Collections.singletonList(new RollbackRuleAttribute(Exception.class))); /*PROPAGATION_REQUIRED:事務隔離性為1,若當前存在事務,則加入該事務;如果當前沒有事務,則建立一個新的事務。這是預設值。 */ requireRule.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED); /*設定事務失效時間,如果超過5秒,則回滾事務*/ requireRule.setTimeout(TX_METHOD_TIMEOUT); Map<String,TransactionAttribute> txMap=new HashMap<>(); txMap.put("add*",requireRule); txMap.put("save*", requireRule); txMap.put("insert*",requireRule); txMap.put("update*",requireRule); txMap.put("delete*",requireRule); txMap.put("remove*",requireRule); txMap.put("get*",readOnlyRule); txMap.put("query*", readOnlyRule); txMap.put("find*", readOnlyRule); txMap.put("select*",readOnlyRule); source.setNameMap(txMap); TransactionInterceptor txAdvice=new TransactionInterceptor(transactionManager, source); return txAdvice; } /** * @author yc * @data 2018年7月27日 * @description 利用AspectJExpressionPointcut設定切面=切點+通知(寫成內部bean的方式) * */ @Bean public Advisor txAdviceAdvisor(){ /* 宣告切點的面 * 切面(Aspect):切面就是通知和切入點的結合。通知和切入點共同定義了關於切面的全部內容——它的功能、在何時和何地完成其功能。 * */ AspectJExpressionPointcut pointcut=new AspectJExpressionPointcut(); /*宣告和設定需要攔截的方法,用切點語言描寫*/ pointcut.setExpression(AOP_POINTCUT_EXPRESSION); /*設定切面=切點pointcut+通知TxAdvice*/ return new DefaultPointcutAdvisor(pointcut, TxAdvice()); } }