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

时间:2014-11-14 15:41:46

标签: java spring http logging aop

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

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

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

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

由于

2 个答案:

答案 0 :(得分: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();
    }
}

<强>方面:

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则没有。

答案 1 :(得分:4)

找到解决方案。

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();

请注意您导入的课程。