16

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 } 
5
  • 1
    Maybe a @ControllerAdvice is what you are looking for. Commented May 12, 2014 at 11:41
  • Is it (a) not running at all or (b) not running for some methods? This would help diagnose your issue. Commented May 12, 2014 at 12:02
  • I didn't know ControllerAdvice existed but looking at the documentation it is for attaching ExceptionHandler, InitBinder, and ModelAttribute to several controllers. I was looking for something to run before every method which in my case are all RequestMapping annotated methods. At the moment the Aspect is not being called at all for any methods. I could use a Spring Interceptor but AOP seemed ideally suited to the task. Commented May 12, 2014 at 13:11
  • I had forgotten to annotate my aspect class with (at)Aspect. @kriegaex did however spot something else that stopped it from working anyway. Thanks for your help. Commented May 12, 2014 at 23:16
  • For folks on older version of Spring MVC ( like 3.1 or so ) , refer this too as I was stuck for sometime while trying to introduce profiling for form controllers in a very old code base. Commented Sep 18, 2018 at 11:09

3 Answers 3

16

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

execution(* com.example.web.controllers.ThingController.*(..)) 
Sign up to request clarification or add additional context in comments.

Comments

8

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

4 Comments

@kaqqao I disagree, @ControllerAdvice only has a specific set of possibilities. You could possibly intercept every @RequestMapping method using @ModelAttribute, but you'd only have the context of the Model and nothing else. I think you should upvote @geoand, although I haven't looked at Spring MVC interceptors, I believe ControllerAdvice is not powerful enough.
@geoand answer is indeed good and I have upvoted it just now. Still, the question didn't really say much out about the specifics, just that something should run before each method and @ControllerAdvice does that...
they both do different things. Interceptors are for cross-cutting concerns, @ControllerAdvice is for providing model enrichers and exception handlers
@ControllerAdvice is not meant for addressing cross-cutting concerns. The correct method is given in the accepted answer. Downvoting,
2

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.

1 Comment

Except, sadly it seems not possible to get controller method parameter values through the interceptor. See stackoverflow.com/questions/39039695

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.