Enable proxy target class for `MethodValidationPostProcessor`
Closes gh-8277
This commit is contained in:
parent
4aecf19c92
commit
a0ef61a27d
|
@ -53,6 +53,7 @@ public class ValidationAutoConfiguration {
|
|||
public MethodValidationPostProcessor methodValidationPostProcessor(
|
||||
Validator validator) {
|
||||
MethodValidationPostProcessor processor = new MethodValidationPostProcessor();
|
||||
processor.setProxyTargetClass(true);
|
||||
processor.setValidator(validator);
|
||||
return processor;
|
||||
}
|
||||
|
|
|
@ -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.
|
||||
|
@ -18,6 +18,7 @@ package org.springframework.boot.autoconfigure.validation;
|
|||
|
||||
import javax.validation.ConstraintViolationException;
|
||||
import javax.validation.Validator;
|
||||
import javax.validation.constraints.Min;
|
||||
import javax.validation.constraints.Size;
|
||||
|
||||
import org.junit.After;
|
||||
|
@ -63,6 +64,17 @@ public class ValidationAutoConfigurationTests {
|
|||
service.doSomething("KO");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void validationUsesCglibProxy() {
|
||||
load(DefaultAnotherSampleService.class);
|
||||
assertThat(this.context.getBeansOfType(Validator.class)).hasSize(1);
|
||||
DefaultAnotherSampleService service = this.context
|
||||
.getBean(DefaultAnotherSampleService.class);
|
||||
service.doSomething(42);
|
||||
this.thrown.expect(ConstraintViolationException.class);
|
||||
service.doSomething(2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void userDefinedMethodValidationPostProcessorTakesPrecedence() {
|
||||
load(SampleConfiguration.class);
|
||||
|
@ -97,6 +109,20 @@ public class ValidationAutoConfigurationTests {
|
|||
|
||||
}
|
||||
|
||||
interface AnotherSampleService {
|
||||
|
||||
void doSomething(@Min(42) Integer counter);
|
||||
}
|
||||
|
||||
@Validated
|
||||
static class DefaultAnotherSampleService implements AnotherSampleService {
|
||||
|
||||
@Override
|
||||
public void doSomething(Integer counter) {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@Configuration
|
||||
static class SampleConfiguration {
|
||||
|
||||
|
|
|
@ -142,6 +142,18 @@ public class ConfigurationPropertiesBindingPostProcessorTests {
|
|||
this.context.refresh();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSuccessfulValidationWithInterface() {
|
||||
MockEnvironment env = new MockEnvironment();
|
||||
env.setProperty("test.foo", "bar");
|
||||
this.context = new AnnotationConfigApplicationContext();
|
||||
this.context.setEnvironment(env);
|
||||
this.context.register(TestConfigurationWithValidationAndInterface.class);
|
||||
this.context.refresh();
|
||||
assertThat(this.context.getBean(ValidatedPropertiesImpl.class).getFoo())
|
||||
.isEqualTo("bar");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInitializersSeeBoundProperties() {
|
||||
MockEnvironment env = new MockEnvironment();
|
||||
|
@ -486,6 +498,39 @@ public class ConfigurationPropertiesBindingPostProcessorTests {
|
|||
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@EnableConfigurationProperties
|
||||
public static class TestConfigurationWithValidationAndInterface {
|
||||
|
||||
@Bean
|
||||
public ValidatedPropertiesImpl testProperties() {
|
||||
return new ValidatedPropertiesImpl();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
interface ValidatedProperties {
|
||||
|
||||
String getFoo();
|
||||
}
|
||||
|
||||
@ConfigurationProperties("test")
|
||||
@Validated
|
||||
public static class ValidatedPropertiesImpl implements ValidatedProperties {
|
||||
|
||||
@NotNull
|
||||
private String foo;
|
||||
|
||||
@Override
|
||||
public String getFoo() {
|
||||
return this.foo;
|
||||
}
|
||||
|
||||
public void setFoo(String foo) {
|
||||
this.foo = foo;
|
||||
}
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@EnableConfigurationProperties
|
||||
public static class TestConfigurationWithCustomValidator {
|
||||
|
|
Loading…
Reference in New Issue