package com.mandui.framework.aspect;
import org.aspectj.lang.annotation.Aspect;
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.context.annotation.Lazy;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.interceptor.*;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
@Aspect
@Configuration
public class TransactionAaspect {
/**
* 指定需要切面事务的路径
* execution(* com.mandui.project.service.impl.*.*(..)) || execution(* com.mandui.test.service.impl.*.*(..))
*/
private static final String AOP_POINTCUT_EXPRESSION = "execution(* com.mandui.project..service..*.*(..))";
//private static final int TX_METHOD_TIMEOUT = 5;
@Lazy
@Autowired
private PlatformTransactionManager transactionManager;
@Bean
public TransactionInterceptor txAdvice() {
NameMatchTransactionAttributeSource source = new NameMatchTransactionAttributeSource();
RuleBasedTransactionAttribute readOnlyTx = new RuleBasedTransactionAttribute();
readOnlyTx.setReadOnly(true);
readOnlyTx.setPropagationBehavior(TransactionDefinition.PROPAGATION_NOT_SUPPORTED);
RuleBasedTransactionAttribute requiredTx = new RuleBasedTransactionAttribute();
requiredTx.setRollbackRules(Collections.singletonList(new RollbackRuleAttribute(Exception.class)));
requiredTx.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
//requiredTx.setTimeout(TX_METHOD_TIMEOUT);
Map<String, TransactionAttribute> txMap = new HashMap<String, TransactionAttribute>();
txMap.put("save*", requiredTx);
txMap.put("remove*", requiredTx);
txMap.put("update*", requiredTx);
txMap.put("batch*", requiredTx);
txMap.put("clear*", requiredTx);
txMap.put("add*", requiredTx);
txMap.put("append*", requiredTx);
txMap.put("modify*", requiredTx);
txMap.put("edit*", requiredTx);
txMap.put("insert*", requiredTx);
txMap.put("delete*", requiredTx);
txMap.put("do*", requiredTx);
txMap.put("create*", requiredTx);
txMap.put("select*", readOnlyTx);
txMap.put("get*", readOnlyTx);
txMap.put("valid*", readOnlyTx);
txMap.put("list*", readOnlyTx);
txMap.put("count*", readOnlyTx);
txMap.put("find*", readOnlyTx);
txMap.put("load*", readOnlyTx);
txMap.put("search*", readOnlyTx);
txMap.put("*", readOnlyTx);
source.setNameMap(txMap);
TransactionInterceptor txAdvice = new TransactionInterceptor();
txAdvice.setTransactionManager(transactionManager);
txAdvice.setTransactionAttributeSource(source);
return txAdvice;
}
@Bean
public Advisor txAdviceAdvisor() {
AspectJExpressionPointcut pointCut = new AspectJExpressionPointcut();
pointCut.setExpression(AOP_POINTCUT_EXPRESSION);
return new DefaultPointcutAdvisor(pointCut, txAdvice());
}
}
控制台