|
|
|
@ -1,5 +1,5 @@ |
|
|
|
/* |
|
|
|
/* |
|
|
|
* Copyright 2002-2010 the original author or authors. |
|
|
|
* Copyright 2002-2011 the original author or authors. |
|
|
|
* |
|
|
|
* |
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
|
|
|
* you may not use this file except in compliance with the License. |
|
|
|
* you may not use this file except in compliance with the License. |
|
|
|
@ -35,25 +35,28 @@ import org.springframework.util.Assert; |
|
|
|
import org.springframework.util.ClassUtils; |
|
|
|
import org.springframework.util.ClassUtils; |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Bean post-processor that automatically applies persistence exception |
|
|
|
* Bean post-processor that automatically applies persistence exception translation to any |
|
|
|
* translation to any bean that carries the |
|
|
|
* bean marked with Spring's @{@link org.springframework.stereotype.Repository Repository} |
|
|
|
* {@link org.springframework.stereotype.Repository} annotation, |
|
|
|
* annotation, adding a corresponding {@link PersistenceExceptionTranslationAdvisor} to |
|
|
|
* adding a corresponding {@link PersistenceExceptionTranslationAdvisor} |
|
|
|
* the exposed proxy (either an existing AOP proxy or a newly generated proxy that |
|
|
|
* to the exposed proxy (either an existing AOP proxy or a newly generated |
|
|
|
* implements all of the target's interfaces). |
|
|
|
* proxy that implements all of the target's interfaces). |
|
|
|
|
|
|
|
* |
|
|
|
* |
|
|
|
* <p>Translates native resource exceptions to Spring's |
|
|
|
* <p>Translates native resource exceptions to Spring's |
|
|
|
* {@link org.springframework.dao.DataAccessException} hierarchy. |
|
|
|
* {@link org.springframework.dao.DataAccessException DataAccessException} hierarchy. |
|
|
|
* Autodetects beans that implement the |
|
|
|
* Autodetects beans that implement the |
|
|
|
* {@link org.springframework.dao.support.PersistenceExceptionTranslator} |
|
|
|
* {@link org.springframework.dao.support.PersistenceExceptionTranslator |
|
|
|
* interface, which are subsequently asked to translate candidate exceptions. |
|
|
|
* PersistenceExceptionTranslator} interface, which are subsequently asked to translate |
|
|
|
|
|
|
|
* candidate exceptions. |
|
|
|
* |
|
|
|
* |
|
|
|
* <p>All of Spring's applicable resource factories implement the |
|
|
|
|
|
|
|
* <code>PersistenceExceptionTranslator</code> interface out of the box. |
|
|
|
* <p>All of Spring's applicable resource factories (e.g. {@link |
|
|
|
* As a consequence, all that is usually needed to enable automatic exception |
|
|
|
* org.springframework.orm.hibernate3.LocalSessionFactoryBean LocalSessionFactoryBean}, |
|
|
|
* translation is marking all affected beans (such as DAOs) with the |
|
|
|
* {@link org.springframework.orm.jpa.LocalEntityManagerFactoryBean |
|
|
|
* <code>Repository</code> annotation, along with defining this post-processor |
|
|
|
* LocalEntityManagerFactoryBean}) implement the {@code PersistenceExceptionTranslator} |
|
|
|
* as bean in the application context. |
|
|
|
* interface out of the box. As a consequence, all that is usually needed to enable |
|
|
|
|
|
|
|
* automatic exception translation is marking all affected beans (such as Repositories or |
|
|
|
|
|
|
|
* DAOs) with the {@code @Repository} annotation, along with defining this post-processor |
|
|
|
|
|
|
|
* as a bean in the application context. |
|
|
|
* |
|
|
|
* |
|
|
|
* @author Rod Johnson |
|
|
|
* @author Rod Johnson |
|
|
|
* @author Juergen Hoeller |
|
|
|
* @author Juergen Hoeller |
|
|
|
@ -82,7 +85,7 @@ public class PersistenceExceptionTranslationPostProcessor extends ProxyConfig |
|
|
|
* @param repositoryAnnotationType the desired annotation type |
|
|
|
* @param repositoryAnnotationType the desired annotation type |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public void setRepositoryAnnotationType(Class<? extends Annotation> repositoryAnnotationType) { |
|
|
|
public void setRepositoryAnnotationType(Class<? extends Annotation> repositoryAnnotationType) { |
|
|
|
Assert.notNull(repositoryAnnotationType, "'requiredAnnotationType' must not be null"); |
|
|
|
Assert.notNull(repositoryAnnotationType, "'repositoryAnnotationType' must not be null"); |
|
|
|
this.repositoryAnnotationType = repositoryAnnotationType; |
|
|
|
this.repositoryAnnotationType = repositoryAnnotationType; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@ -115,7 +118,7 @@ public class PersistenceExceptionTranslationPostProcessor extends ProxyConfig |
|
|
|
// Ignore AOP infrastructure such as scoped proxies.
|
|
|
|
// Ignore AOP infrastructure such as scoped proxies.
|
|
|
|
return bean; |
|
|
|
return bean; |
|
|
|
} |
|
|
|
} |
|
|
|
Class targetClass = AopUtils.getTargetClass(bean); |
|
|
|
Class<?> targetClass = AopUtils.getTargetClass(bean); |
|
|
|
if (AopUtils.canApply(this.persistenceExceptionTranslationAdvisor, targetClass)) { |
|
|
|
if (AopUtils.canApply(this.persistenceExceptionTranslationAdvisor, targetClass)) { |
|
|
|
if (bean instanceof Advised) { |
|
|
|
if (bean instanceof Advised) { |
|
|
|
((Advised) bean).addAdvisor(this.persistenceExceptionTranslationAdvisor); |
|
|
|
((Advised) bean).addAdvisor(this.persistenceExceptionTranslationAdvisor); |
|
|
|
|