0

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?

1 Answer 1

1

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.

3
  • sorry for being so late, I thought there could be a better solution for this, but after a month-long waiting, I guess you are right about this, btw I solved my problem with an old school way: using a wrapper. Commented Jun 21, 2019 at 3:59
  • 1
    Configuring Spring to use AspectJ with LTW (load-time weaving) would have been a matter of minutes or maybe hours, depending on how complex your setting is. I thought you might want to give it a spin. But a wrapper is also nice. I guess you made the wrapper a Spring component and then apply AOP to that wrapper, right?
    – kriegaex
    Commented Jun 21, 2019 at 4:21
  • You are almost right, I'll try LTW another time, because I don't want to use a sledge hammer to crack a nut, thanks again! Commented Jun 24, 2019 at 2:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.