Spring Boot AOP

I am having some problems trying to follow my advice. I tried a few different items, but nothing worked. "@EnableAspectJProxy" seems to work and discovers 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 map any method that is inside my package and annotated with @RequestMapping annotation. I also tried a very general match without any luck.

Here is an example of the method to be accessed:

@RequestMapping(value = "/analysis", method = RequestMethod.GET)
@ApiOperation(value = "Get analyses available for the current user")
JsonModelAndView getAllAnalyses(HttpServletRequest request)
+4
source share
2 answers

. spring, , . , pointcut .

@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);
        }
    }
}

, pointcutcut.

+7

, proxyTargetclass= true ( , ). @EnableASpectJAutoProxy spring.aop.proxyTargetClass=true.

+2

All Articles