如何使用参数化注释指定方法及其与@Pointcut的值
问题描述:
背景:
我正在使用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
}
问:
- 我如何与参数注释及其与
@Pointcut
值指定的方法? - 如何在弹簧
applicationContext.xml
中指定带参数化注释的方法及其值<aop:pointcut>
?
答
@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.
}
您可能需要延长这一只拦截了Requestmapping在你的包。因为它拦截了您的项目中可能包含的每个RequestMappig,包括您可能正在使用的库所使用的一个RequestMappig,这是一个负担。