每当我说@Transactional时,java都会对每个选中的异常进行回滚
由于程序员被迫捕获所有已检查的异常,因此我必须在出现任何问题时抛出已检查的异常。我想回过头来看看这些期待中的任何一个。在每个@Transactional
注释上写rollbackFor=Exception.class
非常容易出错,所以我想告诉spring:“每当我写@Transactional
,我的意思是@Transactional(rollbackFor=Exception.class)
”
我知道,我可以创建自定义注释,但这似乎不自然
那么,有没有办法告诉spring它应该如何在全球范围内处理已检查的例外情况呢
# 1 楼答案
自定义快捷方式注释
不,这正是自定义注释的用例。这里引用了Spring参考中的Custom Shortcut Annotations:
示例代码
下面是针对您的用例的示例注释:
现在用
@MyAnnotation
注释您的服务和/或方法(您会想到一个更好的名称)。这是经过良好测试的默认功能。为什么要重新发明轮子# 2 楼答案
看起来您可以根据方法名称配置事务性建议,如下所示: (来自the Spring 2.0 docs)