Search code examples
springspring-bootspring-mvcspring-aop

Is it possible to add PointCut to ModelAndView method?


I tried to use PointCut to perform some post action after ModelAndView.setViewName, but it seems that it never triggers:

@Aspect
@Component
public class TestAspect {
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Pointcut("execution(* org.springframework.web.servlet.ModelAndView.*(..))")
    public void testPointCut() {

    }

    @After("testPointCut()")
    public void afterPointCut(JoinPoint joinPoint) {
        logger.debug("afterPointCut");
    }
}

If I change the execution part to some class of my own, this point cut works.

So what is the correct way to add PointCut to ModelAndView?


Solution

  • I am not a Spring user, but what I know about Spring AOP is that you can only apply it to Spring components. The class ModelAndView is not derived from any Spring core component class or annotated by anything making it such, it is a simple POJO. As such you cannot target it by Spring AOP pointcuts. You should rather target something within the reach of Spring AOP.

    The alternative would be to unpack the big gun and use full AspectJ LTW (load-time weaving) which is not limited to Spring components.