Commit d3f180b6 authored by Stephane Nicoll's avatar Stephane Nicoll

Merge branch '1.5.x'

parents 7f270356 715cf7da
/* /*
* 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"); * 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.
...@@ -20,7 +20,9 @@ import org.springframework.boot.autoconfigure.EnableAutoConfiguration; ...@@ -20,7 +20,9 @@ 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.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.bind.RelaxedPropertyResolver;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.core.env.Environment;
import org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor; import org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor;
/** /**
...@@ -28,6 +30,7 @@ import org.springframework.dao.annotation.PersistenceExceptionTranslationPostPro ...@@ -28,6 +30,7 @@ import org.springframework.dao.annotation.PersistenceExceptionTranslationPostPro
* translation. * translation.
* *
* @author Andy Wilkinson * @author Andy Wilkinson
* @author Stephane Nicoll
* @since 1.2.0 * @since 1.2.0
*/ */
@ConditionalOnClass(PersistenceExceptionTranslationPostProcessor.class) @ConditionalOnClass(PersistenceExceptionTranslationPostProcessor.class)
...@@ -36,10 +39,18 @@ public class PersistenceExceptionTranslationAutoConfiguration { ...@@ -36,10 +39,18 @@ public class PersistenceExceptionTranslationAutoConfiguration {
@Bean @Bean
@ConditionalOnMissingBean(PersistenceExceptionTranslationPostProcessor.class) @ConditionalOnMissingBean(PersistenceExceptionTranslationPostProcessor.class)
@ConditionalOnProperty(prefix = "spring.dao.exceptiontranslation", name = "enabled", matchIfMissing = true) @ConditionalOnProperty(prefix = "spring.dao.exceptiontranslation", name = "enabled", matchIfMissing = true)
public static PersistenceExceptionTranslationPostProcessor persistenceExceptionTranslationPostProcessor() { public static PersistenceExceptionTranslationPostProcessor persistenceExceptionTranslationPostProcessor(
Environment environment) {
PersistenceExceptionTranslationPostProcessor postProcessor = new PersistenceExceptionTranslationPostProcessor(); PersistenceExceptionTranslationPostProcessor postProcessor = new PersistenceExceptionTranslationPostProcessor();
postProcessor.setProxyTargetClass(true); postProcessor.setProxyTargetClass(determineProxyTargetClass(environment));
return postProcessor; 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);
}
} }
/* /*
* 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"); * 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.
...@@ -37,10 +37,10 @@ import org.springframework.stereotype.Repository; ...@@ -37,10 +37,10 @@ import org.springframework.stereotype.Repository;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
/** /**
*
* Tests for {@link PersistenceExceptionTranslationAutoConfiguration} * Tests for {@link PersistenceExceptionTranslationAutoConfiguration}
* *
* @author Andy Wilkinson * @author Andy Wilkinson
* @author Stephane Nicoll
*/ */
public class PersistenceExceptionTranslationAutoConfigurationTests { public class PersistenceExceptionTranslationAutoConfigurationTests {
...@@ -54,7 +54,7 @@ public class PersistenceExceptionTranslationAutoConfigurationTests { ...@@ -54,7 +54,7 @@ public class PersistenceExceptionTranslationAutoConfigurationTests {
} }
@Test @Test
public void exceptionTranslationPostProcessorBeanIsCreated() { public void exceptionTranslationPostProcessorUsesCglibByDefault() {
this.context = new AnnotationConfigApplicationContext( this.context = new AnnotationConfigApplicationContext(
PersistenceExceptionTranslationAutoConfiguration.class); PersistenceExceptionTranslationAutoConfiguration.class);
Map<String, PersistenceExceptionTranslationPostProcessor> beans = this.context Map<String, PersistenceExceptionTranslationPostProcessor> beans = this.context
...@@ -64,7 +64,20 @@ public class PersistenceExceptionTranslationAutoConfigurationTests { ...@@ -64,7 +64,20 @@ public class PersistenceExceptionTranslationAutoConfigurationTests {
} }
@Test @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<String, PersistenceExceptionTranslationPostProcessor> 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(); this.context = new AnnotationConfigApplicationContext();
EnvironmentTestUtils.addEnvironment(this.context, EnvironmentTestUtils.addEnvironment(this.context,
"spring.dao.exceptiontranslation.enabled=false"); "spring.dao.exceptiontranslation.enabled=false");
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment