Results 1 to 4 of 4

Thread: Prevent aspect re-execution

  1. #1
    Join Date
    Nov 2010
    Posts
    24

    Question 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 at 08:27 AM. Reason: note added

  2. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,632

    Default

    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.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  3. #3
    Join Date
    Nov 2010
    Posts
    24

    Default

    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. #4
    Join Date
    Nov 2010
    Posts
    24

    Default

    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 at 10:16 AM.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •