mirror of
https://github.com/spring-projects/spring-boot.git
synced 2024-09-03 04:26:12 +08:00
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 `MethodValidationPostProcessor` was always configured to use cglib, regardless of the value of that property. Closes gh-8869
This commit is contained in:
parent
b148b46751
commit
a00b695748
@ -24,10 +24,12 @@ 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.ConditionalOnResource;
|
||||
import org.springframework.boot.bind.RelaxedPropertyResolver;
|
||||
import org.springframework.boot.validation.MessageInterpolatorFactory;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Role;
|
||||
import org.springframework.core.env.Environment;
|
||||
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;
|
||||
import org.springframework.validation.beanvalidation.MethodValidationPostProcessor;
|
||||
|
||||
@ -56,11 +58,18 @@ public class ValidationAutoConfiguration {
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
public static MethodValidationPostProcessor methodValidationPostProcessor(
|
||||
Validator validator) {
|
||||
Environment environment, Validator validator) {
|
||||
MethodValidationPostProcessor processor = new MethodValidationPostProcessor();
|
||||
processor.setProxyTargetClass(true);
|
||||
processor.setProxyTargetClass(determineProxyTargetClass(environment));
|
||||
processor.setValidator(validator);
|
||||
return processor;
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -27,6 +27,7 @@ import org.junit.Test;
|
||||
import org.junit.rules.ExpectedException;
|
||||
|
||||
import org.springframework.beans.DirectFieldAccessor;
|
||||
import org.springframework.boot.test.util.EnvironmentTestUtils;
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
@ -75,6 +76,19 @@ public class ValidationAutoConfigurationTests {
|
||||
service.doSomething(2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void validationCanBeConfiguredToUseJdkProxy() {
|
||||
load(AnotherSampleServiceConfiguration.class, "spring.aop.proxy-target-class=false");
|
||||
assertThat(this.context.getBeansOfType(Validator.class)).hasSize(1);
|
||||
assertThat(this.context.getBeansOfType(DefaultAnotherSampleService.class))
|
||||
.isEmpty();
|
||||
AnotherSampleService service = this.context
|
||||
.getBean(AnotherSampleService.class);
|
||||
service.doSomething(42);
|
||||
this.thrown.expect(ConstraintViolationException.class);
|
||||
service.doSomething(2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void userDefinedMethodValidationPostProcessorTakesPrecedence() {
|
||||
load(SampleConfiguration.class);
|
||||
@ -90,14 +104,15 @@ public class ValidationAutoConfigurationTests {
|
||||
.getPropertyValue("validator"));
|
||||
}
|
||||
|
||||
public void load(Class<?> config) {
|
||||
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
|
||||
public void load(Class<?> config, String... environment) {
|
||||
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
|
||||
EnvironmentTestUtils.addEnvironment(ctx, environment);
|
||||
if (config != null) {
|
||||
applicationContext.register(config);
|
||||
ctx.register(config);
|
||||
}
|
||||
applicationContext.register(ValidationAutoConfiguration.class);
|
||||
applicationContext.refresh();
|
||||
this.context = applicationContext;
|
||||
ctx.register(ValidationAutoConfiguration.class);
|
||||
ctx.refresh();
|
||||
this.context = ctx;
|
||||
}
|
||||
|
||||
@Validated
|
||||
@ -123,6 +138,16 @@ public class ValidationAutoConfigurationTests {
|
||||
}
|
||||
}
|
||||
|
||||
@Configuration
|
||||
static class AnotherSampleServiceConfiguration {
|
||||
|
||||
@Bean
|
||||
public AnotherSampleService anotherSampleService() {
|
||||
return new DefaultAnotherSampleService();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Configuration
|
||||
static class SampleConfiguration {
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user