2017-04-24 11 views
0

私は別の層(ハンドラー - >サービス - > 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ブロック内のコードだけです。残りのコードはすべてのハンドラで同じです。

私は例外処理をカプセル化して、どこでも繰り返す必要はないので、将来的には例外を処理する必要があります。すべてのハンドラで変更を加える必要はありません。

私はSpringを使用しているので、Bean 'ExceptionHandler'を作成してすべてのハンドラに挿入することができますが、私が望むことを達成するためのより良い方法があるかどうかを知りたいと思います。

答えて

0

は、あなたが春AOPのを利用することができます春(私は春には、上記のシナリオのために何かをご提供しなければならないと確信しています)

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); 
     } 
    } 
} 
1

Spring provides a @ExceptionHandler annotationハンドラメソッドで特定の例外をクラスからのメソッドによって処理するには
例外ハンドラになるメソッドに注釈を付け、それぞれに処理する予想例外クラスを指定します。
例えば

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

} 

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

あなたのハンドラは春の豆であれば、あなたは例外処理のためのハンドラメソッドが含まれていますし、その後、あなたの具体的なハンドラはそれから継承することができます抽象クラスを作成することができます。


あなたのハンドラでは、春のコントローラがあると、あなたは@ControllerAdvice annotationでBeanを宣言することと一般的な方法を指定することで、まだシンプルなものを作ることができ、すべてのコントローラに対して同じように例外処理を行いたい場合例外を処理するために。
あなたはもはや共通の抽象クラスを持つ必要はありません。ここで

0

から何かを使用していない私が書いた簡単な例であります中央の場所で例外を処理する(下図)@AfterThrowingアドバイス(あなたは春のdoc例えばhereを見ることができます):

@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 
     } 
} 
関連する問題