Spring 带两个参数的 AOP 执行表达式
Spring AOP execution expression with two arguments
我想咨询以下方法
public BaseRepresentationObject createLedgerTransaction(Long fromUserId, Long toUserId, Double serviceAmount,
Double masaryCommission, Double merchantCommission, Double appliedFees, Double tax, Long ratePlanId,
Long serviceId, String pendingTrx, String globalTrxId)
并提取两个参数:pendingTrx
、globalTrxId
用于建议方法。
我使用以下执行表达式:
@Around("execution(* com.masary.ledger.service.components.LedgerTransactionComponent.createLedgerTransaction(..)) && args(pendingTrx,globalTrxId,..)")
public Object doBasicProfilingLedgerCreate(final ProceedingJoinPoint pjp , String pendingTrx, String globalTrxId) throws Throwable
应用构建成功,但未执行建议代码。
我在配置 class 上使用 Spring 引导和 @EnableAspectJAutoProxy(proxyTargetClass=true)
。
顺便说一下,我 @AfterThrowing
对 运行 的建议是正确的。所以我高度认为问题出在我的执行表达式上。
更新:
我有一个非常奇怪的发现:当我使用 String 类型的任何参数时,建议不起作用,否则(Long 或 Double)它起作用。
有什么解释吗?
这对我有用,使用 @Around
:
@Aspect
@Component
public class MyAspect {
@Around("execution (* com.masary.ledger.service.components.LedgerTransactionComponent.createLedgerTransaction(..)) && args(.., pendingTrx, globalTrxId)")
public Object aroundCreateLedgerTransaction(ProceedingJoinPoint pjp, String pendingTrx, String globalTrxId) throws Throwable{
System.out.println("it works!");
return pjp.proceed();
}
}
或 @Around
与 @Pointcut
:
@Aspect
@Component
public class MyAspect {
@Pointcut("execution (* com.masary.ledger.service.components.LedgerTransactionComponent.createLedgerTransaction(..))")
public void pointcutCreateLedgerTransaction(){}
@Around("pointcutCreateLedgerTransaction() && args(.., pendingTrx, globalTrxId)")
public Object aroundCreateLedgerTransaction(ProceedingJoinPoint pjp, String pendingTrx, String globalTrxId) throws Throwable{
System.out.println("it works!");
return pjp.proceed();
}
}
我认为你的错误是你的args的顺序:
- 您指定:
args(pendingTrx,globalTrxId,..)
- 应该是
args(.., pendingTrx,globalTrxId)
我想咨询以下方法
public BaseRepresentationObject createLedgerTransaction(Long fromUserId, Long toUserId, Double serviceAmount,
Double masaryCommission, Double merchantCommission, Double appliedFees, Double tax, Long ratePlanId,
Long serviceId, String pendingTrx, String globalTrxId)
并提取两个参数:pendingTrx
、globalTrxId
用于建议方法。
我使用以下执行表达式:
@Around("execution(* com.masary.ledger.service.components.LedgerTransactionComponent.createLedgerTransaction(..)) && args(pendingTrx,globalTrxId,..)")
public Object doBasicProfilingLedgerCreate(final ProceedingJoinPoint pjp , String pendingTrx, String globalTrxId) throws Throwable
应用构建成功,但未执行建议代码。
我在配置 class 上使用 Spring 引导和 @EnableAspectJAutoProxy(proxyTargetClass=true)
。
顺便说一下,我 @AfterThrowing
对 运行 的建议是正确的。所以我高度认为问题出在我的执行表达式上。
更新: 我有一个非常奇怪的发现:当我使用 String 类型的任何参数时,建议不起作用,否则(Long 或 Double)它起作用。
有什么解释吗?
这对我有用,使用 @Around
:
@Aspect
@Component
public class MyAspect {
@Around("execution (* com.masary.ledger.service.components.LedgerTransactionComponent.createLedgerTransaction(..)) && args(.., pendingTrx, globalTrxId)")
public Object aroundCreateLedgerTransaction(ProceedingJoinPoint pjp, String pendingTrx, String globalTrxId) throws Throwable{
System.out.println("it works!");
return pjp.proceed();
}
}
或 @Around
与 @Pointcut
:
@Aspect
@Component
public class MyAspect {
@Pointcut("execution (* com.masary.ledger.service.components.LedgerTransactionComponent.createLedgerTransaction(..))")
public void pointcutCreateLedgerTransaction(){}
@Around("pointcutCreateLedgerTransaction() && args(.., pendingTrx, globalTrxId)")
public Object aroundCreateLedgerTransaction(ProceedingJoinPoint pjp, String pendingTrx, String globalTrxId) throws Throwable{
System.out.println("it works!");
return pjp.proceed();
}
}
我认为你的错误是你的args的顺序:
- 您指定:
args(pendingTrx,globalTrxId,..)
- 应该是
args(.., pendingTrx,globalTrxId)