2017-09-14 456 views
0

背景:

我正在使用Spring MVC开发Web应用程序。如何使用参数化注释指定方法及其与@Pointcut的值

我想创建一个在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 
} 

问:

  1. 我如何与参数注释及其与@Pointcut值指定的方法?
  2. 如何在弹簧applicationContext.xml中指定带参数化注释的方法及其值<aop:pointcut>

回答

1
@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,这是一个负担。