背景:

我正在用Spring MVC开发一个Web应用程序。

我想使一个方面在POST请求上执行,而不在GET请求上执行,因为我想注入防止在HTML呈现完成之前发送的POST请求的逻辑。

@RequestMapping(value = "/aaa", method = RequestMethod.POST)
public String methodForPost(AnDto dto, Model model) {
    // the aspect should be executed on this method
}

@RequestMapping(value = "/bbb", method = RequestMethod.GET)
public String methodForGET(AnDto dto, Model model) {
    // the aspect shouldn't be executed on this method
}


题:


如何指定带有参数化批注的方法及其@Pointcut的值?
如何在Spring <aop:pointcut>applicationContext.xml中指定带有参数化批注的方法及其值?

最佳答案

@Around(value="@annotation(RequestMapping)")
public Object display(ProceedingJoinPoint joinPoint, RequestMapping requestMapping ) throws Throwable {
    // You have access to requestMapping. From which you can get whether its get or post and decide to proceed or not.
}


更多信息http://docs.spring.io/spring/docs/current/spring-framework-reference/html/aop.html#aop-ataspectj-advice-params-passing

您可能需要扩展它以仅拦截包中的Requestmapping。因为这会拦截项目中您可能拥有的每个RequestMappig,包括您可能正在使用的库所使用的每个RequestMappig,这是一个负担。

09-09 22:25
查看更多