Skip to content

Commit 62a0e54

Browse files
committed
Merge pull request #43917 from quaff
* pr/43917: Polish 'Refactor `@ConfigurationProperties` that only use `prefix`' Refactor `@ConfigurationProperties` that only use `prefix` Enforce `@ConfigurationProperties` don't use only `prefix` Closes gh-43917
2 parents f8ae1ff + 8ec61b9 commit 62a0e54

File tree

174 files changed

+419
-383
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

174 files changed

+419
-383
lines changed

buildSrc/src/main/java/org/springframework/boot/build/architecture/ArchitectureCheck.java

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import com.tngtech.archunit.core.domain.JavaParameter;
4040
import com.tngtech.archunit.core.domain.JavaType;
4141
import com.tngtech.archunit.core.domain.properties.CanBeAnnotated;
42+
import com.tngtech.archunit.core.domain.properties.HasAnnotations;
4243
import com.tngtech.archunit.core.domain.properties.HasName;
4344
import com.tngtech.archunit.core.domain.properties.HasOwner.Predicates.With;
4445
import com.tngtech.archunit.core.domain.properties.HasParameterTypes;
@@ -97,7 +98,9 @@ public ArchitectureCheck() {
9798
noClassesShouldLoadResourcesUsingResourceUtils(), noClassesShouldCallStringToUpperCaseWithoutLocale(),
9899
noClassesShouldCallStringToLowerCaseWithoutLocale(),
99100
conditionalOnMissingBeanShouldNotSpecifyOnlyATypeThatIsTheSameAsMethodReturnType(),
100-
enumSourceShouldNotSpecifyOnlyATypeThatIsTheSameAsMethodParameterType());
101+
enumSourceShouldNotSpecifyOnlyATypeThatIsTheSameAsMethodParameterType(),
102+
classLevelConfigurationPropertiesShouldNotSpecifyOnlyPrefixAttribute(),
103+
methodLevelConfigurationPropertiesShouldNotSpecifyOnlyPrefixAttribute());
101104
getRules().addAll(getProhibitObjectsRequireNonNull()
102105
.map((prohibit) -> prohibit ? noClassesShouldCallObjectsRequireNonNull() : Collections.emptyList()));
103106
getRuleDescriptions().set(getRules().map((rules) -> rules.stream().map(ArchRule::getDescription).toList()));
@@ -344,6 +347,39 @@ public void check(JavaMethod item, ConditionEvents events) {
344347
};
345348
}
346349

350+
private ArchRule classLevelConfigurationPropertiesShouldNotSpecifyOnlyPrefixAttribute() {
351+
return ArchRuleDefinition.classes()
352+
.that()
353+
.areAnnotatedWith("org.springframework.boot.context.properties.ConfigurationProperties")
354+
.should(notSpecifyOnlyPrefixAttributeOfConfigurationProperties())
355+
.allowEmptyShould(true);
356+
}
357+
358+
private ArchRule methodLevelConfigurationPropertiesShouldNotSpecifyOnlyPrefixAttribute() {
359+
return ArchRuleDefinition.methods()
360+
.that()
361+
.areAnnotatedWith("org.springframework.boot.context.properties.ConfigurationProperties")
362+
.should(notSpecifyOnlyPrefixAttributeOfConfigurationProperties())
363+
.allowEmptyShould(true);
364+
}
365+
366+
private ArchCondition<? super HasAnnotations<?>> notSpecifyOnlyPrefixAttributeOfConfigurationProperties() {
367+
return new ArchCondition<>("not specify only prefix attribute of @ConfigurationProperties") {
368+
369+
@Override
370+
public void check(HasAnnotations<?> item, ConditionEvents events) {
371+
JavaAnnotation<?> configurationProperties = item
372+
.getAnnotationOfType("org.springframework.boot.context.properties.ConfigurationProperties");
373+
Map<String, Object> properties = configurationProperties.getProperties();
374+
if (properties.size() == 1 && properties.containsKey("prefix")) {
375+
events.add(SimpleConditionEvent.violated(item, configurationProperties.getDescription()
376+
+ " should specify implicit 'value' attribute other than explicit 'prefix' attribute"));
377+
}
378+
}
379+
380+
};
381+
}
382+
347383
public void setClasses(FileCollection classes) {
348384
this.classes = classes;
349385
}

spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/endpoint/web/CorsEndpointProperties.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2024 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,7 @@
3333
* @author Andy Wilkinson
3434
* @since 2.0.0
3535
*/
36-
@ConfigurationProperties(prefix = "management.endpoints.web.cors")
36+
@ConfigurationProperties("management.endpoints.web.cors")
3737
public class CorsEndpointProperties {
3838

3939
/**

spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/endpoint/web/WebEndpointProperties.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
* @author Phillip Webb
3333
* @since 2.0.0
3434
*/
35-
@ConfigurationProperties(prefix = "management.endpoints.web")
35+
@ConfigurationProperties("management.endpoints.web")
3636
public class WebEndpointProperties {
3737

3838
private final Exposure exposure = new Exposure();

spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/jdbc/DataSourceHealthIndicatorProperties.java

Lines changed: 2 additions & 2 deletions
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,7 @@
2525
* @author Julio Gomez
2626
* @since 2.4.0
2727
*/
28-
@ConfigurationProperties(prefix = "management.health.db")
28+
@ConfigurationProperties("management.health.db")
2929
public class DataSourceHealthIndicatorProperties {
3030

3131
/**

spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/logging/LogFileWebEndpointProperties.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 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,7 @@
2727
* @author Stephane Nicoll
2828
* @since 2.0.0
2929
*/
30-
@ConfigurationProperties(prefix = "management.endpoint.logfile")
30+
@ConfigurationProperties("management.endpoint.logfile")
3131
public class LogFileWebEndpointProperties {
3232

3333
/**

spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/export/appoptics/AppOpticsProperties.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2022 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.
@@ -28,7 +28,7 @@
2828
* @author Stephane Nicoll
2929
* @since 2.1.0
3030
*/
31-
@ConfigurationProperties(prefix = "management.appoptics.metrics.export")
31+
@ConfigurationProperties("management.appoptics.metrics.export")
3232
public class AppOpticsProperties extends StepRegistryProperties {
3333

3434
/**

spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/export/atlas/AtlasProperties.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2023 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.
@@ -28,7 +28,7 @@
2828
* @author Stephane Nicoll
2929
* @since 2.0.0
3030
*/
31-
@ConfigurationProperties(prefix = "management.atlas.metrics.export")
31+
@ConfigurationProperties("management.atlas.metrics.export")
3232
public class AtlasProperties {
3333

3434
/**

spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/export/datadog/DatadogProperties.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2022 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,7 @@
2727
* @author Stephane Nicoll
2828
* @since 2.0.0
2929
*/
30-
@ConfigurationProperties(prefix = "management.datadog.metrics.export")
30+
@ConfigurationProperties("management.datadog.metrics.export")
3131
public class DatadogProperties extends StepRegistryProperties {
3232

3333
/**

spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/export/dynatrace/DynatraceProperties.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2023 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,7 @@
2929
* @author Georg Pirklbauer
3030
* @since 2.1.0
3131
*/
32-
@ConfigurationProperties(prefix = "management.dynatrace.metrics.export")
32+
@ConfigurationProperties("management.dynatrace.metrics.export")
3333
public class DynatraceProperties extends StepRegistryProperties {
3434

3535
private final V1 v1 = new V1();

spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/export/elastic/ElasticProperties.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2024 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,7 @@
2626
* @author Andy Wilkinson
2727
* @since 2.1.0
2828
*/
29-
@ConfigurationProperties(prefix = "management.elastic.metrics.export")
29+
@ConfigurationProperties("management.elastic.metrics.export")
3030
public class ElasticProperties extends StepRegistryProperties {
3131

3232
/**

0 commit comments

Comments
 (0)