我想在Spring(3.2.3)@Controller中的每个方法之前运行一些代码。我有以下定义,但它不会运行。我怀疑切入点表达是不正确的。
调度-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
}
答案 0 :(得分:8)
在当前版本的Spring MVC中执行此操作的正确方法是ControllerAdvice
请参阅:Advising controllers with the @ControllerAdvice
annotation
对于以前的版本,请参阅我的这个答案: https://stackoverflow.com/a/5866960/342852
答案 1 :(得分:6)
您的切入点表达式缺少void
,String
或*
等返回类型,例如
execution(* com.example.web.controllers.ThingController.*(..))
答案 2 :(得分:2)
除了在另一个答案中已经提及的@ControllerAdvice
之外,您应该查看Spring MVC interceptors。
它们基本上简化了控制器的AOP,并且可以在@ControllerAdvice
没有给你足够电力的情况下使用。