Announcement Announcement Module
Collapse
No announcement yet.
@Aspect advice is never called Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • @Aspect advice is never called

    Hi

    I have a simple scenario where I want to use an @Aspect for some operation @AfterReturning.

    My code:
    Code:
    package com.foo.bar;
    
    ...
    
    @Aspect
    @Component
    public class MyAspect implements IMyAspect
    {
        @Pointcut("execution(* *.MyService.performOperation(..)) && args(op)")
        public void performOperation(MyOperation op) { }
        
        @AfterReturning("performOperation(op)") 
        public void afterReturningPerformOperation(MyOperation op) {
            ... // never reaches here
        }
    }
    My config:
    Code:
    	<context:component-scan base-package="com.foo.bar" />
    	<aop:aspectj-autoproxy />
    The MyAspect bean is created for sure (loaded with application context and used in different beans). Also I know that the @Pointcut itself is detected because the PointcutParser parses the expression successfully (it also means that the expression is reflecting at least one actual method?)

    The problem is that no matter what I do, the @AfterReturning method is never called. I don't know what's wrong, it was based on the "Spring in Action 3rd edition" book example and I don't think the modifications had any significance (mainly names).

    The "MyService.performOperation(..)" method is @Transactional, if matters.
    Last edited by YuvalRon; Apr 14th, 2011, 10:46 AM.

  • #2
    Try to change your aspect class like this and see if it works:

    Code:
    package com.foo.bar;
    
    ...
    
    @Aspect
    @Component
    public class MyAspect implements IMyAspect
    {
        @Pointcut("execution(* *.MyService.performOperation(..)) ")
        public void performOperation() { }
        
        @AfterReturning(pointcut="performOperation()", returning="retVal") 
        public void afterReturningPerformOperation(Object retVal) {
            if (retVal instanceof MyOperation) {
               MyOperation op = (MyOperation)retVal;
               //do your stuff with op
            }
        }
    }

    Comment


    • #3
      Thanks

      I tried that, but it didn't work. I'm not sure why would it, because performOperation(op) doesn't return anything, MyOperation op is the argument not the return value..

      Comment


      • #4
        Ok...my mistake, I wasn't paying enough attention. Now I've analysed your config in more detail and I can't find anything wrong.

        Some possibly obvious checks:

        - are the classes containing the methods you want advised properly registered as Spring beans? Spring AOP only works on Spring beans...

        - if yes, are the beans registered in the SAME application context where the aop configuration is defined? Parent/child context access rules don't work for AOP...

        - if yes, are the methods you want advised public, non-static, and called externally(i.e. not inside the class itself)? Because Spring AOP is proxy based, non-public, static or only called internally methods can't be advised.

        If the answer is yes for all questions, them the only possible problem remains the interaction with the tx config, so you should also post that part of your configuration...

        Comment


        • #5
          Thank you Enrico, that solved the problem!

          I wasn't aware of the fact that internal calls are not being advised, and indeed that method was public non-static but called internally. So I had to export it out to another class just to comply with AOP requirements but now it works. Thanks!

          Comment

          Working...
          X