Announcement Announcement Module
No announcement yet.
Prevent aspect re-execution Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Prevent aspect re-execution


    I use Spring AOP to add logging and some exception handling for my service beans. It works perfectly:

    @Around("execution(* project.service..*.*(..))")
    public Object doLoggingAndSomeMore(ProceedingJoinPoint pjp) {
    However, some of my service beans obviously call other service beans, which makes this pointcut to get executed multiple times, for each service method call. I can work around it with a simple 'if' inside 'doLoggingAndSomeMore', but it is an unecessary overhead of aspect execution that I wish to avoid. After all, I just need the logging to happen on the outermost service method, the one called by my web controllers.

    So far, no search shed any light on this. Is there a way to accomplish it?


    Note: it may be irrelevant, but I need the 'Around' advice, because sometimes I need to replace exceptions, as this is the only advice that allows me to catch it and throw something else instead.
    Last edited by mdrg; Apr 5th, 2012, 09:27 AM. Reason: note added

  • #2
    Make your pointcut more restrictive... Add a within clause (containing your web package) which should limit it to the executions of your methods from within the web package. I strongly suggest a read of the reference guide which explains a bit more about the pointcut expressions.


    • #3
      I'll follow your 'within' suggestion. I'm mostly new to AOP and the topic is dense, but given a lead it is much simpler. I'll post a follow-up as soon as I try it.

      Thanks Marten.


      • #4
        I'm not having success with it, and I'm not sure why.

        I used this snippet as an example:
        @Around("execution(List<Account> find*(..)) &&" +
                " && " +
        public Object preProcessQueryPattern(ProceedingJoinPoint pjp, String accountHolderNamePattern)

        But my equivalent advice does not work:
        @Around("execution(* project.service..*.*(..)) && within(project.web..*)")
        I enabled Spring logging, but it does not do any useful logging about aspects during method call. Only my unrestrictive 'execution' works if used alone. My web controllers are also managed by Spring.
        I'll try some more, but if you have any good tip on this, I appreciate.

        Last edited by mdrg; Apr 5th, 2012, 11:16 AM.