Commit a0ef61a2 authored by Stephane Nicoll's avatar Stephane Nicoll

Enable proxy target class for `MethodValidationPostProcessor`

Closes gh-8277
parent 4aecf19c
...@@ -53,6 +53,7 @@ public class ValidationAutoConfiguration { ...@@ -53,6 +53,7 @@ public class ValidationAutoConfiguration {
public MethodValidationPostProcessor methodValidationPostProcessor( public MethodValidationPostProcessor methodValidationPostProcessor(
Validator validator) { Validator validator) {
MethodValidationPostProcessor processor = new MethodValidationPostProcessor(); MethodValidationPostProcessor processor = new MethodValidationPostProcessor();
processor.setProxyTargetClass(true);
processor.setValidator(validator); processor.setValidator(validator);
return processor; return processor;
} }
......
/* /*
* 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.
...@@ -18,6 +18,7 @@ package org.springframework.boot.autoconfigure.validation; ...@@ -18,6 +18,7 @@ package org.springframework.boot.autoconfigure.validation;
import javax.validation.ConstraintViolationException; import javax.validation.ConstraintViolationException;
import javax.validation.Validator; import javax.validation.Validator;
import javax.validation.constraints.Min;
import javax.validation.constraints.Size; import javax.validation.constraints.Size;
import org.junit.After; import org.junit.After;
...@@ -63,6 +64,17 @@ public class ValidationAutoConfigurationTests { ...@@ -63,6 +64,17 @@ public class ValidationAutoConfigurationTests {
service.doSomething("KO"); 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 @Test
public void userDefinedMethodValidationPostProcessorTakesPrecedence() { public void userDefinedMethodValidationPostProcessorTakesPrecedence() {
load(SampleConfiguration.class); load(SampleConfiguration.class);
...@@ -97,6 +109,20 @@ public class ValidationAutoConfigurationTests { ...@@ -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 @Configuration
static class SampleConfiguration { static class SampleConfiguration {
......
...@@ -142,6 +142,18 @@ public class ConfigurationPropertiesBindingPostProcessorTests { ...@@ -142,6 +142,18 @@ public class ConfigurationPropertiesBindingPostProcessorTests {
this.context.refresh(); 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 @Test
public void testInitializersSeeBoundProperties() { public void testInitializersSeeBoundProperties() {
MockEnvironment env = new MockEnvironment(); MockEnvironment env = new MockEnvironment();
...@@ -486,6 +498,39 @@ public class ConfigurationPropertiesBindingPostProcessorTests { ...@@ -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 @Configuration
@EnableConfigurationProperties @EnableConfigurationProperties
public static class TestConfigurationWithCustomValidator { public static class TestConfigurationWithCustomValidator {
......
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