Enforce static @ConfigurationPropertyBinding @Bean methods

See gh-45640

Signed-off-by: Tran Ngoc Nhan <ngocnhan.tran1996@gmail.com>
This commit is contained in:
Tran Ngoc Nhan
2025-05-23 13:15:59 +07:00
committed by Andy Wilkinson
parent 9a2d8c589e
commit 380bfa398f

View File

@@ -59,6 +59,7 @@ import org.springframework.util.ResourceUtils;
* @author Scott Frederick
* @author Ivan Malutin
* @author Phillip Webb
* @author Ngoc Nhan
*/
final class ArchitectureRules {
@@ -88,6 +89,7 @@ final class ArchitectureRules {
rules.add(noClassesShouldCallStringToLowerCaseWithoutLocale());
rules.add(conditionalOnMissingBeanShouldNotSpecifyOnlyATypeThatIsTheSameAsMethodReturnType());
rules.add(enumSourceShouldNotSpecifyOnlyATypeThatIsTheSameAsMethodParameterType());
rules.add(allConfigurationPropertiesBindingBeanMethodsShouldBeStatic());
return List.copyOf(rules);
}
@@ -230,6 +232,14 @@ final class ArchitectureRules {
}
}
private static ArchRule allConfigurationPropertiesBindingBeanMethodsShouldBeStatic() {
return methodsThatAreAnnotatedWith("org.springframework.context.annotation.Bean").and()
.areAnnotatedWith("org.springframework.boot.context.properties.ConfigurationPropertiesBinding")
.should()
.beStatic()
.allowEmptyShould(true);
}
private static boolean containsOnlySingleType(JavaType[] types, JavaType type) {
return types.length == 1 && type.equals(types[0]);
}