Skip to content

Commit 39fed4a

Browse files
committed
Fix binding detection of ConfigurationProperties contributed by @bean
This commit makes sure that a ConfigurationProperties type contributed by a `@Bean` factory method uses properties binding regardless of the presence of a matching constructor. `@Bean` method makes sure the user is in control and will be responsible of creating the instance. As a result, binding of properties will not happen there and therefore can only happen with regular JavaBean accessors. Closes gh-18184
1 parent 0c0e2dd commit 39fed4a

File tree

3 files changed

+79
-0
lines changed

3 files changed

+79
-0
lines changed

spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/main/java/org/springframework/boot/configurationprocessor/PropertyDescriptorResolver.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ class PropertyDescriptorResolver {
5151
*/
5252
Stream<PropertyDescriptor<?>> resolve(TypeElement type, ExecutableElement factoryMethod) {
5353
TypeElementMembers members = new TypeElementMembers(this.environment, type);
54+
if (factoryMethod != null) {
55+
return resolveJavaBeanProperties(type, factoryMethod, members);
56+
}
5457
ExecutableElement constructor = resolveConstructor(type);
5558
if (constructor != null) {
5659
return resolveConstructorProperties(type, factoryMethod, members, constructor);

spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationprocessor/MethodBasedMetadataGenerationTests.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import org.springframework.boot.configurationsample.method.PrivateMethodConfig;
2929
import org.springframework.boot.configurationsample.method.ProtectedMethodConfig;
3030
import org.springframework.boot.configurationsample.method.PublicMethodConfig;
31+
import org.springframework.boot.configurationsample.method.SingleConstructorMethodConfig;
3132

3233
import static org.assertj.core.api.Assertions.assertThat;
3334

@@ -86,6 +87,17 @@ void methodAndClassConfig() {
8687
.has(Metadata.withProperty("conflict.value", String.class).fromSource(MethodAndClassConfig.class));
8788
}
8889

90+
@Test
91+
void singleConstructorMethodConfig() {
92+
ConfigurationMetadata metadata = compile(SingleConstructorMethodConfig.class);
93+
assertThat(metadata).doesNotHave(Metadata.withProperty("foo.my-service", Object.class)
94+
.fromSource(SingleConstructorMethodConfig.Foo.class));
95+
assertThat(metadata).has(
96+
Metadata.withProperty("foo.name", String.class).fromSource(SingleConstructorMethodConfig.Foo.class));
97+
assertThat(metadata).has(Metadata.withProperty("foo.flag", Boolean.class).withDefaultValue(false)
98+
.fromSource(SingleConstructorMethodConfig.Foo.class));
99+
}
100+
89101
@Test
90102
void emptyTypeMethodConfig() {
91103
ConfigurationMetadata metadata = compile(EmptyTypeMethodConfig.class);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
* Copyright 2012-2019 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.configurationsample.method;
18+
19+
import org.springframework.boot.configurationsample.ConfigurationProperties;
20+
21+
/**
22+
* Sample for testing method configuration that uses a constructor that should not be
23+
* associated to constructor binding.
24+
*
25+
* @author Stephane Nicoll
26+
*/
27+
public class SingleConstructorMethodConfig {
28+
29+
@ConfigurationProperties(prefix = "foo")
30+
public Foo foo() {
31+
return new Foo(new Object());
32+
}
33+
34+
public static class Foo {
35+
36+
private String name;
37+
38+
private boolean flag;
39+
40+
private final Object myService;
41+
42+
public Foo(Object myService) {
43+
this.myService = myService;
44+
}
45+
46+
public String getName() {
47+
return this.name;
48+
}
49+
50+
public void setName(String name) {
51+
this.name = name;
52+
}
53+
54+
public boolean isFlag() {
55+
return this.flag;
56+
}
57+
58+
public void setFlag(boolean flag) {
59+
this.flag = flag;
60+
}
61+
62+
}
63+
64+
}

0 commit comments

Comments
 (0)