Top Description Fields Constructors Methods
org.springframework.dao.support

public Class PersistenceExceptionTranslationInterceptor

extends Object
implements MethodInterceptor, BeanFactoryAware, InitializingBean
Class Inheritance
All Implemented Interfaces
org.springframework.beans.factory.InitializingBean, org.springframework.beans.factory.BeanFactoryAware, org.springframework.beans.factory.Aware, org.aopalliance.intercept.MethodInterceptor, org.aopalliance.intercept.Interceptor, org.aopalliance.aop.Advice
Imports
org.aopalliance.intercept.MethodInterceptor, .MethodInvocation, org.springframework.beans.BeansException, org.springframework.beans.factory.BeanFactory, .BeanFactoryAware, .InitializingBean, .ListableBeanFactory, org.springframework.lang.Nullable, org.springframework.util.Assert, .ReflectionUtils

AOP Alliance MethodInterceptor that provides persistence exception translation based on a given PersistenceExceptionTranslator.

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).

Authors
Rod Johnson, Juergen Hoeller
Since
2.0
See Also
PersistenceExceptionTranslator

Field Summary

Modifier and TypeField and Description
private boolean
private ListableBeanFactory
private volatile PersistenceExceptionTranslator

Constructor Summary

AccessConstructor and Description
public
PersistenceExceptionTranslationInterceptor()

Create a new PersistenceExceptionTranslationInterceptor.

public
PersistenceExceptionTranslationInterceptor(PersistenceExceptionTranslator
the PersistenceExceptionTranslator to use
pet
)

Create a new PersistenceExceptionTranslationInterceptor for the given PersistenceExceptionTranslator.

public
PersistenceExceptionTranslationInterceptor(ListableBeanFactory
the ListableBeanFactory to obtaining all PersistenceExceptionTranslators from
beanFactory
)

Create a new PersistenceExceptionTranslationInterceptor, autodetecting PersistenceExceptionTranslators in the given BeanFactory.

Method Summary

Modifier and TypeMethod and Description
public void
afterPropertiesSet()

Implements org.springframework.beans.factory.InitializingBean.afterPropertiesSet.

Invoked by the containing 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.

protected PersistenceExceptionTranslator

Returns:

a chained PersistenceExceptionTranslator, combining all PersistenceExceptionTranslators found in the given bean factory
detectPersistenceExceptionTranslators
(ListableBeanFactory
the ListableBeanFactory to obtain PersistenceExceptionTranslators from
bf
)

Detect all PersistenceExceptionTranslators in the given BeanFactory.

public Object
invoke(MethodInvocation
the method invocation joinpoint
mi
)

Implements org.aopalliance.intercept.MethodInterceptor.invoke.

Implement this method to perform extra treatments before and after the invocation.
public void
setAlwaysTranslate(boolean alwaysTranslate)

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").

public void
setBeanFactory(BeanFactory
owning BeanFactory (never null). The bean can immediately call methods on the factory.
beanFactory
)

Implements org.springframework.beans.factory.BeanFactoryAware.setBeanFactory.

Callback that supplies the owning factory to a bean instance.
public void
setPersistenceExceptionTranslator(PersistenceExceptionTranslator pet)

Specify the PersistenceExceptionTranslator to use.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

alwaysTranslateback to summary
private boolean alwaysTranslate
beanFactoryback to summary
private ListableBeanFactory beanFactory
Annotations
@Nullable
persistenceExceptionTranslatorback to summary
private volatile PersistenceExceptionTranslator persistenceExceptionTranslator
Annotations
@Nullable

Constructor Detail

PersistenceExceptionTranslationInterceptorback to summary
public PersistenceExceptionTranslationInterceptor()

Create a new PersistenceExceptionTranslationInterceptor. Needs to be configured with a PersistenceExceptionTranslator afterwards.

See Also
setPersistenceExceptionTranslator
PersistenceExceptionTranslationInterceptorback to summary
public PersistenceExceptionTranslationInterceptor(PersistenceExceptionTranslator pet)

Create a new PersistenceExceptionTranslationInterceptor for the given PersistenceExceptionTranslator.

Parameters
pet:PersistenceExceptionTranslator

the PersistenceExceptionTranslator to use

PersistenceExceptionTranslationInterceptorback to summary
public PersistenceExceptionTranslationInterceptor(ListableBeanFactory beanFactory)

Create a new PersistenceExceptionTranslationInterceptor, autodetecting PersistenceExceptionTranslators in the given BeanFactory.

Parameters
beanFactory:ListableBeanFactory

the ListableBeanFactory to obtaining all PersistenceExceptionTranslators from

Method Detail

afterPropertiesSetback to summary
public void afterPropertiesSet()

Implements org.springframework.beans.factory.InitializingBean.afterPropertiesSet.

Doc from org.springframework.beans.factory.InitializingBean.afterPropertiesSet.

Invoked by the containing 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.

Annotations
@Override
detectPersistenceExceptionTranslatorsback to summary
protected PersistenceExceptionTranslator detectPersistenceExceptionTranslators(ListableBeanFactory bf)

Detect all PersistenceExceptionTranslators in the given BeanFactory.

Parameters
bf:ListableBeanFactory

the ListableBeanFactory to obtain PersistenceExceptionTranslators from

Returns:PersistenceExceptionTranslator

a chained PersistenceExceptionTranslator, combining all PersistenceExceptionTranslators found in the given bean factory

See Also
ChainedPersistenceExceptionTranslator
invokeback to summary
public Object invoke(MethodInvocation mi) throws Throwable

Implements org.aopalliance.intercept.MethodInterceptor.invoke.

Doc from org.aopalliance.intercept.MethodInterceptor.invoke.

Implement this method to perform extra treatments before and after the invocation. Polite implementations would certainly like to invoke Joinpoint#proceed().

Parameters
mi:MethodInvocation

the method invocation joinpoint

Returns:Object

the result of the call to Joinpoint#proceed(); might be intercepted by the interceptor

Annotations
@Override
@Nullable
Exceptions
Throwable:
if the interceptors or the target object throws an exception
setAlwaysTranslateback to summary
public void setAlwaysTranslate(boolean alwaysTranslate)

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").

Default is "false". Switch this flag to "true" in order to always translate applicable exceptions, independent of 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".

setBeanFactoryback to summary
public void setBeanFactory(BeanFactory beanFactory) throws BeansException

Implements org.springframework.beans.factory.BeanFactoryAware.setBeanFactory.

Doc from org.springframework.beans.factory.BeanFactoryAware.setBeanFactory.

Callback that supplies the owning factory to a bean instance.

Invoked after the population of normal bean properties but before an initialization callback such as InitializingBean#afterPropertiesSet() or a custom init-method.

Parameters
beanFactory:BeanFactory

owning BeanFactory (never null). The bean can immediately call methods on the factory.

Annotations
@Override
Exceptions
BeansException:
in case of initialization errors
setPersistenceExceptionTranslatorback to summary
public void setPersistenceExceptionTranslator(PersistenceExceptionTranslator pet)

Specify the PersistenceExceptionTranslator to use.

Default is to autodetect all PersistenceExceptionTranslators in the containing BeanFactory, using them in a chain.

See Also
detectPersistenceExceptionTranslators