Do not enable cglib if spring.aop.proxy-target-class is configured

This commit makes sure to honour the `spring.aop.proxy-target-class`
property if set by the user. Previously, the
`PersistenceExceptionTranslationPostProcessor` was always configured to
use cglib, regardless of the value of that property.

Closes gh-8887
This commit is contained in:
Stephane Nicoll 2017-04-12 10:55:43 +02:00
parent 0affa3af3a
commit 715cf7da75
2 changed files with 31 additions and 7 deletions

View File

@ -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);
}
}

View File

@ -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<String, PersistenceExceptionTranslationPostProcessor> 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<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();
EnvironmentTestUtils.addEnvironment(this.context,
"spring.dao.exceptiontranslation.enabled=false");