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

  • Prevent aspect re-execution

    Hello,

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

    Code:
    @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?

    Thanks!

    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, 08: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.

    Comment


    • #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.

      Comment


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

        I used this snippet as an example:
        Code:
        @Around("execution(List<Account> find*(..)) &&" +
                "com.xyz.myapp.SystemArchitecture.inDataAccessLayer() && " +
                "args(accountHolderNamePattern)")		
        public Object preProcessQueryPattern(ProceedingJoinPoint pjp, String accountHolderNamePattern)
        ...
        http://static.springsource.org/sprin...-advice-params

        But my equivalent advice does not work:
        Code:
        @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.

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

        Comment

        Working...
        X