大多数网站上都让用注解来实现事物控制,其实比较麻烦,还容易漏掉,于是开始研究如何实现声明式事物,其实要搞也简单一个类搞定。
@Configuration
@AutoConfigureAfter(E6DynamicDataSourceConfiguration.class)
public class TransacationConfig {
private ApplicationContext applicationContext;
public TransacationConfig(ApplicationContext applicationContext)
{
this.applicationContext = applicationContext;
}
/**
* 事物拦截器 千万不要声明一个datasourcetransacationmanager 因为jpa带一个了
*/
public TransactionInterceptor getAdvisor() {
Properties properties = new Properties();
properties.setProperty("get*", "PROPAGATION_REQUIRED,-Exception,readOnly");
properties.setProperty("find*", "PROPAGATION_REQUIRED,-Exception,readOnly");
properties.setProperty("select*", "PROPAGATION_REQUIRED,-Exception,readOnly");
properties.setProperty("insert*", "PROPAGATION_REQUIRED,-Exception");
properties.setProperty("add*", "PROPAGATION_REQUIRED,-Exception");
properties.setProperty("save*", "PROPAGATION_REQUIRED,-Exception");
properties.setProperty("create*", "PROPAGATION_REQUIRED,-Exception");
properties.setProperty("update*", "PROPAGATION_REQUIRED,-Exception");
properties.setProperty("del*", "PROPAGATION_REQUIRED,-Exception");
TransactionInterceptor result = new TransactionInterceptor(null, properties);
result.setBeanFactory(this.applicationContext);
return result;
}
private static final String AOP_POINTCUT_EXPRESSION = "execution (* com.e6yun.project..service.impl.*.*(..)) && !execution (* com.e6yun.project.common.module.base.user.service.impl.*.*(..)) ";
@Bean
public Advisor txAdviceAdvisor() {
AspectJExpressionPointcut pointcut = new AspectJExpressionPointcut();
pointcut.setExpression(AOP_POINTCUT_EXPRESSION);
return new DefaultPointcutAdvisor(pointcut, getAdvisor());
}
}