Shady Ragab
Shady Ragab

Reputation: 725

Spring AOP execution expression with two arguments

I want to advice the following method

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)

and extract the two arguments : pendingTrx, globalTrxId to be used in the advice method.

I use the following execution expression:

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

The application is built successfully, but the advice code is not executed.

I use Spring boot with @EnableAspectJAutoProxy(proxyTargetClass=true) on my configuration class.

By the way I have @AfterThrowing advice to run correctly. So I highly think that the problem is with my execution expression.

Update: I have very weird finding : when I use any argument of type String the advice does not work, otherwise(Long or Double) it works.

any explanation?

Upvotes: 1

Views: 2704

Answers (1)

alexbt
alexbt

Reputation: 17045

This works for me, using @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();
    }
}

Or @Around with a @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();
    }
}

I think your error is the order of your args:

  • you specified: args(pendingTrx,globalTrxId,..)
  • and it should be args(.., pendingTrx,globalTrxId)

Upvotes: 1

Related Questions