繁体   English   中英

如何从Spring控制器获取AOP建议中的RequestMapping请求?

[英]How do you get RequestMapping request in AOP advice from a Spring controller?

给定某种带有请求映射的控制器

@RequestMapping(value="/some/path", method=RequestMethod.POST)

您将如何检索方面类中的方法值(RequestMethod.POST)?

我想跟踪执行POST请求的所有控制器方法。

谢谢

@ AL13N:您自己的答案是正确的,但如果只是将注释绑定到参数,则不需要使用反射。 这是POJO + AspectJ中的一个例子。 在Spring AOP中它应该是相同的,但是:

主方法的样本控制器:

package de.scrum_master.app;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class MyController {
    @RequestMapping(value="/some/path", method=RequestMethod.POST)
    public void foo() {
        System.out.println("foo");
    }
    public void bar() {
        System.out.println("bar");
    }

    public static void main(String[] args) {
        MyController controller = new MyController();
        controller.foo();
        controller.bar();
    }
}

方面:

package de.scrum_master.aspect;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.web.bind.annotation.RequestMapping;

public aspect RequestMappingInterceptor {
    @Pointcut(
        "within(@org.springframework.stereotype.Controller *) && " +
        "@annotation(requestMapping) && " +
        "execution(* *(..))"
    )
    public void controller(RequestMapping requestMapping) {}

    @Before("controller(requestMapping)")
    public void advice(JoinPoint thisJoinPoint, RequestMapping requestMapping) {
        System.out.println(thisJoinPoint);
        System.out.println("  " + requestMapping);
        System.out.println("  " + requestMapping.method()[0]);
    }
}

顺便说一句,切入点的&& execution(* *(..))部分在Spring AOP中可能不是必需的,因为它只知道执行切入点。 在AspectJ中,您需要排除call()和其他类型的切入点,因为AspectJ更强大。 它虽然没有伤害,但更安全,更明确。

控制台输出:

execution(void de.scrum_master.app.MyController.foo())
  @org.springframework.web.bind.annotation.RequestMapping(headers=[], name=, value=[/some/path], produces=[], method=[POST], params=[], consumes=[])
  POST
foo
bar

编辑:交换参数,以使连接点成为第一个通知方法参数,因为Spring AOP似乎坚持这个顺序而AspectJ没有。

找到了解决方案。

import org.aspectj.lang.reflect.MethodSignature;
import java.lang.reflect.Method;

@Pointcut("within(@org.springframework.stereotype.Controller *)")
public void controller() {}

// In advice
MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
Method method = methodSignature .getMethod();
RequestMethod[] requestMethods = method.getAnnotation(RequestMapping.class).method();

请注意您导入的类。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM