程序员最近都爱上了这个网站  程序员们快来瞅瞅吧!  it98k网:it98k.com

本站消息

站长简介/公众号

  出租广告位,需要合作请联系站长


+关注
已关注

分类  

暂无分类

标签  

暂无标签

日期归档  

暂无数据

AspectJ - 关于控制器请求方法不起作用的建议

发布于2021-10-16 09:38     阅读(755)     评论(0)     点赞(9)     收藏(4)


尝试对控制器包中的所有请求方法(所有 GET 和 POST)调用 around 建议。该建议不适用于请求方法。下面是我的控制器和方面建议方法。

此外,我需要打印请求映射参数,如方法类型(Get 或 Post)和请求的 URL。

控制器类:

package net.prc.sales.web.controller;

// imports

@SessionAttributes({Dictionary.FORM_DRIVER_INFO})
@Controller
public class CustomerInfoController {
    @RequestMapping(value = Dictionary.URL_QUOTE_CUSTOMER_INFO, method = RequestMethod.GET)
    public ModelAndView viewEsCustInfo(Model model, HttpSession session) throws SessionExpiredException {
        ModelAndView mav = new ModelAndView();
        // ...
    }
    // ...
}

方面建议:

@Around("net.prc.sales.web.controller.*.*(..) && " + "@annotation(RequestMapping)")
public void ourAroundAdvice(ProceedingJoinPoint method) {
    System.out.println("Before-Advice Part:This is called before the method exceution.\n");
    try {
        method.proceed();
        System.out.println("After-Returning-Advice Part: This is called after the method returns nomally.\n");
    } catch (Throwable e) {
        System.out.println("After-Throwing-Advice Part: This is called after the method throws exception.\n");
    }
}

解决方案


@AleksiYrttiaho 是正确的,因为如果你想匹配你应该使用的子包net.prc.sales.web.controller..*而不是net.prc.sales.web.controller.*.*. 但这不是您的问题,因为示例代码显示该类CustomerInfoController在该包中是正确的。

此外,你需要

  • 在切入点的方法签名中指定返回类型,
  • 为注解指定一个完全限定的类名和
  • 确保您的建议的返回类型与拦截的方法的类型相匹配。如果拦截的方法返回其他内容(在您的示例中ModelAndView),您的建议不能返回 void

我还建议重新扔掉捕获的东西Throwable,而不仅仅是吞下它。

尝试这样的事情:

@Around("execution(* net.prc.sales.web.controller..*(..)) && @annotation(net.prc.foo.bar.RequestMapping)")
public Object ourAroundAdvice(ProceedingJoinPoint thisJoinPoint) throws Throwable {
    System.out.println("Before " + thisJoinPoint);
    Object result = null;
    try {
        result = thisJoinPoint.proceed();
        System.out.println("After returning " + thisJoinPoint);
        return result;
    } catch (Throwable t) {
        System.out.println("After throwing " + thisJoinPoint);
        throw t;
    }
}

我的建议是在做复杂的事情之前先学习 AspectJ 基础知识。您可以使用原生语法而不是注释样式,因为 Eclipse 通过语法突出显示和错误消息为您提供了很好的反馈。然后,如果您准备好切入点,您仍然可以稍后将其转换为 @AspectJ 语法。


更新:至于将注释绑定到建议参数以便能够访问其属性,这也可以完成,我刚刚阅读了您问题的那部分。首先让建议完全执行,然后在这里继续(代码未经测试):

@Around("execution(* net.prc.sales.web.controller..*(..)) && @annotation(requestMapping)")
public Object ourAroundAdvice(ProceedingJoinPoint thisJoinPoint, RequestMapping requestMapping) throws Throwable {
    System.out.println("Before " + thisJoinPoint);
    System.out.println("  Request method = " + requestMapping.method());
    System.out.println("  Request value  = " + requestMapping.value());

    Object result = null;
    try {
        result = thisJoinPoint.proceed();
        System.out.println("After returning " + thisJoinPoint);
        return result;
    } catch (Throwable t) {
        System.out.println("After throwing " + thisJoinPoint);
        throw t;
    }
}


所属网站分类: 技术文章 > 问答

作者:黑洞官方问答小能手

链接:http://www.javaheidong.com/blog/article/303955/a6a5e96e81dc21fc9918/

来源:java黑洞网

任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任

9 0
收藏该文
已收藏

评论内容:(最多支持255个字符)