Skip to content

Commit 0c98d0e

Browse files
committed
Polish "Allow validation api without implementation"
Closes gh-12669
1 parent a74dc74 commit 0c98d0e

File tree

3 files changed

+23
-17
lines changed

3 files changed

+23
-17
lines changed

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/ConfigurationPropertiesBinder.java

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,9 @@ class ConfigurationPropertiesBinder {
5555

5656
private final Validator configurationPropertiesValidator;
5757

58-
private Validator jsr303Validator;
58+
private final boolean jsr303Present;
59+
60+
private volatile Validator jsr303Validator;
5961

6062
private volatile Binder binder;
6163

@@ -66,7 +68,8 @@ class ConfigurationPropertiesBinder {
6668
.getPropertySources();
6769
this.configurationPropertiesValidator = getConfigurationPropertiesValidator(
6870
applicationContext, validatorBeanName);
69-
71+
this.jsr303Present = ConfigurationPropertiesJsr303Validator
72+
.isJsr303Present(applicationContext);
7073
}
7174

7275
public void bind(Bindable<?> target) {
@@ -92,19 +95,23 @@ private List<Validator> getValidators(Bindable<?> target) {
9295
if (this.configurationPropertiesValidator != null) {
9396
validators.add(this.configurationPropertiesValidator);
9497
}
95-
96-
if (target.getAnnotation(Validated.class) != null) {
97-
this.jsr303Validator = ConfigurationPropertiesJsr303Validator.getIfJsr303Present(this.applicationContext);
98-
if (this.jsr303Validator != null) {
99-
validators.add(this.jsr303Validator);
100-
}
98+
if (this.jsr303Present && target.getAnnotation(Validated.class) != null) {
99+
validators.add(getJsr303Validator());
101100
}
102101
if (target.getValue() != null && target.getValue().get() instanceof Validator) {
103102
validators.add((Validator) target.getValue().get());
104103
}
105104
return validators;
106105
}
107106

107+
private Validator getJsr303Validator() {
108+
if (this.jsr303Validator == null) {
109+
this.jsr303Validator = new ConfigurationPropertiesJsr303Validator(
110+
this.applicationContext);
111+
}
112+
return this.jsr303Validator;
113+
}
114+
108115
private BindHandler getBindHandler(ConfigurationProperties annotation,
109116
List<Validator> validators) {
110117
BindHandler handler = new IgnoreTopLevelConverterNotFoundBindHandler();

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/ConfigurationPropertiesJsr303Validator.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,7 @@ final class ConfigurationPropertiesJsr303Validator implements Validator {
3838

3939
private final Delegate delegate;
4040

41-
private ConfigurationPropertiesJsr303Validator(
42-
ApplicationContext applicationContext) {
41+
ConfigurationPropertiesJsr303Validator(ApplicationContext applicationContext) {
4342
this.delegate = new Delegate(applicationContext);
4443
}
4544

@@ -53,15 +52,14 @@ public void validate(Object target, Errors errors) {
5352
this.delegate.validate(target, errors);
5453
}
5554

56-
public static ConfigurationPropertiesJsr303Validator getIfJsr303Present(
57-
ApplicationContext applicationContext) {
55+
public static boolean isJsr303Present(ApplicationContext applicationContext) {
5856
ClassLoader classLoader = applicationContext.getClassLoader();
5957
for (String validatorClass : VALIDATOR_CLASSES) {
6058
if (!ClassUtils.isPresent(validatorClass, classLoader)) {
61-
return null;
59+
return false;
6260
}
6361
}
64-
return new ConfigurationPropertiesJsr303Validator(applicationContext);
62+
return true;
6563
}
6664

6765
private static class Delegate extends LocalValidatorFactoryBean {

spring-boot-project/spring-boot/src/test/java/org/springframework/boot/diagnostics/analyzer/ValidationExceptionFailureAnalyzerTests.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2017 the original author or authors.
2+
* Copyright 2012-2018 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.
@@ -54,7 +54,8 @@ public void validatedPropertiesTest() {
5454

5555
@Test
5656
public void nonValidatedPropertiesTest() {
57-
new AnnotationConfigApplicationContext(NonValidatedTestConfiguration.class).close();
57+
new AnnotationConfigApplicationContext(NonValidatedTestConfiguration.class)
58+
.close();
5859
}
5960

6061
@EnableConfigurationProperties(TestProperties.class)
@@ -71,7 +72,6 @@ private static class TestProperties {
7172

7273
}
7374

74-
7575
@EnableConfigurationProperties(NonValidatedTestProperties.class)
7676
static class NonValidatedTestConfiguration {
7777

@@ -84,4 +84,5 @@ static class NonValidatedTestConfiguration {
8484
private static class NonValidatedTestProperties {
8585

8686
}
87+
8788
}

0 commit comments

Comments
 (0)