Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Boot AOP

I am having some issues trying to get my advice to execute. I tried several different pointcuts to no avail. The "@EnableAspectJProxy" seems to be working and detects my aspect. Any advice is appreciated.

I am using spring-boot-aop-starter.

@Aspect
@Component
public class ExecutionTimeLogger {

    private Logger logger;

    public ExecutionTimeLogger() {
        logger = LoggerFactory.getLogger(getClass());
        logger.info("HEY");
    }

    @Pointcut("within(@org.springframework.stereotype.Controller *)")
    public void controller() {}

    @Pointcut("execution(* edu.x.y.z.server.web.controller.*.*(*))")
    public void methodPointcut() {}

    @Pointcut("within(@org.springframework.web.bind.annotation.RequestMapping *)")
    public void requestMapping() {}

    @Around("controller() && methodPointcut() && requestMapping()")
    public Object profile(ProceedingJoinPoint pjp) throws Throwable {
        StopWatch sw = new StopWatch();
        String name = pjp.getSignature().getName();
        try {
            sw.start();
            return pjp.proceed();
        } finally {
            sw.stop();
            logger.info("STOPWATCH: " + sw.getTime() + " - " + name);
        }
    }
}

I am trying to match any method that is within my package and is annotated with the @RequestMapping annotation. I have tried the very generic match any and all methods without any luck too.

Here is a sample of a method that the advice should be applied to:

@RequestMapping(value = "/analysis", method = RequestMethod.GET)
@ApiOperation(value = "Get analyses available for the current user")
JsonModelAndView getAllAnalyses(HttpServletRequest request)
like image 663
user1595702 Avatar asked Dec 26 '22 03:12

user1595702


1 Answers

I managed to get this resolved. I ended up creating a small spring application to test the use case with the specific pointcuts to remove other potential barriers. I found that my pointcuts needed some adjusting.

@Aspect
@Component
public class ExecutionTimeLogger {

    private Logger logger;

    public ExecutionTimeLogger() {
        logger = LoggerFactory.getLogger(getClass());
        logger.info("HEY");
    }

    @Pointcut("@annotation(org.springframework.web.bind.annotation.RequestMapping)")
    public void requestMapping() {}

    @Pointcut("execution(* edu.x.y.z.server.web.controller.*Controller.*(..))")
    public void methodPointcut() {}

    @Around("requestMapping() && methodPointcut()")
    public Object profile(ProceedingJoinPoint pjp) throws Throwable {
        StopWatch sw = new StopWatch();
        String name = pjp.getSignature().getName();
        try {
            sw.start();
            return pjp.proceed();
        } finally {
            sw.stop();
            logger.info("STOPWATCH: " + sw.getTime() + " - " + name);
        }
    }
}

As you can see the big difference was the annotation pointcut.

like image 135
user1595702 Avatar answered Dec 28 '22 06:12

user1595702