Search code examples
javaspringspring-aop

Is it posible to create a log.info pointcut?


I never tried AOP before and want to know if I could make a pointcut that captures the execution of a log.info(...) in Spring.

import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;

@Aspect
public class AfterFinallyExample  {

    @After("execution(* org.slf4j.*.*(..))")
    public void sendLogParameterToServer() {
        // ...
    }
}

If it is posible, is it consider a good practice?


Solution

  • Could you use AOP to do this, yes you could. Can you do it with, the default, Spring AOP no you cannot.

    Spring AOP, by default, is based on proxies and it will only create proxies for Spring-managed beans. As the loggers aren't managed by Spring (unless you inject all loggers) this won't work.

    You will need to resort to fullblown AspectJ with either compile or loadtime weaving. Which will change the actual bytecode of the classes based on the pointcut and aspect.