Skip to content

Commit f6d8c64

Browse files
committed
Enforce using @ConfigurationProperties("xxx") instead of @ConfigurationProperties(prefix = "xxx")
Signed-off-by: Yanming Zhou <[email protected]>
1 parent 218538e commit f6d8c64

File tree

1 file changed

+38
-2
lines changed

1 file changed

+38
-2
lines changed

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

Lines changed: 38 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.
@@ -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;
@@ -95,7 +96,9 @@ public ArchitectureCheck() {
9596
noClassesShouldLoadResourcesUsingResourceUtils(), noClassesShouldCallStringToUpperCaseWithoutLocale(),
9697
noClassesShouldCallStringToLowerCaseWithoutLocale(),
9798
conditionalOnMissingBeanShouldNotSpecifyOnlyATypeThatIsTheSameAsMethodReturnType(),
98-
enumSourceShouldNotSpecifyOnlyATypeThatIsTheSameAsMethodParameterType());
99+
enumSourceShouldNotSpecifyOnlyATypeThatIsTheSameAsMethodParameterType(),
100+
classLevelConfigurationPropertiesShouldNotSpecifyOnlyPrefixAttribute(),
101+
methodLevelConfigurationPropertiesShouldNotSpecifyOnlyPrefixAttribute());
99102
getRules().addAll(getProhibitObjectsRequireNonNull()
100103
.map((prohibit) -> prohibit ? noClassesShouldCallObjectsRequireNonNull() : Collections.emptyList()));
101104
getRuleDescriptions().set(getRules().map((rules) -> rules.stream().map(ArchRule::getDescription).toList()));
@@ -329,6 +332,39 @@ public void check(JavaMethod item, ConditionEvents events) {
329332
};
330333
}
331334

335+
private ArchRule classLevelConfigurationPropertiesShouldNotSpecifyOnlyPrefixAttribute() {
336+
return ArchRuleDefinition.classes()
337+
.that()
338+
.areAnnotatedWith("org.springframework.boot.context.properties.ConfigurationProperties")
339+
.should(notSpecifyOnlyPrefixAttributeOfConfigurationProperties())
340+
.allowEmptyShould(true);
341+
}
342+
343+
private ArchRule methodLevelConfigurationPropertiesShouldNotSpecifyOnlyPrefixAttribute() {
344+
return ArchRuleDefinition.methods()
345+
.that()
346+
.areAnnotatedWith("org.springframework.boot.context.properties.ConfigurationProperties")
347+
.should(notSpecifyOnlyPrefixAttributeOfConfigurationProperties())
348+
.allowEmptyShould(true);
349+
}
350+
351+
private ArchCondition<? super HasAnnotations<?>> notSpecifyOnlyPrefixAttributeOfConfigurationProperties() {
352+
return new ArchCondition<>("not specify only prefix attribute of @ConfigurationProperties") {
353+
354+
@Override
355+
public void check(HasAnnotations<?> item, ConditionEvents events) {
356+
JavaAnnotation<?> configurationProperties = item
357+
.getAnnotationOfType("org.springframework.boot.context.properties.ConfigurationProperties");
358+
Map<String, Object> properties = configurationProperties.getProperties();
359+
if (properties.size() == 1 && properties.containsKey("prefix")) {
360+
events.add(SimpleConditionEvent.violated(item, configurationProperties.getDescription()
361+
+ " should specify implicit 'value' attribute other than explicit 'prefix' attribute"));
362+
}
363+
}
364+
365+
};
366+
}
367+
332368
public void setClasses(FileCollection classes) {
333369
this.classes = classes;
334370
}

0 commit comments

Comments
 (0)