Skip to content

Commit 3e25186

Browse files
committed
Revise usage of @EnableConfigurationProperties
1. use `@EnableConfigurationProperties` to register configuration properties beans if the bean name is not relevant. 2. remove `@EnableConfigurationProperties` without specified value if the `@Configuration` class doesn't contain any `@ConfigurationProperties` bean. Signed-off-by: Yanming Zhou <[email protected]>
1 parent 2904285 commit 3e25186

14 files changed

+17
-59
lines changed

spring-cloud-commons/src/main/java/org/springframework/cloud/commons/util/UtilAutoConfiguration.java

+2-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2020 the original author or authors.
2+
* Copyright 2012-2025 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.
@@ -29,14 +29,9 @@
2929
@Configuration(proxyBeanMethods = false)
3030
@ConditionalOnProperty(value = "spring.cloud.util.enabled", matchIfMissing = true)
3131
@AutoConfigureOrder(0)
32-
@EnableConfigurationProperties
32+
@EnableConfigurationProperties(InetUtilsProperties.class)
3333
public class UtilAutoConfiguration {
3434

35-
@Bean
36-
public InetUtilsProperties inetUtilsProperties() {
37-
return new InetUtilsProperties();
38-
}
39-
4035
@Bean
4136
@ConditionalOnMissingBean
4237
public InetUtils inetUtils(InetUtilsProperties properties) {

spring-cloud-commons/src/test/java/org/springframework/cloud/client/actuator/FeaturesEndpointTests.java

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2020 the original author or authors.
2+
* Copyright 2012-2025 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.
@@ -25,7 +25,6 @@
2525

2626
import org.springframework.beans.factory.annotation.Autowired;
2727
import org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration;
28-
import org.springframework.boot.context.properties.EnableConfigurationProperties;
2928
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
3029
import org.springframework.context.annotation.Bean;
3130
import org.springframework.context.annotation.Configuration;
@@ -85,7 +84,6 @@ HasFeatures localFeatures() {
8584
}
8685

8786
@Configuration(proxyBeanMethods = false)
88-
@EnableConfigurationProperties
8987
public static class Config {
9088

9189
@Autowired(required = false)

spring-cloud-commons/src/test/java/org/springframework/cloud/client/discovery/health/DiscoveryClientHealthIndicatorTests.java

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2020 the original author or authors.
2+
* Copyright 2012-2025 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.
@@ -26,7 +26,6 @@
2626
import org.springframework.boot.actuate.health.HealthContributor;
2727
import org.springframework.boot.actuate.health.HealthIndicator;
2828
import org.springframework.boot.actuate.health.Status;
29-
import org.springframework.boot.context.properties.EnableConfigurationProperties;
3029
import org.springframework.boot.test.context.SpringBootTest;
3130
import org.springframework.cloud.client.CommonsClientAutoConfiguration;
3231
import org.springframework.cloud.client.discovery.DiscoveryClient;
@@ -80,7 +79,6 @@ private Status assertHealth(Health health, Status expected) {
8079
}
8180

8281
@Configuration(proxyBeanMethods = false)
83-
@EnableConfigurationProperties
8482
public static class Config {
8583

8684
@Bean

spring-cloud-context/src/main/java/org/springframework/cloud/bootstrap/encrypt/EncryptionBootstrapConfiguration.java

-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,6 @@ public static TextEncryptor createTextEncryptor(KeyProperties keyProperties, Rsa
7676
@Configuration(proxyBeanMethods = false)
7777
@Conditional(KeyCondition.class)
7878
@ConditionalOnClass({ RsaSecretEncryptor.class, ASN1Sequence.class })
79-
@EnableConfigurationProperties
8079
protected static class RsaEncryptionConfiguration {
8180

8281
@Bean

spring-cloud-context/src/test/java/org/springframework/cloud/bootstrap/TestBootstrapConfiguration.java

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2020 the original author or authors.
2+
* Copyright 2012-2025 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.
@@ -20,7 +20,6 @@
2020
import java.util.List;
2121

2222
import org.springframework.beans.factory.annotation.Qualifier;
23-
import org.springframework.boot.context.properties.EnableConfigurationProperties;
2423
import org.springframework.context.ApplicationContextInitializer;
2524
import org.springframework.context.ApplicationEventPublisher;
2625
import org.springframework.context.ConfigurableApplicationContext;
@@ -37,7 +36,6 @@
3736
*/
3837
@Order(0)
3938
@Configuration(proxyBeanMethods = false)
40-
@EnableConfigurationProperties
4139
public class TestBootstrapConfiguration {
4240

4341
public static List<String> fooSightings = null;

spring-cloud-context/src/test/java/org/springframework/cloud/bootstrap/config/BootstrapConfigurationTests.java

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2020 the original author or authors.
2+
* Copyright 2012-2025 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.
@@ -29,7 +29,6 @@
2929
import org.springframework.boot.WebApplicationType;
3030
import org.springframework.boot.builder.SpringApplicationBuilder;
3131
import org.springframework.boot.context.properties.ConfigurationProperties;
32-
import org.springframework.boot.context.properties.EnableConfigurationProperties;
3332
import org.springframework.boot.context.properties.bind.Bindable;
3433
import org.springframework.boot.context.properties.bind.Binder;
3534
import org.springframework.cloud.bootstrap.BootstrapApplicationListener;
@@ -741,7 +740,6 @@ void activeAndIncludeProfileFromBootstrapPropertySource_WhenMultiplePlacesHaveAc
741740
}
742741

743742
@Configuration(proxyBeanMethods = false)
744-
@EnableConfigurationProperties
745743
protected static class BareConfiguration {
746744

747745
}

spring-cloud-context/src/test/java/org/springframework/cloud/context/properties/ConfigurationPropertiesRebinderIntegrationTests.java

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2020 the original author or authors.
2+
* Copyright 2012-2025 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.
@@ -27,7 +27,6 @@
2727
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
2828
import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration;
2929
import org.springframework.boot.context.properties.ConfigurationProperties;
30-
import org.springframework.boot.context.properties.EnableConfigurationProperties;
3130
import org.springframework.boot.test.context.SpringBootTest;
3231
import org.springframework.boot.test.util.TestPropertyValues;
3332
import org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration;
@@ -115,7 +114,6 @@ interface SomeService {
115114
}
116115

117116
@Configuration(proxyBeanMethods = false)
118-
@EnableConfigurationProperties
119117
@ImportAutoConfiguration({ RefreshConfiguration.RebinderConfiguration.class,
120118
PropertyPlaceholderAutoConfiguration.class })
121119
protected static class TestConfiguration {

spring-cloud-context/src/test/java/org/springframework/cloud/context/properties/ConfigurationPropertiesRebinderLifecycleIntegrationTests.java

+2-8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2020 the original author or authors.
2+
* Copyright 2012-2025 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.
@@ -29,7 +29,6 @@
2929
import org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration;
3030
import org.springframework.cloud.autoconfigure.RefreshAutoConfiguration;
3131
import org.springframework.cloud.context.properties.ConfigurationPropertiesRebinderLifecycleIntegrationTests.TestConfiguration;
32-
import org.springframework.context.annotation.Bean;
3332
import org.springframework.context.annotation.Configuration;
3433
import org.springframework.context.annotation.Import;
3534
import org.springframework.core.env.ConfigurableEnvironment;
@@ -63,15 +62,10 @@ public void testRefresh() {
6362
}
6463

6564
@Configuration(proxyBeanMethods = false)
66-
@EnableConfigurationProperties
65+
@EnableConfigurationProperties(TestProperties.class)
6766
@Import({ RefreshConfiguration.RebinderConfiguration.class, PropertyPlaceholderAutoConfiguration.class })
6867
protected static class TestConfiguration {
6968

70-
@Bean
71-
protected TestProperties properties() {
72-
return new TestProperties();
73-
}
74-
7569
}
7670

7771
// Hack out a protected inner class for testing

spring-cloud-context/src/test/java/org/springframework/cloud/context/properties/ConfigurationPropertiesRebinderListIntegrationTests.java

+2-8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2020 the original author or authors.
2+
* Copyright 2012-2025 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.
@@ -32,7 +32,6 @@
3232
import org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration;
3333
import org.springframework.cloud.autoconfigure.RefreshAutoConfiguration;
3434
import org.springframework.cloud.context.properties.ConfigurationPropertiesRebinderListIntegrationTests.TestConfiguration;
35-
import org.springframework.context.annotation.Bean;
3635
import org.springframework.context.annotation.Configuration;
3736
import org.springframework.context.annotation.Import;
3837
import org.springframework.core.env.ConfigurableEnvironment;
@@ -97,15 +96,10 @@ public void testReplacePropertiesWithCommaSeparated() {
9796
}
9897

9998
@Configuration(proxyBeanMethods = false)
100-
@EnableConfigurationProperties
99+
@EnableConfigurationProperties(TestProperties.class)
101100
@Import({ RefreshConfiguration.RebinderConfiguration.class, PropertyPlaceholderAutoConfiguration.class })
102101
protected static class TestConfiguration {
103102

104-
@Bean
105-
protected TestProperties localTestProperties() {
106-
return new TestProperties();
107-
}
108-
109103
}
110104

111105
// Hack out a protected inner class for testing

spring-cloud-context/src/test/java/org/springframework/cloud/context/properties/ConfigurationPropertiesRebinderProxyIntegrationTests.java

+2-8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2020 the original author or authors.
2+
* Copyright 2012-2025 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.
@@ -33,7 +33,6 @@
3333
import org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration;
3434
import org.springframework.cloud.autoconfigure.RefreshAutoConfiguration;
3535
import org.springframework.cloud.context.properties.ConfigurationPropertiesRebinderProxyIntegrationTests.TestConfiguration;
36-
import org.springframework.context.annotation.Bean;
3736
import org.springframework.context.annotation.Configuration;
3837
import org.springframework.context.annotation.Import;
3938
import org.springframework.core.env.ConfigurableEnvironment;
@@ -65,16 +64,11 @@ public void testAppendProperties() {
6564
}
6665

6766
@Configuration(proxyBeanMethods = false)
68-
@EnableConfigurationProperties
67+
@EnableConfigurationProperties(TestProperties.class)
6968
@Import({ Interceptor.class, RefreshConfiguration.RebinderConfiguration.class,
7069
PropertyPlaceholderAutoConfiguration.class, AopAutoConfiguration.class })
7170
protected static class TestConfiguration {
7271

73-
@Bean
74-
protected TestProperties properties() {
75-
return new TestProperties();
76-
}
77-
7872
}
7973

8074
@Aspect

spring-cloud-context/src/test/java/org/springframework/cloud/context/properties/ConfigurationPropertiesRebinderRefreshScopeIntegrationTests.java

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2020 the original author or authors.
2+
* Copyright 2012-2025 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.
@@ -22,7 +22,6 @@
2222
import org.springframework.beans.factory.annotation.Autowired;
2323
import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration;
2424
import org.springframework.boot.context.properties.ConfigurationProperties;
25-
import org.springframework.boot.context.properties.EnableConfigurationProperties;
2625
import org.springframework.boot.test.context.SpringBootTest;
2726
import org.springframework.boot.test.util.TestPropertyValues;
2827
import org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration;
@@ -83,7 +82,6 @@ public void testRefresh() {
8382
}
8483

8584
@Configuration(proxyBeanMethods = false)
86-
@EnableConfigurationProperties
8785
@Import({ RefreshAutoConfiguration.class, ConfigurationPropertiesRebinderAutoConfiguration.class,
8886
PropertyPlaceholderAutoConfiguration.class })
8987
protected static class TestConfiguration {

spring-cloud-context/src/test/java/org/springframework/cloud/context/scope/refresh/MoreRefreshScopeIntegrationTests.java

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2020 the original author or authors.
2+
* Copyright 2012-2025 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.
@@ -29,7 +29,6 @@
2929
import org.springframework.beans.factory.annotation.Autowired;
3030
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
3131
import org.springframework.boot.context.properties.ConfigurationProperties;
32-
import org.springframework.boot.context.properties.EnableConfigurationProperties;
3332
import org.springframework.boot.test.context.SpringBootTest;
3433
import org.springframework.boot.test.util.TestPropertyValues;
3534
import org.springframework.boot.test.util.TestPropertyValues.Type;
@@ -181,7 +180,6 @@ public void setMessage(String message) {
181180
}
182181

183182
@Configuration
184-
@EnableConfigurationProperties
185183
@EnableAutoConfiguration
186184
protected static class TestConfiguration {
187185

spring-cloud-context/src/test/java/org/springframework/cloud/context/scope/refresh/RefreshScopeConfigurationScaleTests.java

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2020 the original author or authors.
2+
* Copyright 2012-2025 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.
@@ -33,7 +33,6 @@
3333
import org.springframework.beans.factory.annotation.Autowired;
3434
import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration;
3535
import org.springframework.boot.context.properties.ConfigurationProperties;
36-
import org.springframework.boot.context.properties.EnableConfigurationProperties;
3736
import org.springframework.boot.test.context.SpringBootTest;
3837
import org.springframework.boot.test.util.TestPropertyValues;
3938
import org.springframework.cloud.autoconfigure.RefreshAutoConfiguration;
@@ -152,7 +151,6 @@ public void setMessage(String message) {
152151
}
153152

154153
@Configuration(proxyBeanMethods = false)
155-
@EnableConfigurationProperties
156154
@Import({ RefreshAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class })
157155
protected static class TestConfiguration {
158156

spring-cloud-context/src/test/java/org/springframework/cloud/context/scope/refresh/RefreshScopeListBindingIntegrationTests.java

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2020 the original author or authors.
2+
* Copyright 2012-2025 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.
@@ -26,7 +26,6 @@
2626
import org.springframework.beans.factory.annotation.Autowired;
2727
import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration;
2828
import org.springframework.boot.context.properties.ConfigurationProperties;
29-
import org.springframework.boot.context.properties.EnableConfigurationProperties;
3029
import org.springframework.boot.test.context.SpringBootTest;
3130
import org.springframework.boot.test.util.TestPropertyValues;
3231
import org.springframework.cloud.autoconfigure.RefreshAutoConfiguration;
@@ -88,7 +87,6 @@ private Map<String, Object> findTestProperties() {
8887
}
8988

9089
@Configuration(proxyBeanMethods = false)
91-
@EnableConfigurationProperties
9290
@Import({ RefreshAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class })
9391
protected static class TestConfiguration {
9492

0 commit comments

Comments
 (0)