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

And*_*ung 6 java logging aop spring http

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

@RequestMapping(value="/some/path", method=RequestMethod.POST)
Run Code Online (Sandbox Code Playgroud)

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

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

谢谢

kri*_*aex 9

@ 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();
    }
}
Run Code Online (Sandbox Code Playgroud)

方面:

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]);
    }
}
Run Code Online (Sandbox Code Playgroud)

顺便说一下&& 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
Run Code Online (Sandbox Code Playgroud)

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


And*_*ung 5

找到了解决办法。

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();
Run Code Online (Sandbox Code Playgroud)

请注意您导入的类。