1. pom
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-aop</artifactId> </dependency>
2. TransactionAdviceConfig 全局事物类
(* *.service.*.*(..))中几个通配符的含义:
1、execution(): 表达式主体。
2、第一个*号:表示返回类型,*号表示所有的类型。
3. com.spf.boot.service.impl 表示匹配所有的service包路径
4、第二个*号:表示类名,*号表示所有的类。
5、*(..):最后这个星号表示方法名,*号表示所有的方法,后面括弧里面表示方法的参数,两个句点表示任何参数。
package com.spf.boot.config; 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.transaction.PlatformTransactionManager; import org.springframework.transaction.TransactionDefinition; import org.springframework.transaction.interceptor.DefaultTransactionAttribute; import org.springframework.transaction.interceptor.NameMatchTransactionAttributeSource; import org.springframework.transaction.interceptor.TransactionInterceptor; import java.util.Properties; @Aspect @Configuration public class TransactionAdviceConfig { private static final String AOP_POINTCUT_EXPRESSION = "execution (* com.spf.boot.service.impl.*.*(..))"; @Autowired private PlatformTransactionManager transactionManager; @Bean public TransactionInterceptor txAdvice() { TransactionInterceptor tsi = new TransactionInterceptor(); Properties properties = new Properties(); properties.setProperty("get*", "PROPAGATION_SUPPORTS"); properties.setProperty("select*", "PROPAGATION_SUPPORTS"); properties.setProperty("load*", "PROPAGATION_SUPPORTS"); properties.setProperty("query*", "PROPAGATION_SUPPORTS"); properties.setProperty("list*", "PROPAGATION_SUPPORTS"); properties.setProperty("add*", "PROPAGATION_REQUIRED"); properties.setProperty("insert*", "PROPAGATION_REQUIRED"); properties.setProperty("save*", "PROPAGATION_REQUIRED"); properties.setProperty("update*", "PROPAGATION_REQUIRED"); properties.setProperty("modify*", "PROPAGATION_REQUIRED"); properties.setProperty("do*", "PROPAGATION_REQUIRED"); properties.setProperty("del*", "PROPAGATION_REQUIRED"); properties.setProperty("remove*", "PROPAGATION_REQUIRED"); properties.setProperty("process*", "PROPAGATION_REQUIRED"); properties.setProperty("create*", "PROPAGATION_REQUIRED"); properties.setProperty("valid*", "PROPAGATION_REQUIRED"); properties.setProperty("do*", "PROPAGATION_REQUIRED"); properties.setProperty("write*", "PROPAGATION_REQUIRED"); properties.setProperty("cancel*", "PROPAGATION_REQUIRED"); properties.setProperty("*", "readOnly"); tsi.setTransactionAttributes(properties); tsi.setTransactionManager(transactionManager); return tsi; } @Bean public Advisor txAdviceAdvisor() { AspectJExpressionPointcut pointcut = new AspectJExpressionPointcut(); pointcut.setExpression(AOP_POINTCUT_EXPRESSION); return new DefaultPointcutAdvisor(pointcut, txAdvice()); } }