12

我想在 Spring (3.2.3) @Controller 中的每个方法之前运行一些代码。我定义了以下内容,但它不会运行。我怀疑切入点表达式不正确。

调度程序-servlet.xml

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

cewc事物方面

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

@Before("thing()")
public void doStuffBeforeThing(JoinPoint joinPoint) {
    // do stuff here
}
4

3 回答 3

13

您的切入点表达式缺少返回类型,例如void,String*, 例如

execution(* com.example.web.controllers.ThingController.*(..))
于 2014-05-12T16:32:58.637 回答
9

在当前版本的 Spring MVC 中正确的方法是通过ControllerAdvice.
请参阅:使用注释向控制器提供建议@ControllerAdvice

对于以前的版本,请参考我的这个答案: https ://stackoverflow.com/a/5866960/342852

于 2014-05-12T11:54:00.517 回答
2

除了@ControllerAdvice在另一个答案中已经提到的之外,您应该查看Spring MVC interceptors

它们基本上简化了控制器的 AOP,并且可以在@ControllerAdvice无法提供足够功能的情况下使用。

于 2014-05-12T11:59:44.620 回答