Skip to content

Commit 4ff8126

Browse files
committed
Make binder API private again
Closes spring-projectsgh-10867
1 parent 0f69a15 commit 4ff8126

File tree

6 files changed

+48
-67
lines changed

6 files changed

+48
-67
lines changed

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

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
import org.springframework.boot.context.properties.source.ConfigurationPropertySource;
2727
import org.springframework.boot.context.properties.source.ConfigurationPropertySources;
2828
import org.springframework.boot.context.properties.source.UnboundElementsSourceFilter;
29-
import org.springframework.core.annotation.AnnotationUtils;
3029
import org.springframework.core.convert.ConversionService;
3130
import org.springframework.core.env.PropertySource;
3231
import org.springframework.util.Assert;
@@ -39,10 +38,9 @@
3938
* {@link PropertySource}.
4039
*
4140
* @author Stephane Nicoll
42-
* @since 2.0.0
4341
* @see ConfigurationPropertiesBinderBuilder
4442
*/
45-
public class ConfigurationPropertiesBinder {
43+
class ConfigurationPropertiesBinder {
4644

4745
private final Iterable<PropertySource<?>> propertySources;
4846

@@ -61,20 +59,6 @@ public class ConfigurationPropertiesBinder {
6159
this.configurationSources = ConfigurationPropertySources.from(propertySources);
6260
}
6361

64-
/**
65-
* Bind the specified {@code target} object if it is annotated with
66-
* {@link ConfigurationProperties}, otherwise ignore it.
67-
* @param target the target to bind the configuration property sources to
68-
* @throws ConfigurationPropertiesBindingException if the binding failed
69-
*/
70-
public void bind(Object target) {
71-
ConfigurationProperties annotation = AnnotationUtils
72-
.findAnnotation(target.getClass(), ConfigurationProperties.class);
73-
if (annotation != null) {
74-
bind(target, annotation);
75-
}
76-
}
77-
7862
/**
7963
* Bind the specified {@code target} object using the configuration defined by the
8064
* specified {@code annotation}.
@@ -93,8 +77,10 @@ void bind(Object target, ConfigurationProperties annotation) {
9377
binder.bind(annotation.prefix(), bindable, handler);
9478
}
9579
catch (Exception ex) {
96-
throw new ConfigurationPropertiesBindingException(target.getClass(),
97-
getAnnotationDetails(annotation), ex);
80+
String message = "Could not bind properties to '"
81+
+ ClassUtils.getShortName(target.getClass()) + "': "
82+
+ getAnnotationDetails(annotation);
83+
throw new ConfigurationPropertiesBindingException(message, ex);
9884
}
9985
}
10086

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,19 +38,18 @@
3838
* {@link ApplicationContext}.
3939
*
4040
* @author Stephane Nicoll
41-
* @since 2.0.0
4241
*/
43-
public class ConfigurationPropertiesBinderBuilder {
42+
class ConfigurationPropertiesBinderBuilder {
4443

4544
/**
4645
* The bean name of the configuration properties validator.
4746
*/
48-
public static final String VALIDATOR_BEAN_NAME = "configurationPropertiesValidator";
47+
private static final String VALIDATOR_BEAN_NAME = ConfigurationPropertiesBindingPostProcessor.VALIDATOR_BEAN_NAME;
4948

5049
/**
5150
* The bean name of the configuration properties conversion service.
5251
*/
53-
public static final String CONVERSION_SERVICE_BEAN_NAME = ConfigurableApplicationContext.CONVERSION_SERVICE_BEAN_NAME;
52+
private static final String CONVERSION_SERVICE_BEAN_NAME = ConfigurableApplicationContext.CONVERSION_SERVICE_BEAN_NAME;
5453

5554
private static final String[] VALIDATOR_CLASSES = { "javax.validation.Validator",
5655
"javax.validation.ValidatorFactory",
@@ -68,7 +67,7 @@ public class ConfigurationPropertiesBinderBuilder {
6867
* Creates an instance with the {@link ApplicationContext} to use.
6968
* @param applicationContext the application context
7069
*/
71-
public ConfigurationPropertiesBinderBuilder(ApplicationContext applicationContext) {
70+
ConfigurationPropertiesBinderBuilder(ApplicationContext applicationContext) {
7271
Assert.notNull(applicationContext, "ApplicationContext must not be null");
7372
this.applicationContext = applicationContext;
7473
}

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

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,32 +17,18 @@
1717
package org.springframework.boot.context.properties;
1818

1919
import org.springframework.core.NestedExceptionUtils;
20-
import org.springframework.util.ClassUtils;
2120

2221
/**
2322
* Exception thrown when a {@code @ConfigurationProperties} annotated object failed to be
2423
* bound.
2524
*
2625
* @author Stephane Nicoll
27-
* @since 2.0.0
2826
*/
29-
public class ConfigurationPropertiesBindingException extends RuntimeException {
27+
class ConfigurationPropertiesBindingException extends RuntimeException {
3028

31-
private final Class<?> targetClass;
32-
33-
public ConfigurationPropertiesBindingException(Class<?> targetClass, String message,
29+
ConfigurationPropertiesBindingException(String message,
3430
Throwable cause) {
35-
super("Could not bind properties to '" + ClassUtils.getShortName(targetClass)
36-
+ "': " + message, cause);
37-
this.targetClass = targetClass;
38-
}
39-
40-
/**
41-
* Return the target type of the object that failed to be bound.
42-
* @return the target {@link Class}
43-
*/
44-
public Class<?> getTargetClass() {
45-
return this.targetClass;
31+
super(message, cause);
4632
}
4733

4834
/**

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,11 @@ public class ConfigurationPropertiesBindingPostProcessor
5555
implements BeanPostProcessor, BeanFactoryAware, EnvironmentAware,
5656
ApplicationContextAware, InitializingBean, PriorityOrdered {
5757

58+
/**
59+
* The bean name of the configuration properties validator.
60+
*/
61+
public static final String VALIDATOR_BEAN_NAME = "configurationPropertiesValidator";
62+
5863
private static final Log logger = LogFactory
5964
.getLog(ConfigurationPropertiesBindingPostProcessor.class);
6065

spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/ConfigurationPropertiesBinderBuilderTests.java

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import org.springframework.boot.context.properties.bind.validation.BindValidationException;
2626
import org.springframework.boot.context.properties.bind.validation.ValidationErrors;
2727
import org.springframework.context.support.StaticApplicationContext;
28+
import org.springframework.core.annotation.AnnotationUtils;
2829
import org.springframework.core.convert.converter.Converter;
2930
import org.springframework.core.convert.support.DefaultConversionService;
3031
import org.springframework.mock.env.MockEnvironment;
@@ -63,7 +64,7 @@ public void useCustomConversionService() {
6364
.withEnvironment(this.environment)
6465
.withConversionService(conversionService).build();
6566
PropertyWithAddress target = new PropertyWithAddress();
66-
binder.bind(target);
67+
bind(binder, target);
6768
assertThat(target.getAddress()).isNotNull();
6869
assertThat(target.getAddress().streetName).isEqualTo("FooStreet");
6970
assertThat(target.getAddress().number).isEqualTo(42);
@@ -86,7 +87,7 @@ public void bindToJavaTimeDuration() {
8687
ConfigurationPropertiesBinder binder = this.builder
8788
.withEnvironment(this.environment).build();
8889
PropertyWithDuration target = new PropertyWithDuration();
89-
binder.bind(target);
90+
bind(binder, target);
9091
assertThat(target.getDuration().getSeconds()).isEqualTo(60);
9192
}
9293

@@ -122,7 +123,7 @@ public void validationWithJsr303() {
122123
.withEnvironment(this.environment).build();
123124
assertThat(
124125
bindWithValidationErrors(binder, new PropertyWithJSR303()).getAllErrors())
125-
.hasSize(2);
126+
.hasSize(2);
126127
}
127128

128129
@Test
@@ -132,15 +133,15 @@ public void validationWithJsr303AndValidInput() {
132133
ConfigurationPropertiesBinder binder = new ConfigurationPropertiesBinder(
133134
this.environment.getPropertySources(), null, null);
134135
PropertyWithJSR303 target = new PropertyWithJSR303();
135-
binder.bind(target);
136+
bind(binder, target);
136137
assertThat(target.getFoo()).isEqualTo("123456");
137138
assertThat(target.getBar()).isEqualTo("654321");
138139
}
139140

140141
private ValidationErrors bindWithValidationErrors(
141142
ConfigurationPropertiesBinder binder, Object target) {
142143
try {
143-
binder.bind(target);
144+
bind(binder, target);
144145
throw new AssertionError("Should have failed to bind " + target);
145146
}
146147
catch (ConfigurationPropertiesBindingException ex) {
@@ -150,6 +151,11 @@ private ValidationErrors bindWithValidationErrors(
150151
}
151152
}
152153

154+
private void bind(ConfigurationPropertiesBinder binder, Object target) {
155+
binder.bind(target, AnnotationUtils
156+
.findAnnotation(target.getClass(), ConfigurationProperties.class));
157+
}
158+
153159
@ConfigurationProperties(prefix = "test")
154160
public static class PropertyWithAddress {
155161

spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/ConfigurationPropertiesBinderTests.java

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import org.springframework.boot.context.properties.bind.BindException;
2626
import org.springframework.boot.context.properties.bind.validation.BindValidationException;
2727
import org.springframework.boot.context.properties.bind.validation.ValidationErrors;
28+
import org.springframework.core.annotation.AnnotationUtils;
2829
import org.springframework.core.env.MapPropertySource;
2930
import org.springframework.core.env.MutablePropertySources;
3031
import org.springframework.core.env.StandardEnvironment;
@@ -61,7 +62,7 @@ public void bindSimpleProperties() {
6162
ConfigurationPropertiesBinder binder = new ConfigurationPropertiesBinder(
6263
this.environment.getPropertySources(), null, null);
6364
PersonProperties target = new PersonProperties();
64-
binder.bind(target);
65+
bind(binder, target);
6566
assertThat(target.name).isEqualTo("John Smith");
6667
assertThat(target.age).isEqualTo(42);
6768
}
@@ -74,7 +75,7 @@ public void bindUnknownFieldFailureMessageContainsDetailsOfPropertyOrigin() {
7475
this.environment.getPropertySources(), null, null);
7576
PersonProperties target = new PersonProperties();
7677
try {
77-
binder.bind(target);
78+
bind(binder, target);
7879
fail("Expected exception");
7980
}
8081
catch (ConfigurationPropertiesBindingException ex) {
@@ -92,17 +93,10 @@ public void bindWithIgnoreInvalidFieldsAnnotation() {
9293
ConfigurationPropertiesBinder binder = new ConfigurationPropertiesBinder(
9394
this.environment.getPropertySources(), null, null);
9495
PropertyWithIgnoreInvalidFields target = new PropertyWithIgnoreInvalidFields();
95-
binder.bind(target);
96+
bind(binder, target);
9697
assertThat(target.getBar()).isEqualTo(0);
9798
}
9899

99-
@Test
100-
public void bindNonAnnotatedObject() {
101-
ConfigurationPropertiesBinder binder = new ConfigurationPropertiesBinder(
102-
this.environment.getPropertySources(), null, null);
103-
binder.bind("FooBar");
104-
}
105-
106100
@Test
107101
public void bindToEnum() {
108102
bindToEnum("test.theValue=foo");
@@ -120,7 +114,7 @@ private void bindToEnum(String property) {
120114
ConfigurationPropertiesBinder binder = new ConfigurationPropertiesBinder(
121115
this.environment.getPropertySources(), null, null);
122116
PropertyWithEnum target = new PropertyWithEnum();
123-
binder.bind(target);
117+
bind(binder, target);
124118
assertThat(target.getTheValue()).isEqualTo(FooEnum.FOO);
125119
}
126120

@@ -136,7 +130,7 @@ private void bindToEnumSet(String property, FooEnum... expected) {
136130
ConfigurationPropertiesBinder binder = new ConfigurationPropertiesBinder(
137131
this.environment.getPropertySources(), null, null);
138132
PropertyWithEnum target = new PropertyWithEnum();
139-
binder.bind(target);
133+
bind(binder, target);
140134
assertThat(target.getTheValues()).contains(expected);
141135
}
142136

@@ -147,7 +141,7 @@ public void bindToCharArray() {
147141
ConfigurationPropertiesBinder binder = new ConfigurationPropertiesBinder(
148142
this.environment.getPropertySources(), null, null);
149143
PropertyWithCharArray target = new PropertyWithCharArray();
150-
binder.bind(target);
144+
bind(binder, target);
151145
assertThat(target.getChars()).isEqualTo("word".toCharArray());
152146
}
153147

@@ -163,7 +157,7 @@ private void testRelaxedPropertyNames(String... pairs) {
163157
ConfigurationPropertiesBinder binder = new ConfigurationPropertiesBinder(
164158
this.environment.getPropertySources(), null, null);
165159
PropertyWithRelaxedNames target = new PropertyWithRelaxedNames();
166-
binder.bind(target);
160+
bind(binder, target);
167161
assertThat(target.getFooBar()).isEqualTo("test2");
168162
assertThat(target.getBarBAZ()).isEqualTo("testb");
169163
}
@@ -175,7 +169,7 @@ public void bindToNestedProperty() {
175169
ConfigurationPropertiesBinder binder = new ConfigurationPropertiesBinder(
176170
this.environment.getPropertySources(), null, null);
177171
PropertyWithNestedValue target = new PropertyWithNestedValue();
178-
binder.bind(target);
172+
bind(binder, target);
179173
assertThat(target.getNested().getValue()).isEqualTo("test1");
180174
}
181175

@@ -186,7 +180,7 @@ public void bindToMap() {
186180
ConfigurationPropertiesBinder binder = new ConfigurationPropertiesBinder(
187181
this.environment.getPropertySources(), null, null);
188182
PropertiesWithMap target = new PropertiesWithMap();
189-
binder.bind(target);
183+
bind(binder, target);
190184
assertThat(target.getMap()).containsOnly(entry("foo", "bar"));
191185
}
192186

@@ -199,7 +193,7 @@ public void bindToMapWithSystemProperties() {
199193
ConfigurationPropertiesBinder binder = new ConfigurationPropertiesBinder(
200194
propertySources, null, null);
201195
PropertiesWithComplexMap target = new PropertiesWithComplexMap();
202-
binder.bind(target);
196+
bind(binder, target);
203197
assertThat(target.getMap()).containsOnlyKeys("foo");
204198
assertThat(target.getMap().get("foo")).containsOnly(entry("bar", "baz"));
205199
}
@@ -214,7 +208,7 @@ public void bindWithOverriddenProperties() {
214208
ConfigurationPropertiesBinder binder = new ConfigurationPropertiesBinder(
215209
propertySources, null, null);
216210
PersonProperties target = new PersonProperties();
217-
binder.bind(target);
211+
bind(binder, target);
218212
assertThat(target.name).isEqualTo("Jane");
219213
}
220214

@@ -226,7 +220,7 @@ public void validationWithSetter() {
226220
this.environment.getPropertySources(), null, null);
227221
PropertyWithValidatingSetter target = new PropertyWithValidatingSetter();
228222
try {
229-
binder.bind(target);
223+
bind(binder, target);
230224
fail("Expected exception");
231225
}
232226
catch (ConfigurationPropertiesBindingException ex) {
@@ -255,15 +249,15 @@ public void validationWithCustomValidatorNotSupported() {
255249
ConfigurationPropertiesBinder binder = new ConfigurationPropertiesBinder(
256250
this.environment.getPropertySources(), null, validator);
257251
PropertyWithValidatingSetter target = new PropertyWithValidatingSetter();
258-
binder.bind(target);
252+
bind(binder, target);
259253
assertThat(target.getFoo()).isEqualTo("bar");
260254
verify(validator, times(0)).validate(eq(target), any(Errors.class));
261255
}
262256

263257
private ValidationErrors bindWithValidationErrors(
264258
ConfigurationPropertiesBinder binder, Object target) {
265259
try {
266-
binder.bind(target);
260+
bind(binder, target);
267261
throw new AssertionError("Should have failed to bind " + target);
268262
}
269263
catch (ConfigurationPropertiesBindingException ex) {
@@ -273,6 +267,11 @@ private ValidationErrors bindWithValidationErrors(
273267
}
274268
}
275269

270+
private void bind(ConfigurationPropertiesBinder binder, Object target) {
271+
binder.bind(target, AnnotationUtils
272+
.findAnnotation(target.getClass(), ConfigurationProperties.class));
273+
}
274+
276275
@ConfigurationProperties(value = "person", ignoreUnknownFields = false)
277276
static class PersonProperties {
278277

0 commit comments

Comments
 (0)