Allow DAO ExceptionTranslator to proxy classes

Update the auto-configured PersistenceExceptionTranslationPostProcessor
to proxy target classes as well as interfaces. Also provide a config
property to disable registration all together.

Fixes gh-1844
This commit is contained in:
Phillip Webb 2014-11-06 19:13:50 -08:00
parent a1a643ca24
commit 58d660d10d
4 changed files with 29 additions and 1 deletions

View File

@ -19,6 +19,7 @@ package org.springframework.boot.autoconfigure.dao;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor;
@ -34,8 +35,11 @@ public class PersistenceExceptionTranslationAutoConfiguration {
@Bean
@ConditionalOnMissingBean(PersistenceExceptionTranslationPostProcessor.class)
@ConditionalOnProperty(prefix = "spring.dao.exceptiontranslation", name = "enabled", matchIfMissing = true)
public PersistenceExceptionTranslationPostProcessor persistenceExceptionTranslationPostProcessor() {
return new PersistenceExceptionTranslationPostProcessor();
PersistenceExceptionTranslationPostProcessor postProcessor = new PersistenceExceptionTranslationPostProcessor();
postProcessor.setProxyTargetClass(true);
return postProcessor;
}
}

View File

@ -17,6 +17,12 @@
"description": "Execute all Spring Batch jobs in the context on startup.",
"defaultValue": true
},
{
"name": "spring.dao.exceptiontranslation.enabled",
"dataType": "java.lang.Boolean",
"description": "Enables the PersistenceExceptionTranslationPostProcessor.",
"defaultValue": true
},
{
"name": "spring.datasource.jmx-enabled",
"dataType": "java.lang.Boolean",

View File

@ -25,6 +25,7 @@ import org.junit.After;
import org.junit.Test;
import org.springframework.boot.autoconfigure.jdbc.EmbeddedDataSourceConfiguration;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;
import org.springframework.boot.test.EnvironmentTestUtils;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@ -32,6 +33,7 @@ import org.springframework.dao.InvalidDataAccessApiUsageException;
import org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor;
import org.springframework.stereotype.Repository;
import static org.hamcrest.Matchers.empty;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;
@ -60,6 +62,19 @@ public class PersistenceExceptionTranslationAutoConfigurationTests {
Map<String, PersistenceExceptionTranslationPostProcessor> beans = this.context
.getBeansOfType(PersistenceExceptionTranslationPostProcessor.class);
assertThat(beans.size(), is(equalTo(1)));
assertThat(beans.values().iterator().next().isProxyTargetClass(), equalTo(true));
}
@Test
public void exceptionTranslationPostProcessorBeanIsDisabled() {
this.context = new AnnotationConfigApplicationContext();
EnvironmentTestUtils.addEnvironment(this.context,
"spring.dao.exceptiontranslation.enabled=false");
this.context.register(PersistenceExceptionTranslationAutoConfiguration.class);
this.context.refresh();
Map<String, PersistenceExceptionTranslationPostProcessor> beans = this.context
.getBeansOfType(PersistenceExceptionTranslationPostProcessor.class);
assertThat(beans.entrySet(), empty());
}
@Test(expected = IllegalArgumentException.class)

View File

@ -205,6 +205,9 @@ content into your application; rather pick only the properties that you need.
spring.datasource.max-wait=
spring.datasource.jmx-enabled=true # Export JMX MBeans (if supported)
# DATASOURCE ({sc-spring-boot-autoconfigure}/dao/PersistenceExceptionTranslationAutoConfiguration.{sc-ext}[PersistenceExceptionTranslationAutoConfiguration]
spring.dao.exceptiontranslation.enabled=true
# MONGODB ({sc-spring-boot-autoconfigure}/mongo/MongoProperties.{sc-ext}[MongoProperties])
spring.data.mongodb.host= # the db host
spring.data.mongodb.port=27017 # the connection port (defaults to 27107)