【问题标题】:@Around advice with Controller is not working@Around 与 Controller 的建议不起作用
【发布时间】:2015-11-23 20:21:03
【问题描述】:

我正在尝试在 Spring 启动应用程序(使用 Spring 安全性)中使用 AOP 记录控制器方法,但无法成功。添加方面相关代码后,它没有返回任何异常并且处理程序方法无法检测到:

控制器方法:

@RequestMapping(value="/index", method = RequestMethod.GET)
public ModelAndView welcome() {
    ModelAndView modelView = new ModelAndView();
    modelView.setViewName("index");
    return modelView;
}

方面:

@Pointcut("within(@org.springframework.web.bind.annotation.RequestMapping *)")
    public void requestMapping() {}

    @Around("execution(* com.hms.controllers.HomeController.*(..)) && @annotation(requestMapping)")
    public void aroundControllerMethod(ProceedingJoinPoint joinPoint, RequestMapping requestMapping) {
        System.out.println("Testing aroundController: " + joinPoint.getSignature().getName());
}

以下日志失败:

2015-11-18 12:57:41.310 DEBUG 6180 --- [nio-8080-exec-1] s.w.s.m.m.a.RequestMappingHandlerMapping : Looking up handler method for path /index
2015-11-18 12:57:41.310 DEBUG 6180 --- [nio-8080-exec-1] s.w.s.m.m.a.RequestMappingHandlerMapping : Did not find handler method for [/index]

如果 Aspect 相关代码被注释,索引页面返回没有任何问题

有什么见解吗?

【问题讨论】:

    标签: java spring-security spring-boot spring-aop


    【解决方案1】:

    控制器已经被 spring mvc 代理了,可能没有安装 around 建议。几年前我遇到了同样的问题,并很快使用日志过滤器解决了它。你也可以定义一个HandlerInterceptor

    【讨论】:

    • 之前或之后的建议是否适用于控制器??
    • 老实说我不知道​​。我做了一个试验,结果不好,从那时起,我一直使用过滤器或拦截器,这些机制非常适合拦截基于 URI 映射的请求。这里有一篇关于拦截器的好帖子。 journaldev.com/2676/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-08-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多