diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/dao/PersistenceExceptionTranslationAutoConfiguration.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/dao/PersistenceExceptionTranslationAutoConfiguration.java index a1e97908d7d..d858d060696 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/dao/PersistenceExceptionTranslationAutoConfiguration.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/dao/PersistenceExceptionTranslationAutoConfiguration.java @@ -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; } } diff --git a/spring-boot-autoconfigure/src/main/resources/META-INF/additional-spring-configuration-metadata.json b/spring-boot-autoconfigure/src/main/resources/META-INF/additional-spring-configuration-metadata.json index 4ef041becea..23127ac9535 100644 --- a/spring-boot-autoconfigure/src/main/resources/META-INF/additional-spring-configuration-metadata.json +++ b/spring-boot-autoconfigure/src/main/resources/META-INF/additional-spring-configuration-metadata.json @@ -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", diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/dao/PersistenceExceptionTranslationAutoConfigurationTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/dao/PersistenceExceptionTranslationAutoConfigurationTests.java index 6017323f3c6..23150542bf7 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/dao/PersistenceExceptionTranslationAutoConfigurationTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/dao/PersistenceExceptionTranslationAutoConfigurationTests.java @@ -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 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 beans = this.context + .getBeansOfType(PersistenceExceptionTranslationPostProcessor.class); + assertThat(beans.entrySet(), empty()); } @Test(expected = IllegalArgumentException.class) diff --git a/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc b/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc index e9b62e1b333..7f0c34cadbf 100644 --- a/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc +++ b/spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc @@ -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)