springboot中怎么利用自定义注解控制事务回滚
springboot中怎么利用自定义注解控制事务回滚,针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。
springboot 自定义注解通过切面控制方法主动抛异常达到事务回滚的目的
写一个自定义注解
import java.lang.annotation.ElementType; import java.lang.annotation.Target; @Target({ElementType.TYPE, ElementType.METHOD, ElementType.FIELD}) public @interface ThrowException { public String value() default ""; }
写一个切面控制,⚠️要用环绕通知
@Aspect @Component @Slf4j public class ThrowExectionAspect implements Ordered { //service层切点 @Pointcut("@within(com.hasl.isp.policyprovider.annotation.ThrowException)") public void excetionAspect() {} @Around("excetionAspect()") public Object around(ProceedingJoinPoint pjp) throws Throwable { Object result = pjp.proceed(pjp.getArgs()); if(result != null && result instanceof ReturnMsg) { ReturnMsg returnMsg = (ReturnMsg)result; if (returnMsg.getMsgList().size() > 0){ throw new BaseException("业务异常"); } } return result; } @Override public int getOrder() { return Integer.MAX_VALUE; } }
关于springboot中怎么利用自定义注解控制事务回滚问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注行业资讯频道了解更多相关知识。