Question

I want to run some code before every method in a Spring (3.2.3) @Controller. I have the following defined but it won't run. I suspect the pointcut expression is incorrect.

dispatcher-servlet.xml

<aop:aspectj-autoproxy/>
<bean class="com.example.web.controllers.ThingAspect"/>

c.e.w.c.ThingAspect

@Pointcut("execution(com.example.web.controllers.ThingController.*(..))")
public void thing() {
}

@Before("thing()")
public void doStuffBeforeThing(JoinPoint joinPoint) {
    // do stuff here
}
Was it helpful?

Solution

Your pointcut expression is missing a return type like void, String or *, e.g.

execution(* com.example.web.controllers.ThingController.*(..))

OTHER TIPS

The correct way to do it in current versions of Spring MVC is through a ControllerAdvice.
See: Advising controllers with the @ControllerAdvice annotation

For previous versions, refer to this answer of mine: https://stackoverflow.com/a/5866960/342852

Besides @ControllerAdvice that is already mentioned in another answer, you should check out Spring MVC interceptors.

They basically simplify AOP for controllers and can be used in cases where @ControllerAdvice doesn't give you enough power.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top