Skip to content

Commit 715cf7d

Browse files
committed
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
1 parent 0affa3a commit 715cf7d

File tree

2 files changed

+31
-7
lines changed

2 files changed

+31
-7
lines changed

spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/dao/PersistenceExceptionTranslationAutoConfiguration.java

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2014 the original author or authors.
2+
* Copyright 2012-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -20,14 +20,17 @@
2020
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
2121
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
2222
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
23+
import org.springframework.boot.bind.RelaxedPropertyResolver;
2324
import org.springframework.context.annotation.Bean;
25+
import org.springframework.core.env.Environment;
2426
import org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor;
2527

2628
/**
2729
* {@link EnableAutoConfiguration Auto-configuration} for Spring's persistence exception
2830
* translation.
2931
*
3032
* @author Andy Wilkinson
33+
* @author Stephane Nicoll
3134
* @since 1.2.0
3235
*/
3336
@ConditionalOnClass(PersistenceExceptionTranslationPostProcessor.class)
@@ -36,10 +39,18 @@ public class PersistenceExceptionTranslationAutoConfiguration {
3639
@Bean
3740
@ConditionalOnMissingBean(PersistenceExceptionTranslationPostProcessor.class)
3841
@ConditionalOnProperty(prefix = "spring.dao.exceptiontranslation", name = "enabled", matchIfMissing = true)
39-
public static PersistenceExceptionTranslationPostProcessor persistenceExceptionTranslationPostProcessor() {
42+
public static PersistenceExceptionTranslationPostProcessor persistenceExceptionTranslationPostProcessor(
43+
Environment environment) {
4044
PersistenceExceptionTranslationPostProcessor postProcessor = new PersistenceExceptionTranslationPostProcessor();
41-
postProcessor.setProxyTargetClass(true);
45+
postProcessor.setProxyTargetClass(determineProxyTargetClass(environment));
4246
return postProcessor;
4347
}
4448

49+
private static boolean determineProxyTargetClass(Environment environment) {
50+
RelaxedPropertyResolver resolver = new RelaxedPropertyResolver(environment,
51+
"spring.aop.");
52+
Boolean value = resolver.getProperty("proxyTargetClass", Boolean.class);
53+
return (value != null ? value : true);
54+
}
55+
4556
}

spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/dao/PersistenceExceptionTranslationAutoConfigurationTests.java

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2016 the original author or authors.
2+
* Copyright 2012-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -37,10 +37,10 @@
3737
import static org.assertj.core.api.Assertions.assertThat;
3838

3939
/**
40-
*
4140
* Tests for {@link PersistenceExceptionTranslationAutoConfiguration}
4241
*
4342
* @author Andy Wilkinson
43+
* @author Stephane Nicoll
4444
*/
4545
public class PersistenceExceptionTranslationAutoConfigurationTests {
4646

@@ -54,7 +54,7 @@ public void close() {
5454
}
5555

5656
@Test
57-
public void exceptionTranslationPostProcessorBeanIsCreated() {
57+
public void exceptionTranslationPostProcessorUsesCglibByDefault() {
5858
this.context = new AnnotationConfigApplicationContext(
5959
PersistenceExceptionTranslationAutoConfiguration.class);
6060
Map<String, PersistenceExceptionTranslationPostProcessor> beans = this.context
@@ -64,7 +64,20 @@ public void exceptionTranslationPostProcessorBeanIsCreated() {
6464
}
6565

6666
@Test
67-
public void exceptionTranslationPostProcessorBeanIsDisabled() {
67+
public void exceptionTranslationPostProcessorCanBeConfiguredToUseJdkProxy() {
68+
this.context = new AnnotationConfigApplicationContext();
69+
EnvironmentTestUtils.addEnvironment(this.context,
70+
"spring.aop.proxyTargetClass=false");
71+
this.context.register(PersistenceExceptionTranslationAutoConfiguration.class);
72+
this.context.refresh();
73+
Map<String, PersistenceExceptionTranslationPostProcessor> beans = this.context
74+
.getBeansOfType(PersistenceExceptionTranslationPostProcessor.class);
75+
assertThat(beans).hasSize(1);
76+
assertThat(beans.values().iterator().next().isProxyTargetClass()).isFalse();
77+
}
78+
79+
@Test
80+
public void exceptionTranslationPostProcessorCanBeDisabled() {
6881
this.context = new AnnotationConfigApplicationContext();
6982
EnvironmentTestUtils.addEnvironment(this.context,
7083
"spring.dao.exceptiontranslation.enabled=false");

0 commit comments

Comments
 (0)