1

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?

Shady Ragab
  • 705
  • 10
  • 26

1 Answers1

1

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)
alexbt
  • 16,415
  • 6
  • 78
  • 87
  • thanks a million...do you have any document for the expression language of the method arguments in AOP, because I have still similar problems. – Shady Ragab Oct 24 '16 at 12:58
  • I used this as reference: http://docs.spring.io/spring/docs/current/spring-framework-reference/html/aop.html – alexbt Oct 24 '16 at 13:00
  • In my case not adding the arguments to the definition of the pointcut results in the following exception:error at ::0 formal unbound in pointcut at org.aspectj.weaver.tools.PointcutParser.parsePointcutExpression(PointcutParser.java:319) ~[aspectjweaver-1.9.4.jar:na] – carlos palma Sep 28 '20 at 18:01