解決Springboot全局異常處理與AOP日志處理中@AfterThrowing失效問(wèn)題
一、前言
- 在實(shí)際業(yè)務(wù)場(chǎng)景中,我們通常會(huì)使用全局異常處理機(jī)制,也就是在業(yè)務(wù)代碼發(fā)生異常的時(shí)候,攔截異常并進(jìn)行統(tǒng)一的處理,然后以Json格式返回給前端。
- 同時(shí)我們也會(huì)使用AOP進(jìn)行操作日志記錄,在不發(fā)生異常時(shí),可以使用四種advice方式記錄操作日志:@Before(“”),@After(“”)、 @AfterReturning(“”)、 @Around(“”)。當(dāng)發(fā)生異常時(shí),使用@AfterThrowing(value = “”,throwing = “e”)進(jìn)行日志記錄。
二、問(wèn)題
同時(shí)使用上述兩種方式,可能出現(xiàn)某一種失效的場(chǎng)景。
三、失效場(chǎng)景
失效場(chǎng)景一: 如果采用前后端不分離架構(gòu),采用下屬代碼返回前端響應(yīng)結(jié)果,如果統(tǒng)一異常處理執(zhí)行順序在@AfterThrowing之前,就會(huì)出現(xiàn)@AfterThrowing中不執(zhí)行情況。前后端分離架構(gòu)不會(huì)出現(xiàn)此問(wè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日志處理類(lèi)實(shí)現(xiàn)Ordered 接口,并重寫(xiě)getOrder()方法,使其返回值為1,返回值越小,執(zhí)行的順序越靠前,使其執(zhí)行順序優(yōu)先于全部異常處理類(lèi)。
@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(); } }
失效場(chǎng)景二:如果使用了 @Around(“”),在執(zhí)行 joinPoint.proceed()方法時(shí),捕獲了異常,會(huì)導(dǎo)致全局異常處理無(wú)法收到異常,因此失效。
@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 對(duì)象: " + obj); System.out.println("around excute after ········"); }
解決方案:不要進(jìn)行異常捕獲,或者捕獲后重新拋出異常。
@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 對(duì)象: " + obj); System.out.println("around excute after ········"); }
4、測(cè)試全部代碼
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)先級(jí),值越小,優(yōu)先級(jí)越高 * @return */ @Override public int getOrder() { return 1; } /** * 定義切點(diǎn)(織入點(diǎn)) * execution(* com.nowcoder.community.controller.*.*(..)) * - 第一個(gè) * 表示 支持任意類(lèi)型返回值的方法 * - com.nowcoder.community.controller 表示這個(gè)包下的類(lèi) * - 第二個(gè) * 表示 controller包下的任意類(lèi) * - 第三個(gè) * 表示 類(lèi)中的任意方法 * - (..) 表示方法可以擁有任意參數(shù) * 可以根據(jù)自己的需求替換。 * */ @Pointcut("execution(* com.nowcoder.community.controller.*.*(..))") public void pointcut(){ } /** * 定義 advice 通知 * - 1. @Before 目標(biāo)方法執(zhí)行之前 * - 2. @After 目標(biāo)方法執(zhí)行之后 * - 3. @AfterReturning 目標(biāo)方法返回執(zhí)行結(jié)果之后 * - 4. @AfterThrowing 目標(biāo)方法拋出異常后 * - 5. @Around 可以使用ProceedingJoinPoint joinPoint,獲取目標(biāo)對(duì)象,通過(guò)動(dòng)態(tài)代理,代理目標(biāo)類(lèi)執(zhí)行,在目標(biāo)對(duì)象執(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; } // 請(qǐng)求ip String ip = attributes.getRequest().getRemoteHost(); // 請(qǐng)求路徑 String requestURI = attributes.getRequest().getRequestURI(); // 請(qǐng)求方法 String requestMethod = attributes.getRequest().getMethod(); System.out.println(String.format("用戶(hù): [%s] 的請(qǐng)求路徑為:[%s], 請(qǐng)求方式為: [%s],請(qǐng)求類(lèi)名為: [%s], 請(qǐng)求方法名為: [%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; } // 請(qǐng)求ip String ip = attributes.getRequest().getRemoteHost(); // 請(qǐng)求路徑 String requestURI = attributes.getRequest().getRequestURI(); // 請(qǐng)求方法 String requestMethod = attributes.getRequest().getMethod(); System.out.println(String.format("用戶(hù): [%s] 的請(qǐng)求路徑為:[%s], 請(qǐng)求方式為: [%s],請(qǐng)求類(lèi)名為: [%s], 請(qǐng)求方法名為: [%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; } // 請(qǐng)求ip String ip = attributes.getRequest().getRemoteHost(); // 請(qǐng)求路徑 String requestURI = attributes.getRequest().getRequestURI(); // 請(qǐng)求方法 String requestMethod = attributes.getRequest().getMethod(); System.out.println(String.format("用戶(hù): [%s] 的請(qǐng)求路徑為:[%s], 請(qǐng)求方式為: [%s],請(qǐng)求類(lèi)名為: [%s], 請(qǐng)求方法名為: [%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; } // 請(qǐng)求ip String ip = attributes.getRequest().getRemoteHost(); // 請(qǐng)求路徑 String requestURI = attributes.getRequest().getRequestURI(); // 請(qǐng)求方法 String requestMethod = attributes.getRequest().getMethod(); System.out.println(String.format("用戶(hù): [%s] 的請(qǐng)求路徑為:[%s], 請(qǐng)求方式為: [%s],請(qǐng)求類(lèi)名為: [%s], 請(qǐng)求方法名為: [%s],請(qǐng)求失敗原因: [%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; } // 請(qǐng)求ip String ip = attributes.getRequest().getRemoteHost(); // 請(qǐng)求路徑 String requestURI = attributes.getRequest().getRequestURI(); // 請(qǐng)求方法 String requestMethod = attributes.getRequest().getMethod(); System.out.println(String.format("用戶(hù): [%s] 的請(qǐng)求路徑為:[%s], 請(qǐng)求方式為: [%s],請(qǐng)求類(lèi)名為: [%s], 請(qǐng)求方法名為: [%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("用戶(hù): [%s] 的請(qǐng)求路徑為:[%s], 請(qǐng)求方式為: [%s],請(qǐng)求類(lèi)名為: [%s], 請(qǐng)求方法名為: [%s]", ip,requestURI, requestMethod,className,methodName)); System.out.println("around excute after ········"); } }
到此這篇關(guān)于解決Springboot全局異常處理與AOP日志處理中@AfterThrowing失效問(wèn)題的文章就介紹到這了,更多相關(guān)Springboot @AfterThrowing失效內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot2整合Drools規(guī)則引擎及案例詳解
這篇文章主要介紹了SpringBoot2整合Drools規(guī)則引擎及案例詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-10-10解決JD-GUI for mac big sur打不開(kāi)問(wèn)題
這篇文章主要介紹了解決JD-GUI for mac big sur打不開(kāi)問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-01-01Idea開(kāi)發(fā)工具之SpringBoot整合JSP的過(guò)程
最近在學(xué)習(xí)SpringBoot,看到SpringBoot整合jsp,順帶記錄一下。本文通過(guò)圖文實(shí)例相結(jié)合給大家講解SpringBoot整合JSP的過(guò)程,感興趣的朋友一起看看吧2021-09-09Spring-Validation 后端數(shù)據(jù)校驗(yàn)的實(shí)現(xiàn)
這篇文章主要介紹了Spring-Validation 后端數(shù)據(jù)校驗(yàn)的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07關(guān)于weblogic部署Java項(xiàng)目的包沖突問(wèn)題的解決
這篇文章主要介紹了關(guān)于weblogic部署Java項(xiàng)目的包沖突問(wèn)題的解決,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-01-01手把手教你使用Java實(shí)現(xiàn)在線(xiàn)生成pdf文檔
在實(shí)際的業(yè)務(wù)開(kāi)發(fā)的時(shí)候,常常會(huì)需要把相關(guān)的數(shù)據(jù)信息,通過(guò)一些技術(shù)手段生成對(duì)應(yīng)的PDF文件,然后返回給用戶(hù)。本文將手把手教大家如何利用Java實(shí)現(xiàn)在線(xiàn)生成pdf文檔,需要的可以參考一下2022-03-03Java filter中的chain.doFilter使用詳解
這篇文章主要介紹了Java filter中的chain.doFilter使用詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-11-11