提交 2e032889 编写于 作者: J Juergen Hoeller

added "alwaysTranslate" flag (SPR-6630)

上级 fd3a39dc
/*
* Copyright 2002-2008 the original author or authors.
* Copyright 2002-2010 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
......@@ -49,6 +49,8 @@ public class PersistenceExceptionTranslationInterceptor
private PersistenceExceptionTranslator persistenceExceptionTranslator;
private boolean alwaysTranslate = false;
/**
* Create a new PersistenceExceptionTranslationInterceptor.
......@@ -89,6 +91,22 @@ public class PersistenceExceptionTranslationInterceptor
this.persistenceExceptionTranslator = pet;
}
/**
* Specify whether to always translate the exception ("true"), or whether throw the
* raw exception when declared, i.e. when the originating method signature's exception
* declarations allow for the raw exception to be thrown ("false").
* <p>Default is "false". Switch this flag to "true" in order to always translate
* applicable exceptions, independent from the originating method signature.
* <p>Note that the originating method does not have to declare the specific exception.
* Any base class will do as well, even <code>throws Exception</code>: As long as the
* originating method does explicitly declare compatible exceptions, the raw exception
* will be rethrown. If you would like to avoid throwing raw exceptions in any case,
* switch this flag to "true".
*/
public void setAlwaysTranslate(boolean alwaysTranslate) {
this.alwaysTranslate = alwaysTranslate;
}
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
if (this.persistenceExceptionTranslator == null) {
// No explicit exception translator specified - perform autodetection.
......@@ -138,7 +156,7 @@ public class PersistenceExceptionTranslationInterceptor
}
catch (RuntimeException ex) {
// Let it throw raw if the type of the exception is on the throws clause of the method.
if (ReflectionUtils.declaresException(mi.getMethod(), ex.getClass())) {
if (!this.alwaysTranslate && ReflectionUtils.declaresException(mi.getMethod(), ex.getClass())) {
throw ex;
}
else {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册