开发中,我们一般会利用AOP配置全局性的事务,对指定包下指定的方法(如add,update等)进行事务控制,在springboot中如何实现呢?
@EnableTransactionManagement
@Aspect
@Configuration
public class GlobalTransactionConfig {
//写事务的超时时间为10秒
private static final int TX_METHOD_TIMEOUT = 10;
//restful包下所有service包或者service的子包的任意类的任意方法
private static final String AOP_POINTCUT_EXPRESSION = "execution (* com.jun.cloud.restful..*.service..*.*(..))";
@Autowired
private PlatformTransactionManager transactionManager;
@Bean
public TransactionInterceptor txAdvice() {
/**
* 这里配置只读事务
*/
RuleBasedTransactionAttribute readOnlyTx = new RuleBasedTransactionAttribute();
readOnlyTx.setReadOnly(true);
readOnlyTx.setPropagationBeh