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:
parent
a1a643ca24
commit
58d660d10d
|
|
@ -19,6 +19,7 @@ package org.springframework.boot.autoconfigure.dao;
|
||||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||||
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor;
|
import org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor;
|
||||||
|
|
||||||
|
|
@ -34,8 +35,11 @@ public class PersistenceExceptionTranslationAutoConfiguration {
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
@ConditionalOnMissingBean(PersistenceExceptionTranslationPostProcessor.class)
|
@ConditionalOnMissingBean(PersistenceExceptionTranslationPostProcessor.class)
|
||||||
|
@ConditionalOnProperty(prefix = "spring.dao.exceptiontranslation", name = "enabled", matchIfMissing = true)
|
||||||
public PersistenceExceptionTranslationPostProcessor persistenceExceptionTranslationPostProcessor() {
|
public PersistenceExceptionTranslationPostProcessor persistenceExceptionTranslationPostProcessor() {
|
||||||
return new PersistenceExceptionTranslationPostProcessor();
|
PersistenceExceptionTranslationPostProcessor postProcessor = new PersistenceExceptionTranslationPostProcessor();
|
||||||
|
postProcessor.setProxyTargetClass(true);
|
||||||
|
return postProcessor;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -17,6 +17,12 @@
|
||||||
"description": "Execute all Spring Batch jobs in the context on startup.",
|
"description": "Execute all Spring Batch jobs in the context on startup.",
|
||||||
"defaultValue": true
|
"defaultValue": true
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"name": "spring.dao.exceptiontranslation.enabled",
|
||||||
|
"dataType": "java.lang.Boolean",
|
||||||
|
"description": "Enables the PersistenceExceptionTranslationPostProcessor.",
|
||||||
|
"defaultValue": true
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"name": "spring.datasource.jmx-enabled",
|
"name": "spring.datasource.jmx-enabled",
|
||||||
"dataType": "java.lang.Boolean",
|
"dataType": "java.lang.Boolean",
|
||||||
|
|
|
||||||
|
|
@ -25,6 +25,7 @@ import org.junit.After;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.springframework.boot.autoconfigure.jdbc.EmbeddedDataSourceConfiguration;
|
import org.springframework.boot.autoconfigure.jdbc.EmbeddedDataSourceConfiguration;
|
||||||
import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;
|
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.AnnotationConfigApplicationContext;
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
|
@ -32,6 +33,7 @@ import org.springframework.dao.InvalidDataAccessApiUsageException;
|
||||||
import org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor;
|
import org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor;
|
||||||
import org.springframework.stereotype.Repository;
|
import org.springframework.stereotype.Repository;
|
||||||
|
|
||||||
|
import static org.hamcrest.Matchers.empty;
|
||||||
import static org.hamcrest.Matchers.equalTo;
|
import static org.hamcrest.Matchers.equalTo;
|
||||||
import static org.hamcrest.Matchers.is;
|
import static org.hamcrest.Matchers.is;
|
||||||
import static org.junit.Assert.assertThat;
|
import static org.junit.Assert.assertThat;
|
||||||
|
|
@ -60,6 +62,19 @@ public class PersistenceExceptionTranslationAutoConfigurationTests {
|
||||||
Map<String, PersistenceExceptionTranslationPostProcessor> beans = this.context
|
Map<String, PersistenceExceptionTranslationPostProcessor> beans = this.context
|
||||||
.getBeansOfType(PersistenceExceptionTranslationPostProcessor.class);
|
.getBeansOfType(PersistenceExceptionTranslationPostProcessor.class);
|
||||||
assertThat(beans.size(), is(equalTo(1)));
|
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)
|
@Test(expected = IllegalArgumentException.class)
|
||||||
|
|
|
||||||
|
|
@ -205,6 +205,9 @@ content into your application; rather pick only the properties that you need.
|
||||||
spring.datasource.max-wait=
|
spring.datasource.max-wait=
|
||||||
spring.datasource.jmx-enabled=true # Export JMX MBeans (if supported)
|
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])
|
# MONGODB ({sc-spring-boot-autoconfigure}/mongo/MongoProperties.{sc-ext}[MongoProperties])
|
||||||
spring.data.mongodb.host= # the db host
|
spring.data.mongodb.host= # the db host
|
||||||
spring.data.mongodb.port=27017 # the connection port (defaults to 27107)
|
spring.data.mongodb.port=27017 # the connection port (defaults to 27107)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue