解決Springboot全局異常處理與AOP日志處理中@AfterThrowing失效問題
一、前言
- 在實際業(yè)務(wù)場景中,我們通常會使用全局異常處理機制,也就是在業(yè)務(wù)代碼發(fā)生異常的時候,攔截異常并進行統(tǒng)一的處理,然后以Json格式返回給前端。
- 同時我們也會使用AOP進行操作日志記錄,在不發(fā)生異常時,可以使用四種advice方式記錄操作日志:@Before(“”),@After(“”)、 @AfterReturning(“”)、 @Around(“”)。當發(fā)生異常時,使用@AfterThrowing(value = “”,throwing = “e”)進行日志記錄。
二、問題
同時使用上述兩種方式,可能出現(xiàn)某一種失效的場景。
三、失效場景
失效場景一: 如果采用前后端不分離架構(gòu),采用下屬代碼返回前端響應結(jié)果,如果統(tǒng)一異常處理執(zhí)行順序在@AfterThrowing之前,就會出現(xiàn)@AfterThrowing中不執(zhí)行情況。前后端分離架構(gòu)不會出現(xiàn)此問題。
String xRequestedWith = request.getHeader("x-requested-with"); if ("XMLHttpRequest".equals(xRequestedWith)) { response.setContentType("application/plain;charset=utf-8"); PrintWriter writer = response.getWriter(); writer.write(CommunityUtil.getJSONString(1, "服務(wù)器異常!")); } else { response.sendRedirect(request.getContextPath() + "/error"); }
解決方案:讓AOP日志處理類實現(xiàn)Ordered 接口,并重寫getOrder()方法,使其返回值為1,返回值越小,執(zhí)行的順序越靠前,使其執(zhí)行順序優(yōu)先于全部異常處理類。
@Component @Aspect public class LogAspectTest implements Ordered { @Override public int getOrder() { return 1; } @AfterThrowing(value = "pointcut()",throwing = "e") public void afterThrowing(JoinPoint joinPoint,Exception e){ String className = joinPoint.getTarget().getClass().getName(); String methodName = joinPoint.getSignature().getName(); System.out.println("className is : " + className + ". methodName is : " + methodName); System.out.println("afterThrowing excute········" + e.getMessage()); e.printStackTrace(); } }
失效場景二:如果使用了 @Around(“”),在執(zhí)行 joinPoint.proceed()方法時,捕獲了異常,會導致全局異常處理無法收到異常,因此失效。
@Around("pointcut()") public void around(ProceedingJoinPoint joinPoint) throws Throwable { String className = joinPoint.getTarget().getClass().getName(); String methodName = joinPoint.getSignature().getName(); System.out.println("className is : " + className + ". methodName is : " + methodName); System.out.println("around excute before ········"); Object obj = null; try { obj = joinPoint.proceed(); } catch (Throwable e) { System.out.println("我捕獲了異常"); } System.out.println("obj 對象: " + obj); System.out.println("around excute after ········"); }
解決方案:不要進行異常捕獲,或者捕獲后重新拋出異常。
@Around("pointcut()") public void around(ProceedingJoinPoint joinPoint) throws Throwable { String className = joinPoint.getTarget().getClass().getName(); String methodName = joinPoint.getSignature().getName(); System.out.println("className is : " + className + ". methodName is : " + methodName); System.out.println("around excute before ········"); Object obj = null; try { obj = joinPoint.proceed(); } catch (Throwable e) { System.out.println("我捕獲了異常"); throw new RuntimeException("執(zhí)行失敗",e); } System.out.println("obj 對象: " + obj); System.out.println("around excute after ········"); }
4、測試全部代碼
package com.nowcoder.community.aspect; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.*; import org.springframework.core.Ordered; import org.springframework.stereotype.Component; import org.springframework.web.context.request.RequestAttributes; import org.springframework.web.context.request.RequestContextHolder; import org.springframework.web.context.request.ServletRequestAttributes; /** * @Description aop 日志記錄 */ @Component @Aspect public class LogAspectTest implements Ordered { /** * 定義執(zhí)行順序的優(yōu)先級,值越小,優(yōu)先級越高 * @return */ @Override public int getOrder() { return 1; } /** * 定義切點(織入點) * execution(* com.nowcoder.community.controller.*.*(..)) * - 第一個 * 表示 支持任意類型返回值的方法 * - com.nowcoder.community.controller 表示這個包下的類 * - 第二個 * 表示 controller包下的任意類 * - 第三個 * 表示 類中的任意方法 * - (..) 表示方法可以擁有任意參數(shù) * 可以根據(jù)自己的需求替換。 * */ @Pointcut("execution(* com.nowcoder.community.controller.*.*(..))") public void pointcut(){ } /** * 定義 advice 通知 * - 1. @Before 目標方法執(zhí)行之前 * - 2. @After 目標方法執(zhí)行之后 * - 3. @AfterReturning 目標方法返回執(zhí)行結(jié)果之后 * - 4. @AfterThrowing 目標方法拋出異常后 * - 5. @Around 可以使用ProceedingJoinPoint joinPoint,獲取目標對象,通過動態(tài)代理,代理目標類執(zhí)行,在目標對象執(zhí)行前后均可 */ @Before("pointcut()") public void before(JoinPoint joinPoint){ String className = joinPoint.getTarget().getClass().getName(); String methodName = joinPoint.getSignature().getName(); ServletRequestAttributes attributes = (ServletRequestAttributes)RequestContextHolder.getRequestAttributes(); if (attributes == null){ return; } // 請求ip String ip = attributes.getRequest().getRemoteHost(); // 請求路徑 String requestURI = attributes.getRequest().getRequestURI(); // 請求方法 String requestMethod = attributes.getRequest().getMethod(); System.out.println(String.format("用戶: [%s] 的請求路徑為:[%s], 請求方式為: [%s],請求類名為: [%s], 請求方法名為: [%s]", ip,requestURI, requestMethod,className,methodName)); System.out.println("before excute········"); } @After("pointcut()") public void after(JoinPoint joinPoint){ String className = joinPoint.getTarget().getClass().getName(); String methodName = joinPoint.getSignature().getName(); ServletRequestAttributes attributes = (ServletRequestAttributes)RequestContextHolder.getRequestAttributes(); if (attributes == null){ return; } // 請求ip String ip = attributes.getRequest().getRemoteHost(); // 請求路徑 String requestURI = attributes.getRequest().getRequestURI(); // 請求方法 String requestMethod = attributes.getRequest().getMethod(); System.out.println(String.format("用戶: [%s] 的請求路徑為:[%s], 請求方式為: [%s],請求類名為: [%s], 請求方法名為: [%s]", ip,requestURI, requestMethod,className,methodName)); System.out.println("after excute········"); } @AfterReturning("pointcut()") public void afterReturning(JoinPoint joinPoint){ String className = joinPoint.getTarget().getClass().getName(); String methodName = joinPoint.getSignature().getName(); ServletRequestAttributes attributes = (ServletRequestAttributes)RequestContextHolder.getRequestAttributes(); if (attributes == null){ return; } // 請求ip String ip = attributes.getRequest().getRemoteHost(); // 請求路徑 String requestURI = attributes.getRequest().getRequestURI(); // 請求方法 String requestMethod = attributes.getRequest().getMethod(); System.out.println(String.format("用戶: [%s] 的請求路徑為:[%s], 請求方式為: [%s],請求類名為: [%s], 請求方法名為: [%s]", ip,requestURI, requestMethod,className,methodName)); System.out.println("afterReturning excute········"); } @AfterThrowing(value = "pointcut()",throwing = "e") public void afterThrowing(JoinPoint joinPoint,Exception e){ String className = joinPoint.getTarget().getClass().getName(); String methodName = joinPoint.getSignature().getName(); ServletRequestAttributes attributes = (ServletRequestAttributes)RequestContextHolder.getRequestAttributes(); if (attributes == null){ return; } // 請求ip String ip = attributes.getRequest().getRemoteHost(); // 請求路徑 String requestURI = attributes.getRequest().getRequestURI(); // 請求方法 String requestMethod = attributes.getRequest().getMethod(); System.out.println(String.format("用戶: [%s] 的請求路徑為:[%s], 請求方式為: [%s],請求類名為: [%s], 請求方法名為: [%s],請求失敗原因: [%s]", ip, requestURI, requestMethod,className,methodName,e.getMessage()+e.getCause())); System.out.println("afterThrowing excute········" + e.getMessage()); e.printStackTrace(); } @Around("pointcut()") public void around(ProceedingJoinPoint joinPoint) throws Throwable { String className = joinPoint.getTarget().getClass().getName(); String methodName = joinPoint.getSignature().getName(); System.out.println("className is : " + className + ". methodName is : " + methodName); ServletRequestAttributes attributes = (ServletRequestAttributes)RequestContextHolder.getRequestAttributes(); if (attributes == null){ return; } // 請求ip String ip = attributes.getRequest().getRemoteHost(); // 請求路徑 String requestURI = attributes.getRequest().getRequestURI(); // 請求方法 String requestMethod = attributes.getRequest().getMethod(); System.out.println(String.format("用戶: [%s] 的請求路徑為:[%s], 請求方式為: [%s],請求類名為: [%s], 請求方法名為: [%s]", ip,requestURI, requestMethod,className,methodName)); Object obj = null; try { obj = joinPoint.proceed(); } catch (Throwable e) { System.out.println("我捕獲了異常"); throw new RuntimeException("執(zhí)行失敗",e); } System.out.println(String.format("用戶: [%s] 的請求路徑為:[%s], 請求方式為: [%s],請求類名為: [%s], 請求方法名為: [%s]", ip,requestURI, requestMethod,className,methodName)); System.out.println("around excute after ········"); } }
到此這篇關(guān)于解決Springboot全局異常處理與AOP日志處理中@AfterThrowing失效問題的文章就介紹到這了,更多相關(guān)Springboot @AfterThrowing失效內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot2整合Drools規(guī)則引擎及案例詳解
這篇文章主要介紹了SpringBoot2整合Drools規(guī)則引擎及案例詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2019-10-10Idea開發(fā)工具之SpringBoot整合JSP的過程
最近在學習SpringBoot,看到SpringBoot整合jsp,順帶記錄一下。本文通過圖文實例相結(jié)合給大家講解SpringBoot整合JSP的過程,感興趣的朋友一起看看吧2021-09-09Spring-Validation 后端數(shù)據(jù)校驗的實現(xiàn)
這篇文章主要介紹了Spring-Validation 后端數(shù)據(jù)校驗的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-07-07關(guān)于weblogic部署Java項目的包沖突問題的解決
這篇文章主要介紹了關(guān)于weblogic部署Java項目的包沖突問題的解決,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-01-01Java filter中的chain.doFilter使用詳解
這篇文章主要介紹了Java filter中的chain.doFilter使用詳解,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-11-11