我有以下Groovy类:
@Slf4j
class WidgetService {
WidgetDao widgetDao = new WidgetDao()
createWidget(String name, int type) {
try {
widgetDao.createWidget(name, type)
} catch(WidgetException wexc) {
log.error(wexc)
int x = doFizz()
long y = doBuzz(x)
determineHowToHandle(y)
}
}
Widget getWidgetById(Long id) {
try {
widgetDao.getWidgetById(id)
} catch(WidgetException wexc) {
log.error(wexc)
int x = doFizz()
long y = doBuzz(x)
determineHowToHandle(y)
}
}
Widget getWidgetByName(String name) {
try {
widgetDao.getWidgetByName(name)
} catch(WidgetException wexc) {
log.error(wexc)
int x = doFizz()
long y = doBuzz(x)
determineHowToHandle(y)
}
}
def deleteWidget(Widget w) {
try {
widgetDao.deleteWidget(w)
} catch(WidgetException wexc) {
log.error(wexc)
int x = doFizz()
long y = doBuzz(x)
determineHowToHandle(y)
}
}
...dozens of more methods with *exact* same catch block
}
正如您所看到的,我的try-catch块中有很多重复的样板代码。如果我可以定义一个闭包或某种基于AOP的处理程序,只需将widgetDao
感兴趣的方法作为lambda或类似的东西传递给闭包/处理程序,这将是很好的:
def createWidgetClosure = { it =>
widgetDao.createWidget(it.name, it.type)
}
def getWidgetByIdClosure = { it =>
widgetDao.getWidgetById(it.id)
}
def tryCatchClosure = { closure =>
try {
closure()
} catch(WidgetException wexc) {
log.error(wexc)
int x = doFizz()
long y = doBuzz(x)
determineHowToHandle(y)
}
}
这样我的`WidgetService就可以看起来像这样:
@Slf4j
class WidgetService {
WidgetDao widgetDao = new WidgetDao()
createWidget(String name, int type) {
tryCatchClosure(createWidgetClosure())
}
Widget getWidgetById(Long id) {
tryCatchClosure(getWidgetByIdClosure())
}
...dozens of more methods with *exact* same catch block
}
这可能吗?如果是这样,怎么样?
答案 0 :(得分:5)
您可以使用tryCatchClosure
现在拥有的内容,执行以下操作。您甚至可以使tryCatchClosure
成为一个以Closure
为参数的方法。
class WidgetService {
WidgetDao widgetDao = new WidgetDao()
def tryCatchClosure(Closure closure) {
try {
closure()
} catch(WidgetException wexc) {
log.error(wexc)
int x = doFizz()
long y = doBuzz(x)
determineHowToHandle(y)
}
}
createWidget(String name, int type) {
tryCatchClosure {
widgetDao.createWidget(name, type)
}
}
Widget getWidgetById(Long id) {
tryCatchClosure {
widgetDao.getWidgetById(id)
}
}
Widget getWidgetByName(String name) {
tryCatchClosure {
widgetDao.getWidgetByName(name)
}
}
def deleteWidget(Widget w) {
tryCatchClosure {
widgetDao.deleteWidget(w)
}
}
// ...dozens of more methods with *exact* same catch block
}
或者您也可以通过在其metaClass上覆盖WidgetDao
方法并处理异常(try / catch)来拦截invokeMethod
上的每个方法调用。 Similar to this