我想在Spring(3.2.3)@Controller的每個方法之前運行一些代碼。我有以下定義,但它不會運行。我懷疑切入點表達式是不正確的。控制器中所有方法的Spring AOP切入點
調度-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
}
也許['@ ControllerAdvice'](http://docs.spring.io/spring/docs/4.0.3.RELEASE/javadoc-api/org/springframework/web/bind/annotation/ControllerAdvice .html)是你正在尋找的。 –
它是(a)完全不運行還是(b)某些方法沒有運行?這將有助於診斷您的問題。 – Phil
我不知道ControllerAdvice是否存在,但是查看了將ExceptionHandler,InitBinder和ModelAttribute附加到多個控制器的文檔。我正在尋找一些東西來運行每個方法,在我的情況下都是RequestMapping註釋方法。目前該方面根本沒有被任何方法調用。我可以使用Spring Interceptor,但AOP看起來非常適合這項任務。 – Goose