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 ece100ddea3..32084cb0da8 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 @@ -1,5 +1,5 @@ /* - * Copyright 2012-2014 the original author or authors. + * Copyright 2012-2017 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -20,7 +20,9 @@ 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.boot.bind.RelaxedPropertyResolver; import org.springframework.context.annotation.Bean; +import org.springframework.core.env.Environment; import org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor; /** @@ -28,6 +30,7 @@ import org.springframework.dao.annotation.PersistenceExceptionTranslationPostPro * translation. * * @author Andy Wilkinson + * @author Stephane Nicoll * @since 1.2.0 */ @ConditionalOnClass(PersistenceExceptionTranslationPostProcessor.class) @@ -36,10 +39,18 @@ public class PersistenceExceptionTranslationAutoConfiguration { @Bean @ConditionalOnMissingBean(PersistenceExceptionTranslationPostProcessor.class) @ConditionalOnProperty(prefix = "spring.dao.exceptiontranslation", name = "enabled", matchIfMissing = true) - public static PersistenceExceptionTranslationPostProcessor persistenceExceptionTranslationPostProcessor() { + public static PersistenceExceptionTranslationPostProcessor persistenceExceptionTranslationPostProcessor( + Environment environment) { PersistenceExceptionTranslationPostProcessor postProcessor = new PersistenceExceptionTranslationPostProcessor(); - postProcessor.setProxyTargetClass(true); + postProcessor.setProxyTargetClass(determineProxyTargetClass(environment)); return postProcessor; } + private static boolean determineProxyTargetClass(Environment environment) { + RelaxedPropertyResolver resolver = new RelaxedPropertyResolver(environment, + "spring.aop."); + Boolean value = resolver.getProperty("proxyTargetClass", Boolean.class); + return (value != null ? value : true); + } + } 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 a8f9065bc46..495797452bb 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 @@ -1,5 +1,5 @@ /* - * Copyright 2012-2016 the original author or authors. + * Copyright 2012-2017 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -37,10 +37,10 @@ import org.springframework.stereotype.Repository; import static org.assertj.core.api.Assertions.assertThat; /** - * * Tests for {@link PersistenceExceptionTranslationAutoConfiguration} * * @author Andy Wilkinson + * @author Stephane Nicoll */ public class PersistenceExceptionTranslationAutoConfigurationTests { @@ -54,7 +54,7 @@ public class PersistenceExceptionTranslationAutoConfigurationTests { } @Test - public void exceptionTranslationPostProcessorBeanIsCreated() { + public void exceptionTranslationPostProcessorUsesCglibByDefault() { this.context = new AnnotationConfigApplicationContext( PersistenceExceptionTranslationAutoConfiguration.class); Map beans = this.context @@ -64,7 +64,20 @@ public class PersistenceExceptionTranslationAutoConfigurationTests { } @Test - public void exceptionTranslationPostProcessorBeanIsDisabled() { + public void exceptionTranslationPostProcessorCanBeConfiguredToUseJdkProxy() { + this.context = new AnnotationConfigApplicationContext(); + EnvironmentTestUtils.addEnvironment(this.context, + "spring.aop.proxyTargetClass=false"); + this.context.register(PersistenceExceptionTranslationAutoConfiguration.class); + this.context.refresh(); + Map beans = this.context + .getBeansOfType(PersistenceExceptionTranslationPostProcessor.class); + assertThat(beans).hasSize(1); + assertThat(beans.values().iterator().next().isProxyTargetClass()).isFalse(); + } + + @Test + public void exceptionTranslationPostProcessorCanBeDisabled() { this.context = new AnnotationConfigApplicationContext(); EnvironmentTestUtils.addEnvironment(this.context, "spring.dao.exceptiontranslation.enabled=false");