【问题标题】:Spring AOP execution expression with two arguments带有两个参数的 Spring AOP 执行表达式
【发布时间】:2016-10-24 00:53:47
【问题描述】:

我想建议以下方法

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)

并提取两个参数:pendingTrxglobalTrxId 用于通知方法。

我使用下面的执行表达式:

@Around("execution(* com.masary.ledger.service.components.LedgerTransactionComponent.createLedgerTransaction(..)) && args(pendingTrx,globalTrxId,..)")
    public Object doBasicProfilingLedgerCreate(final ProceedingJoinPoint pjp , String pendingTrx, String globalTrxId) throws Throwable 

应用构建成功,但是advice代码没有执行。

我在我的配置类中使用带有@EnableAspectJAutoProxy(proxyTargetClass=true) 的Spring boot。

顺便说一句,我有 @AfterThrowing 建议正确运行。所以我高度认为问题出在我的执行表达式上。

更新: 我有一个很奇怪的发现:当我使用任何 String 类型的参数时,建议不起作用,否则(Long 或 Double)它会起作用。

有什么解释吗?

【问题讨论】:

    标签: spring-boot aop aspectj spring-aop


    【解决方案1】:

    这对我有用,使用 @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(pendingTrx,globalTrxId,..)
    • 应该是args(.., pendingTrx,globalTrxId)

    【讨论】:

    • 谢谢一百万...你有关于AOP中方法参数的表达语言的文档吗,因为我还有类似的问题。
    • 在我的情况下,不将参数添加到切入点的定义会导致以下异常:::0 的错误在 org.aspectj.weaver.tools.PointcutParser.parsePointcutExpression(PointcutParser) 的切入点中正式未绑定.java:319) ~[aspectjweaver-1.9.4.jar:na]
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多