发布于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黑洞网
任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任
昵称:
评论内容:(最多支持255个字符)
---无人问津也好,技不如人也罢,你都要试着安静下来,去做自己该做的事,而不是让内心的烦躁、焦虑,坏掉你本来就不多的热情和定力
Copyright © 2018-2021 java黑洞网 All Rights Reserved 版权所有,并保留所有权利。京ICP备18063182号-2
投诉与举报,广告合作请联系vgs_info@163.com或QQ3083709327
免责声明:网站文章均由用户上传,仅供读者学习交流使用,禁止用做商业用途。若文章涉及色情,反动,侵权等违法信息,请向我们举报,一经核实我们会立即删除!