App下載

spring全局異常攔截器如何實(shí)現(xiàn)

猿友 2021-06-22 14:07:13 瀏覽數(shù) (3222)
反饋

本篇文章介紹了spring全局異常攔截器如何實(shí)現(xiàn),本文可以實(shí)現(xiàn)在spring中手動(dòng)構(gòu)建全局異常攔截器。

你可能會(huì)問,Spring已經(jīng)自帶了全局異常攔截,為什么還要重復(fù)造輪子呢?

這是個(gè)好問題,我覺得有以下幾個(gè)原因

  1. 裝逼
  2. Spring的全局異常攔截只是針對于Spring MVC的接口,對于你的RPC接口就無能為力了
  3. 無法定制化
  4. 除了寫業(yè)務(wù)代碼,我們其實(shí)還能干點(diǎn)別的事

我覺得上述理由已經(jīng)比較充分的解答了為什么要重復(fù)造輪子,接下來就來看一下怎么造輪子

造個(gè)什么樣的輪子?

我覺得全局異常攔截應(yīng)該有如下特性

  1. 使用方便,最好和spring原生的使用方式一致,降低學(xué)習(xí)成本
  2. 能夠支持所有接口
  3. 調(diào)用異常處理器可預(yù)期,比如說定義了RuntimeException的處理器和Exception的處理器,如果這個(gè)時(shí)候拋出NullPointException,這時(shí)候要能沒有歧義的選擇預(yù)期的處理器

如何造輪子?

由于現(xiàn)在的應(yīng)用基本上都是基于spring的,因此我也是基于SpringAop來實(shí)現(xiàn)全局異常攔截

首先先定義幾個(gè)注解

  1. @Target(ElementType.TYPE)
  2. @Retention(RetentionPolicy.RUNTIME)
  3. @Documented
  4. @Component
  5. public @interface ExceptionAdvice {
  6. }
  7.  
  8. @Target(ElementType.METHOD)
  9. @Retention(RetentionPolicy.RUNTIME)
  10. @Documented
  11. public @interface ExceptionHandler {
  12. Class<? extends Throwable>[] value();
  13. }
  14.  
  15. @Target(ElementType.METHOD)
  16. @Retention(RetentionPolicy.RUNTIME)
  17. @Documented
  18. public @interface ExceptionIntercept {
  19. }

@ExceptionAdvice 的作用是標(biāo)志定義異常處理器的類,方便找到異常處理器

@ExceptionHandler 的作用是標(biāo)記某個(gè)方法是處理異常的,里面的值是能夠處理的異常類型

@ExceptionIntercept 的作用是標(biāo)記需要異常攔截的方法

接下來定義統(tǒng)一返回格式,以便出現(xiàn)錯(cuò)誤的時(shí)候統(tǒng)一返回

  1. @Data
  2. public class BaseResponse<T> {
  3. private Integer code;
  4. private String message;
  5. private T data;
  6.  
  7. public BaseResponse(Integer code, String message) {
  8. this.code = code;
  9. this.message = message;
  10. }
  11. }

然后定義一個(gè)收集異常處理器的類

  1. public class ExceptionMethodPool {
  2. private List<ExceptionMethod> methods;
  3. private Object excutor;
  4.  
  5. public ExceptionMethodPool(Object excutor) {
  6. this.methods = new ArrayList<ExceptionMethod>();
  7. this.excutor = excutor;
  8. }
  9.  
  10. public Object getExcutor() {
  11. return excutor;
  12. }
  13.  
  14. public void add(Class<? extends Throwable> clazz, Method method) {
  15. methods.add(new ExceptionMethod(clazz, method));
  16. }
  17.  
  18. //按序查找能夠處理該異常的處理器
  19. public Method obtainMethod(Throwable throwable) {
  20. return methods
  21. .stream()
  22. .filter(e -> e.getClazz().isAssignableFrom(throwable.getClass()))
  23. .findFirst()
  24. .orElseThrow(() ->new RuntimeException("沒有找到對應(yīng)的異常處理器"))
  25. .getMethod();
  26. }
  27.  
  28. @AllArgsConstructor
  29. @Getter
  30. class ExceptionMethod {
  31. private Class<? extends Throwable> clazz;
  32. private Method method;
  33. }
  34. }

ExceptionMethod 里面有兩個(gè)屬性

  • clazz:這個(gè)代表著能夠處理的異常
  • method:代表著處理異常調(diào)用的方法

ExceptionMethodPool 里面按序存放所有異常處理器,excutor是執(zhí)行這些異常處理器的對象

