简体   繁体   中英

Spring and AOP : @After works but not @AfterReturning

In a webapp, I use Spring AOP to check authorisations for my services on incoming calls and to manage messages (info, warn, error) on returning results. Using an aspect to do that saves me lines of code and generalizes the behaviour of my services (and it looks sexy ^^).

So I have this type of conf in my app context

    <aop:aspectj-autoproxy />
    <bean id="authenticationCheckAspect" class="fr.test.server.business.aspect.AuthenticationCheckAspect" />

And my aspect looks like that :

package fr.test.server.business.aspect;

@Aspect
public class AuthenticationCheckAspect {

    private static final Logger LOG = LoggerFactory.getLogger(AuthenticationCheckAspect.class);

    @Autowired
    private AuthenticationBiz authBiz;

    /**
     * methodAnnotatedWithMyService Pointcut
     */
    @Pointcut("execution(@fr.test.server.business.aspect.MyService * *(..))")
    public void methodAnnotatedWithMyService() {
        // Méthode vide servant de Pointcut
    }

    @Before("methodAnnotatedWithMyService()")
    public void checkAuthentication(final JoinPoint joinPoint) throws FunctionalException {
        LOG.debug("checkAuthentication {}", joinPoint);

        {process...}
    }

    @AfterReturning(pointcut = "methodAnnotatedWithMyService()", returning = "result")
    public void manageErrors(final JoinPoint joinPoint, final Object result) {
        LOG.debug("Returning {}", joinPoint);
    }
}

Before executing any method tagged @MyService , the method checkAuthentication() is supposed to be triggered and it is :) That's a relief.

After executing any method tagged @MyService , the method manageErrors is supposed to be triggered as well but it doesn't :( Note that with @After , it works BUT I absolutely need the return value of my @MyService annotated method and that's why I need @AfterReturning .

As my @Before advice works (and @After too when I tried it), I imagine I don't have a problem of proxied class or something like that, nothing would happen otherwise but I really dont understand why my @AfterReturning advice is not called.

NOTE : I don't get any error while executing a call. It's just that nothing is done on my @AfterReturning advice :(

Any idea ? Thx !

Your code looks good. I will suggest to add

@AfterThrowing(pointcut = "methodAnnotatedWithMyService()",  throwing="ex")
  public void doRecoveryActions( Exception e) {
    // Some code may be System.out.println 
    // or e.printStackTrace()
  }

and see if this is being executed.

if there is a exception thrown within pointcut methodAnnotatedWithMyService() then @AfterReturning will not be called.. but a @After will be called..

from http://static.springsource.org/spring/docs/2.0.x/reference/aop.html

@AfterReturning advice runs when a matched method execution returns normally

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM