如何封装处理程序的异常



我正在处理具有不同层的应用程序(处理程序 -> services-> dao(。

EmployeeHandler {
    Employee get(){...}
    Employee save(){...}
    Employee update(){...}
    etc(){...}
}
CompanyHandler {
    Company get(){...}
    Company save(){...}
    Company update(){...}
    etc(){...}
}
ManagerHandler {
   Manager get(){...}
   Manager save(){...}
   Manager update(){...}
   etc(){...}
}

处理程序是捕获异常的操作员。它们具有相同的方法,但是通过不同的实现,我执行一些验证和更多必需的任务。

Manager save(){
  try{
    // do something
    employeeService.save(employee);
   } 
   catch (MyCustomException e) {
     // handle exception -- here I do the same for each method in all handlers
   } 
   catch (Exception e) {
     // catch any exception -- here I also do the same thing for all handlers
    }
}

基本上,唯一更改的代码是" try"块内部的代码,其余的每个处理程序都是相同的。

我想封装异常处理,所以我不必在任何地方重复,如果我必须处理任何例外,我就不必在所有处理程序中进行更改。

我的想法是自从我使用春季以来,我可以创建一个bean'exceptionhandler',因此可以在所有处理程序中注入,但是我想知道是否有更好的方法可以实现我想要的东西。<<<<<<

Spring提供了一个@ExceptionHandler注释,可以在处理程序方法中处理特定的异常,从类中的方法提升。
只需注释将是例外处理程序的方法,并指定每个在每个类别中要处理的例外类别。

例如:

@ExceptionHandler(MyCustomException.class)
public void handleException(MyCustomException e) {
     ... // exception handling
}
@ExceptionHandler(Exception.class)
public void handleException(Exception e) {
     ... // exception handling        
}

如果您的处理程序是弹簧豆,则可以创建一个抽象类,该类将包含用于例外处理的处理程序方法,然后您可以使您的混凝土处理程序从其继承。


如果您的处理程序是弹簧控制器,并且您想以相同的方式对所有控制器进行异常处理,则可以通过用@ControllerAdvice注释来声明BEAN并指定处理通用方法来处理exception in。
您不再需要一个共同的抽象类。

这是我写的一个快速示例,它不使用春季的任何内容(我敢肯定,春天必须为您提供上述方案的东西(

interface ExceptionHandler<T extends Throwable> {
    void handle(T exception);
}
class UnsupportedOperationExceptionHandler implements ExceptionHandler<UnsupportedOperationException> {
    @Override
    public void handle(UnsupportedOperationException exception) {
        //blaa
    }
}
class IllegalArguementExceptionHandler implements ExceptionHandler<IllegalArgumentException> {
    @Override
    public void handle(IllegalArgumentException exception) {
        //blaa part 2
    }
}
class YourHandler {
    private final Map<Class<? extends Throwable>, ExceptionHandler> exceptionHandlers; //This will contain all your handlers
    @Inject
    YourHandler(Map<Class<? extends Throwable>, ExceptionHandler> exceptionHandlers) {
        this.exceptionHandlers = exceptionHandlers;
    }
    public void save() {
        try {
            //some method here.
        } catch (Exception e) {
            exceptionHandlers.get(e.getClass()).handle(e);
        }
    }
}

您可以使用Spring AOP的@AfterThrowing建议(如下所示(在中心位置处理异常(您可以在此处查找Spring doc示例(:

@Aspect
public class MyProjectExceptionHandler {
    @AfterThrowing (pointcut = "execution(* com.myproject.service.impl..*(..))", 
                              throwing = "ex")
    public void handleMyCustomException(MyCustomException ex) 
                       throws Throwable  { 
         //add your code here
        }
    @AfterThrowing (pointcut = "execution(* com.myproject.service.impl..*(..))", 
                   throwing = "ex")
    public void handleException(Exception ex) 
       throws Throwable  {
       //add your code here
      }
}

最新更新