接下來把所有定義的異常處理器收集起來

  1. @Component
  2. public class ExceptionBeanPostProcessor implements BeanPostProcessor {
  3. private ExceptionMethodPool exceptionMethodPool;
  4. @Autowired
  5. private ConfigurableApplicationContext context;
  6.  
  7. @Override
  8. public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
  9. Class<?> clazz = bean.getClass();
  10. ExceptionAdvice advice = clazz.getAnnotation(ExceptionAdvice.class);
  11. if (advice == null) return bean;
  12. if (exceptionMethodPool != null) throw new RuntimeException("不允許有兩個(gè)異常定義類");
  13. exceptionMethodPool = new ExceptionMethodPool(bean);
  14.  
  15. //保持處理異常方法順序
  16. Arrays.stream(clazz.getDeclaredMethods())
  17. .filter(method -> method.getAnnotation(ExceptionHandler.class) != null)
  18. .forEach(method -> {
  19. ExceptionHandler exceptionHandler = method.getAnnotation(ExceptionHandler.class);
  20. Arrays.stream(exceptionHandler.value()).forEach(c -> exceptionMethodPool.add(c,method));
  21. });
  22. //注冊進(jìn)spring容器
  23. context.getBeanFactory().registerSingleton("exceptionMethodPool",exceptionMethodPool);
  24. return bean;
  25. }
  26. }

ExceptionBeanPostProcessor 通過實(shí)現(xiàn)BeanPostProcessor 接口,在bean初始化之前,把所有異常處理器塞進(jìn) ExceptionMethodPool,并把其注冊進(jìn)Spring容器

然后定義異常處理器

  1. @Component
  2. public class ExceptionProcessor {
  3. @Autowired
  4. private ExceptionMethodPool exceptionMethodPool;
  5.  
  6. public BaseResponse process(Throwable e) {
  7. return (BaseResponse) FunctionUtil.computeOrGetDefault(() ->{
  8. Method method = exceptionMethodPool.obtainMethod(e);
  9. method.setAccessible(true);
  10. return method.invoke(exceptionMethodPool.getExcutor(),e);
  11. },new BaseResponse(0,"未知錯(cuò)誤"));
  12. }
  13. }

這里應(yīng)用了我自己通過函數(shù)式編程封裝的一些語法糖,有興趣的可以看下

最后通過AOP進(jìn)行攔截

  1. @Aspect
  2. @Component
  3. public class ExceptionInterceptAop {
  4. @Autowired
  5. private ExceptionProcessor exceptionProcessor;
  6.  
  7. @Pointcut("@annotation(com.example.exception.intercept.ExceptionIntercept)")
  8. public void pointcut() {
  9. }
  10.  
  11. @Around("pointcut()")
  12. public Object around(ProceedingJoinPoint point) {
  13. return computeAndDealException(() -> point.proceed(),
  14. e -> exceptionProcessor.process(e));
  15. }
  16.  
  17. public static <R> R computeAndDealException(ThrowExceptionSupplier<R> supplier, Function<Throwable, R> dealFunc) {
  18. try {
  19. return supplier.get();
  20. } catch (Throwable e) {
  21. return dealFunc.apply(e);
  22. }
  23. }
  24. @FunctionalInterface
  25. public interface ThrowExceptionSupplier<T> {
  26. T get() throws Throwable;
  27. }
  28. }

到這里代碼部分就已經(jīng)完成了,我們來看下如何使用

  1. @ExceptionAdvice
  2. public class ExceptionConfig {
  3. @ExceptionHandler(value = NullPointerException.class)
  4. public BaseResponse process(NullPointerException e){
  5. return new BaseResponse(0,"NPE");
  6. }
  7.  
  8. @ExceptionHandler(value = Exception.class)
  9. public BaseResponse process(Exception e){
  10. return new BaseResponse(0,"Ex");
  11. }
  12.  
  13. }
  14.  
  15. @RestController
  16. public class TestControler {
  17.  
  18. @RequestMapping("/test")
  19. @ExceptionIntercept
  20. public BaseResponse test(@RequestParam("a") Integer a){
  21. if (a == 1){
  22. return new BaseResponse(1,a+"");
  23. }
  24. else if (a == 2){
  25. throw new NullPointerException();
  26. }
  27. else throw new RuntimeException();
  28. }
  29. }

我們通過@ExceptionAdvice標(biāo)志定義異常處理器的類,然后通過@ExceptionHandler標(biāo)注處理異常的方法,方便收集

最后在需要異常攔截的方法上面通過@ExceptionIntercept進(jìn)行異常攔截

我沒有使用Spring那種匹配最近父類的方式尋找匹配的異常處理器,我覺得這種設(shè)計(jì)是一個(gè)敗筆,理由如下

  • 代碼復(fù)雜
  • 不能一眼看出要去調(diào)用哪個(gè)異常處理器,尤其是定義的異常處理器非常多的時(shí)候,要是弄多個(gè)定義類就更不好找了,可能要把所有的處理器看完才知道應(yīng)該調(diào)用哪個(gè)

出于以上考慮,我只保留了一個(gè)異常處理器定義類,并且匹配順序和方法定義順序一致,從上到下依次匹配,這樣只要找到一個(gè)能夠處理的處理器,那么就知道了會(huì)如何調(diào)用


0 人點(diǎn)贊