From 380bfa398f2ba9f7afe43e2b7e5a13240a60d7cc Mon Sep 17 00:00:00 2001 From: Tran Ngoc Nhan Date: Fri, 23 May 2025 13:15:59 +0700 Subject: [PATCH] Enforce static @ConfigurationPropertyBinding @Bean methods See gh-45640 Signed-off-by: Tran Ngoc Nhan --- .../boot/build/architecture/ArchitectureRules.java | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/buildSrc/src/main/java/org/springframework/boot/build/architecture/ArchitectureRules.java b/buildSrc/src/main/java/org/springframework/boot/build/architecture/ArchitectureRules.java index 3dc1dae043..a097c2ce2a 100644 --- a/buildSrc/src/main/java/org/springframework/boot/build/architecture/ArchitectureRules.java +++ b/buildSrc/src/main/java/org/springframework/boot/build/architecture/ArchitectureRules.java @@ -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]); }