2017-09-14 98 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,這是一個負擔。