gpt4 book ai didi

java - 如何使用参数化注释指定方法及其使用 @Pointcut 的值

转载 作者:行者123 更新时间:2023-12-02 12:15:25 24 4
gpt4 key购买 nike

背景:

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

我想创建一个在 POST 请求上执行而不是在 GET 请求上执行的切面,因为我想注入(inject)阻止在 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. 如何指定带有参数化注释的方法及其值 <aop:pointcut> Spring 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,这是一种负担。

关于java - 如何使用参数化注释指定方法及其使用 @Pointcut 的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46221992/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com