Skip to content

Commit 6b3329b

Browse files
committed
Add support for annotations on constructor parameter binding
Closes gh-17109
1 parent e6151a6 commit 6b3329b

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ private Object getDefaultValue(ConstructorParameter parameter, BindConverter con
8383
private Object bind(ConstructorParameter parameter, BeanPropertyBinder propertyBinder) {
8484
String propertyName = parameter.getName();
8585
ResolvableType type = parameter.getType();
86-
return propertyBinder.bindProperty(propertyName, Bindable.of(type));
86+
return propertyBinder.bindProperty(propertyName, Bindable.of(type).withAnnotations(parameter.getAnnotations()));
8787
}
8888

8989
private static final class Bean {

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

+26
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package org.springframework.boot.context.properties.bind;
1717

18+
import java.time.LocalDate;
1819
import java.util.ArrayList;
1920
import java.util.List;
2021

@@ -23,6 +24,7 @@
2324
import org.springframework.boot.context.properties.source.ConfigurationPropertyName;
2425
import org.springframework.boot.context.properties.source.ConfigurationPropertySource;
2526
import org.springframework.boot.context.properties.source.MockConfigurationPropertySource;
27+
import org.springframework.format.annotation.DateTimeFormat;
2628

2729
import static org.assertj.core.api.Assertions.assertThat;
2830

@@ -137,6 +139,16 @@ void bindToClassWithNoValueAndDefaultValueShouldUseDefault() {
137139
assertThat(bean.getCustomList()).containsOnly("x,y,z");
138140
}
139141

142+
@Test
143+
void bindWithAnnotations() {
144+
MockConfigurationPropertySource source = new MockConfigurationPropertySource();
145+
source.put("foo.date", "2014-04-01");
146+
this.sources.add(source);
147+
ConverterAnnotatedExampleBean bean = this.binder.bind("foo", Bindable.of(ConverterAnnotatedExampleBean.class))
148+
.get();
149+
assertThat(bean.getDate().toString()).isEqualTo("2014-04-01");
150+
}
151+
140152
public static class ExampleValueBean {
141153

142154
private final int intValue;
@@ -265,4 +277,18 @@ public List<String> getCustomList() {
265277

266278
}
267279

280+
public static class ConverterAnnotatedExampleBean {
281+
282+
private final LocalDate date;
283+
284+
ConverterAnnotatedExampleBean(@DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate date) {
285+
this.date = date;
286+
}
287+
288+
public LocalDate getDate() {
289+
return this.date;
290+
}
291+
292+
}
293+
268294
}

0 commit comments

Comments
 (0)