私は別の層(ハンドラー - >サービス - > 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'を作成してすべてのハンドラに挿入することができますが、私が望むことを達成するためのより良い方法があるかどうかを知りたいと思います。