使用自定义注释的方法看起来不错且简单明了,但是如果您实际上不想使用它,则可以创建自定义来修改以下解释:TransactionAttributeSource
@Transactional
public class RollbackForAllAnnotationTransactionAttributeSource
extends AnnotationTransactionAttributeSource {
@Override
protected TransactionAttribute determineTransactionAttribute(
AnnotatedElement ae) {
TransactionAttribute target = super.determineTransactionAttribute(ae);
if (target == null) return null;
else return new DelegatingTransactionAttribute(target) {
@Override
public boolean rollbackOn(Throwable ex) {
return true;
}
};
}
}
而不是按如下方式手动配置它(请参阅源代码):<tx:annotation-driven/>
AnnotationDrivenBeanDefinitionParser
<bean id = "txAttributeSource"
class = "RollbackForAllAnnotationTransactionAttributeSource" />
<bean id = "txInterceptor"
class = "org.springframework.transaction.interceptor.TransactionInterceptor">
<property name = "transactionManagerBeanName" value = "transactionManager" />
<property name = "transactionAttributeSource" ref = "txAttributeSource" />
</bean>
<bean
class = "org.springframework.transaction.interceptor.BeanFactoryTransactionAttributeSourceAdvisor">
<property name="transactionAttributeSource" ref = "txAttributeSource" />
<property name = "adviceBeanName" value = "txInterceptor" />
</bean>
您还需要 或 .<aop:config/>
<aop:aspectj-autoproxy />
但是请注意,对于期望正常行为为 的其他开发人员来说,此类覆盖可能会令人困惑。@Transactional