import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import org.aspectj.lang.annotation.Aspect;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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.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;
@Aspect
@Configuration
public class TransConfig {
private static final Logger logger = LoggerFactory.getLogger(TransConfig.class);
private static final int AOP_TIME_OUT = 50000;
private static final String AOP_POINTCUT_EXPRESSION = "execution(* com..*.service..*.*(..))";
@Autowired
private PlatformTransactionManager transactionManager;
public TransConfig() {
}
@Bean
public TransactionInterceptor txAdvice() {
NameMatchTransactionAttributeSource source = new NameMatchTransactionAttributeSource();
RuleBasedTransactionAttribute readOnlyTx = new RuleBasedTransactionAttribute();
readOnlyTx.setReadOnly(true);
readOnlyTx.setPropagationBehavior(0);
RuleBasedTransactionAttribute requiredTx = new RuleBasedTransactionAttribute();
requiredTx.setRollbackRules(Collections.singletonList(new RollbackRuleAttribute(Exception.class)));
requiredTx.setPropagationBehavior(0);
requiredTx.setTimeout(50000);
Map<String, TransactionAttribute> methodMap = new HashMap();
methodMap.put("tx*", requiredTx);
source.setNameMap(methodMap);
TransactionInterceptor txAdvice = new TransactionInterceptor(this.transactionManager, source);
return txAdvice;
}
@Bean(
name = {"txAdviceAdvisor"}
)
public Advisor txAdviceAdvisor() {
logger.info("===============================創建txAdviceAdvisor===================================");
AspectJExpressionPointcut pointcut = new AspectJExpressionPointcut();
pointcut.setExpression("execution(* com..*.service..*.*(..))");
return new DefaultPointcutAdvisor(pointcut, this.txAdvice());
}
}
springBoot事務切面Aop
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.