## ***第一次方式:***
@Aspect
@Configuration
public class TransactionAdviceConfig {
private static final int TX_METHOD_TIME_OUT = 120;
private static final String POITCUT_EXPRESSION = "execution (* com..service.*.*(..))";
@Autowired
private PlatformTransactionManager platformTransactionManager;
public TransactionAdviceConfig() {
}
@Bean
public TransactionInterceptor txAdvice() {
NameMatchTransactionAttributeSource source = new NameMatchTransactionAttributeSource();
Map<String, TransactionAttribute> nameMap = new HashMap(16);
RuleBasedTransactionAttribute readOnlyRule = new RuleBasedTransactionAttribute();
readOnlyRule.setReadOnly(true);
readOnlyRule.setPropagationBehavior(0);
RuleBasedTransactionAttribute requireRule = new RuleBasedTransactionAttribute();
requireRule.setRollbackRules(Collections.singletonList(new RollbackRuleAttribute(Exception.class)));
requireRule.setPropagationBehavior(0);
requireRule.setTimeout(120);
nameMap.put("add*", requireRule);
nameMap.put("save*", requireRule);
nameMap.put("insert*", requireRule);
nameMap.put("update*", requireRule);
nameMap.put("delete*", requireRule);
nameMap.put("create*", requireRule);
nameMap.put("active*", requireRule);
nameMap.put("approve*", requireRule);
nameMap.put("remove*", requireRule);
nameMap.put("invoke*", requireRule);
nameMap.put("on*", requireRule);
nameMap.put("handle*", requireRule);
nameMap.put("restore*", requireRule);
nameMap.put("modify*", requireRule);
nameMap.put("charge*", requireRule);
nameMap.put("refund*", requireRule);
nameMap.put("payment*", requireRule);
nameMap.put("batch*", requireRule);
nameMap.put("get*", readOnlyRule);
nameMap.put("query*", readOnlyRule);
nameMap.put("*Query", readOnlyRule);
nameMap.put("find*", readOnlyRule);
nameMap.put("select*", readOnlyRule);
nameMap.put("count*", readOnlyRule);
nameMap.put("load*", readOnlyRule);
source.setNameMap(nameMap);
return new TransactionInterceptor(this.platformTransactionManager, source);
}
@Bean
public Advisor txAdviceAdvisor() {
AspectJExpressionPointcut pointcut = new AspectJExpressionPointcut();
pointcut.setExpression("execution (* com..service.*.*(..))");
return new DefaultPointcutAdvisor(pointcut, this.txAdvice());
}
}
## ***第二种方式:***
@Configuration
public class TransactionConfiguration {
public static final String AOP_POINTCUT_EXPRESSION = "execution(* com.github.modules..service.impl.*(..))";
@Autowired
private PlatformTransactionManager platformTransactionManager;
/**
* 获取事务配置
*
* @return
*/
public static Properties getAttrubites() {
Properties attributes = new Properties();
//查询
attributes.setProperty("get*", "PROPAGATION_REQUIRED,-Throwable,readOnly");
attributes.setProperty("query*", "PROPAGATION_REQUIRED,-Throwable,readOnly");
attributes.setProperty("find*", "PROPAGATION_REQUIRED,-Throwable,readOnly");
attributes.setProperty("select*", "PROPAGATION_REQUIRED,-Throwable,readOnly");
//添加
attributes.setProperty("add*", "PROPAGATION_REQUIRED,-Exception");
attributes.setProperty("insert*", "PROPAGATION_REQUIRED,-Exception");
attributes.setProperty("save*", "PROPAGATION_REQUIRED,-Exception");
attributes.setProperty("create*", "PROPAGATION_REQUIRED,-Exception");
//更新
attributes.setProperty("update*", "PROPAGATION_REQUIRED,-Exception");
attributes.setProperty("modify*", "PROPAGATION_REQUIRED,-Exception");
//删除
attributes.setProperty("delete*", "PROPAGATION_REQUIRED,-Exception");
attributes.setProperty("remove*", "PROPAGATION_REQUIRED,-Exception");
return attributes;
}
/**
* 采用注解实例化的拦截器Bean,注入切面配置信息
*/
@Bean
public DefaultPointcutAdvisor defaultPointcutAdvisor(TransactionInterceptor ti){
ti.setTransactionManager(platformTransactionManager);
ti.setTransactionAttributes(getAttrubites());
AspectJExpressionPointcut pointcut = new AspectJExpressionPointcut();
pointcut.setExpression(AOP_POINTCUT_EXPRESSION);
return new DefaultPointcutAdvisor(pointcut, ti);
}
}
springboot 自定义事务切面
最新推荐文章于 2024-05-12 15:55:15 发布