Spring AOP切入控制器中的所有方法

Goo*_*ose 7 java spring spring-mvc spring-aop pointcut

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

调度员servlet.xml中

<aop:aspectj-autoproxy/>
<bean class="com.example.web.controllers.ThingAspect"/>
Run Code Online (Sandbox Code Playgroud)

cewcThingAspect

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

@Before("thing()")
public void doStuffBeforeThing(JoinPoint joinPoint) {
    // do stuff here
}
Run Code Online (Sandbox Code Playgroud)

Sea*_*oyd 8

在当前版本的Spring MVC中执行此操作的正确方法是通过a ControllerAdvice.
请参阅:使用@ControllerAdvice注释建议控制器

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

  • @kaqqao 我不同意,`@ControllerAdvice` 只有一组特定的可能性。您可以使用“@ModelAttribute”拦截每个“@RequestMapping”方法,但您只有“Model”的上下文而没有其他内容。我认为你应该支持@geoand,虽然我没有看过Spring MVC拦截器,但我相信`ControllerAdvice`不够强大。 (2认同)

kri*_*aex 7

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

execution(* com.example.web.controllers.ThingController.*(..))
Run Code Online (Sandbox Code Playgroud)