gpt4 book ai didi

spring - @Tansactional 和 @Aspect 排序

转载 作者:行者123 更新时间:2023-12-02 08:57:04 24 4
gpt4 key购买 nike

我想在 @Transactional 事务开始之前执行我的代码。

@Aspect
@Order(Ordered.HIGHEST_PRECEDENCE)
//@Order(Ordered.LOWEST_PRECEDENCE)
public class SynchronizerAspect {

@Pointcut("execution(public * xxx.xxx.services.*.*(..))")
private void anyServiceOperation() {
}

@Around("anyServiceOperation()")
public Object synchronizerAdvice(ProceedingJoinPoint joinPoint) throws Throwable {
System.out.println("Synchronizing : " + joinPoint.getSignature().getName());
return joinPoint.proceed();
}

当我调用标记为 @Transactional 的服务方法时,我总是在事务内执行方面代码:

[INFO] INFO: FETCH created: Fri Oct 30 15:43:11 UTC 2015 duration: 0 connection: 40 statement: 999 resultset: 0
[INFO] paĹş 30, 2015 3:43:11 PM com.mysql.jdbc.log.Slf4JLogger logInfo
[INFO] INFO: QUERY created: Fri Oct 30 15:43:11 UTC 2015 duration: 1 connection: 40 statement: 14 resultset: 15 message: SELECT @@session.tx_isolation
[INFO] paĹş 30, 2015 3:43:11 PM com.mysql.jdbc.log.Slf4JLogger logInfo
[INFO] INFO: FETCH created: Fri Oct 30 15:43:11 UTC 2015 duration: 3 connection: 40 statement: 14 resultset: 15
[INFO] paĹş 30, 2015 3:43:11 PM com.mysql.jdbc.log.Slf4JLogger logInfo
[INFO] INFO: QUERY created: Fri Oct 30 15:43:11 UTC 2015 duration: 1 connection: 40 statement: 999 resultset: 0 message: SET SESSION TRANSACTION ISOLATION LEVEL READ COMMITTED
[INFO] paĹş 30, 2015 3:43:11 PM com.mysql.jdbc.log.Slf4JLogger logInfo
[INFO] INFO: FETCH created: Fri Oct 30 15:43:11 UTC 2015 duration: 0 connection: 40 statement: 999 resultset: 0
[INFO] paĹş 30, 2015 3:43:11 PM com.mysql.jdbc.log.Slf4JLogger logInfo
[INFO] INFO: QUERY created: Fri Oct 30 15:43:11 UTC 2015 duration: 0 connection: 40 statement: 15 resultset: 16 message: SELECT 1
[INFO] paĹş 30, 2015 3:43:11 PM com.mysql.jdbc.log.Slf4JLogger logInfo
[INFO] INFO: FETCH created: Fri Oct 30 15:43:11 UTC 2015 duration: 3 connection: 40 statement: 15 resultset: 16
[INFO] Synchronizing : getCompany

我还设置了@EnableTransactionManagement(order = 500)

我应该做些什么才能使其正常工作吗?

此外,我使用aspectj-maven-plugin:1.7在编译时编织方面。

这么聪明还用了@Ordering吗?

无论我如何设置@Order,aspectj-maven-plugin日志都显示我的@Aspect是在@Transactional之前添加的

[INFO] Join point 'method-execution(boolean xxx.xxx.services.LicenseServiceImpl.checkLicenseFile(int, int))' in Type 'xxx.xxx.services.LicenseServiceImpl' (LicenseServiceImpl.java:261) advised by around advice from 'xxx.xxx.aop.SynchronizerAspect' (SynchronizerAspect.java:29)
[INFO] Join point 'method-execution(boolean xxx.xxx.services.LicenseServiceImpl.checkLicenseFile(int, int))' in Type 'xxx.xxx.services.LicenseServiceImpl' (LicenseServiceImpl.java:261) advised by around advice from 'org.springframework.transaction.aspectj.AnnotationTransactionAspect' (spring-aspects-4.2.2.RELEASE.jar!AbstractTransactionAspect.class:66(from AbstractTransactionAspect.aj))

最佳答案

好的。我知道答案:

如果使用Spring基于代理的AOP,@Order注解可以正常工作。对于加载时编织,需要声明方面优先级:

import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.DeclarePrecedence;

@Aspect
@DeclarePrecedence("xxx.xxx.aop.SynchronizerAspect, org.springframework.transaction.aspectj.AnnotationTransactionAspect, *")
public class AspectPrecedence {
// empty
}

从 AutoProxy(默认)切换到 AspectJ:

@EnableAspectJAutoProxy
@EnableTransactionManagement(mode = AdviceMode.ASPECTJ)

关于spring - @Tansactional 和 @Aspect 排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33440182/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com