public class PersistenceExceptionTranslationInterceptor extends Object implements MethodInterceptor, BeanFactoryAware, InitializingBean
Delegates to the given PersistenceExceptionTranslator
to translate
a RuntimeException thrown into Spring's DataAccessException hierarchy
(if appropriate). If the RuntimeException in question is declared on the
target method, it is always propagated as-is (with no translation applied).
PersistenceExceptionTranslator
Constructor and Description |
---|
PersistenceExceptionTranslationInterceptor()
Create a new PersistenceExceptionTranslationInterceptor.
|
PersistenceExceptionTranslationInterceptor(ListableBeanFactory beanFactory)
Create a new PersistenceExceptionTranslationInterceptor, autodetecting
PersistenceExceptionTranslators in the given BeanFactory.
|
PersistenceExceptionTranslationInterceptor(PersistenceExceptionTranslator pet)
Create a new PersistenceExceptionTranslationInterceptor
for the given PersistenceExceptionTranslator.
|
Modifier and Type | Method and Description |
---|---|
void |
afterPropertiesSet()
Invoked by the containing
BeanFactory after it has set all bean properties
and satisfied BeanFactoryAware , ApplicationContextAware etc. |
protected PersistenceExceptionTranslator |
detectPersistenceExceptionTranslators(ListableBeanFactory bf)
Detect all PersistenceExceptionTranslators in the given BeanFactory.
|
Object |
invoke(MethodInvocation mi)
Implement this method to perform extra treatments before and
after the invocation.
|
void |
setAlwaysTranslate(boolean alwaysTranslate)
Specify whether to always translate the exception ("true"), or whether throw the
raw exception when declared, i.e.
|
void |
setBeanFactory(BeanFactory beanFactory)
Callback that supplies the owning factory to a bean instance.
|
void |
setPersistenceExceptionTranslator(PersistenceExceptionTranslator pet)
Specify the PersistenceExceptionTranslator to use.
|
public PersistenceExceptionTranslationInterceptor()
public PersistenceExceptionTranslationInterceptor(PersistenceExceptionTranslator pet)
pet
- the PersistenceExceptionTranslator to usepublic PersistenceExceptionTranslationInterceptor(ListableBeanFactory beanFactory)
beanFactory
- the ListableBeanFactory to obtaining all
PersistenceExceptionTranslators frompublic void setPersistenceExceptionTranslator(PersistenceExceptionTranslator pet)
Default is to autodetect all PersistenceExceptionTranslators in the containing BeanFactory, using them in a chain.
public void setAlwaysTranslate(boolean alwaysTranslate)
Default is "false". Switch this flag to "true" in order to always translate applicable exceptions, independent from the originating method signature.
Note that the originating method does not have to declare the specific exception.
Any base class will do as well, even throws Exception
: 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 setBeanFactory(BeanFactory beanFactory) throws BeansException
BeanFactoryAware
Invoked after the population of normal bean properties
but before an initialization callback such as
InitializingBean.afterPropertiesSet()
or a custom init-method.
setBeanFactory
in interface BeanFactoryAware
beanFactory
- owning BeanFactory (never null
).
The bean can immediately call methods on the factory.BeansException
- in case of initialization errorsBeanInitializationException
public void afterPropertiesSet()
InitializingBean
BeanFactory
after it has set all bean properties
and satisfied BeanFactoryAware
, ApplicationContextAware
etc.
This method allows the bean instance to perform validation of its overall configuration and final initialization when all bean properties have been set.
afterPropertiesSet
in interface InitializingBean
@Nullable public Object invoke(MethodInvocation mi) throws Throwable
MethodInterceptor
Joinpoint.proceed()
.invoke
in interface MethodInterceptor
mi
- the method invocation joinpointJoinpoint.proceed()
;
might be intercepted by the interceptorThrowable
- if the interceptors or the target object
throws an exceptionprotected PersistenceExceptionTranslator detectPersistenceExceptionTranslators(ListableBeanFactory bf)
bf
- the ListableBeanFactory to obtain PersistenceExceptionTranslators fromChainedPersistenceExceptionTranslator