Skip to content

Commit 46e4c3c

Browse files
committed
Support to inherit common properties for configuring multiple beans
It's used for configuring multiple beans (spring-projects#15732), we could extract common or use primary properties as parent now. Take `org.springframework.boot.autoconfigure.data.redis.RedisProperties` for example, given: ``` # primary spring.data.redis: host: 127.0.0.1 port: 6379 # additional additional.data.redis: port: 6380 ``` Then effective properties: ``` additional.data.redis: host: 127.0.0.1 port: 6380 ``` should be bound to `additionalRedisProperties`: ```java @bean(autowireCandidate = false) // do not back off autoconfigured one @ConfigurationProperties(prefix = "additional.data.redis", inheritedPrefix = "spring.data.redis") RedisProperties additionalRedisProperties() { return new RedisProperties(); } ```
1 parent 35361d1 commit 46e4c3c

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed

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

+9
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
* values are externalized.
4040
*
4141
* @author Dave Syer
42+
* @author Yanming Zhou
4243
* @since 1.0.0
4344
* @see ConfigurationPropertiesScan
4445
* @see ConstructorBinding
@@ -69,6 +70,14 @@
6970
@AliasFor("value")
7071
String prefix() default "";
7172

73+
/**
74+
* The prefix of the properties that {@link #prefix()} will inherit, It's used for
75+
* configuring multiple beans which share common properties.
76+
* @return the prefix of the properties to inherit
77+
* @see #prefix()
78+
*/
79+
String inheritedPrefix() default "";
80+
7281
/**
7382
* Flag to indicate that when binding to this object invalid fields should be ignored.
7483
* Invalid means invalid according to the binder that is used, and usually this means

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

+46
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@
128128
* @author Stephane Nicoll
129129
* @author Madhura Bhave
130130
* @author Vladislav Kisel
131+
* @author Yanming Zhou
131132
*/
132133
@ExtendWith(OutputCaptureExtension.class)
133134
class ConfigurationPropertiesTests {
@@ -1270,6 +1271,15 @@ void loadWhenBindingToJavaBeanWithConversionToCustomListImplementation() {
12701271
assertThat(this.context.getBean(SetterBoundCustomListProperties.class).getValues()).containsExactly("a", "b");
12711272
}
12721273

1274+
@Test
1275+
void bindMultipleBeansUsingInheritedPrefix() {
1276+
load(InheritedPrefixConfiguration.class, "spring.service.host=127.0.0.1", "spring.service.port=6379",
1277+
"additional.service.port=6380");
1278+
ServiceProperties properties = this.context.getBean("additionalServiceProperties", ServiceProperties.class);
1279+
assertThat(properties.getHost()).isEqualTo("127.0.0.1");
1280+
assertThat(properties.getPort()).isEqualTo(6380);
1281+
}
1282+
12731283
private AnnotationConfigApplicationContext load(Class<?> configuration, String... inlinedProperties) {
12741284
return load(new Class<?>[] { configuration }, inlinedProperties);
12751285
}
@@ -3310,4 +3320,40 @@ static final class CustomList<E> extends ArrayList<E> {
33103320

33113321
}
33123322

3323+
@ConfigurationProperties(prefix = "spring.service")
3324+
static class ServiceProperties {
3325+
3326+
private String host = "localhost";
3327+
3328+
private int port = 6379;
3329+
3330+
String getHost() {
3331+
return this.host;
3332+
}
3333+
3334+
void setHost(String host) {
3335+
this.host = host;
3336+
}
3337+
3338+
int getPort() {
3339+
return this.port;
3340+
}
3341+
3342+
void setPort(int port) {
3343+
this.port = port;
3344+
}
3345+
3346+
}
3347+
3348+
@EnableConfigurationProperties(ServiceProperties.class)
3349+
static class InheritedPrefixConfiguration {
3350+
3351+
@Bean(autowireCandidate = false) // do not back off auto-configured one
3352+
@ConfigurationProperties(prefix = "additional.service", inheritedPrefix = "spring.service")
3353+
ServiceProperties additionalServiceProperties() {
3354+
return new ServiceProperties();
3355+
}
3356+
3357+
}
3358+
33133359
}

0 commit comments

Comments
 (0)