From 380bfa398f2ba9f7afe43e2b7e5a13240a60d7cc Mon Sep 17 00:00:00 2001 From: Tran Ngoc Nhan Date: Fri, 23 May 2025 13:15:59 +0700 Subject: [PATCH 1/2] 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 3dc1dae043f..a097c2ce2a2 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]); } From c9bf87be7da7b47e29d3bcd72b884ada7a505f41 Mon Sep 17 00:00:00 2001 From: Tran Ngoc Nhan Date: Fri, 23 May 2025 01:17:06 +0700 Subject: [PATCH 2/2] Make all @ConfigurationPropertiesBinding @Bean methods static See gh-45640 Signed-off-by: Tran Ngoc Nhan --- .../autoconfigure/flyway/FlywayAutoConfiguration.java | 2 +- .../servlet/SecurityAutoConfigurationTests.java | 2 +- .../properties/ConfigurationPropertiesTests.java | 10 +++++----- .../properties/ConversionServiceDeducerTests.java | 4 ++-- 4 files changed, 9 insertions(+), 9 deletions(-) diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/flyway/FlywayAutoConfiguration.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/flyway/FlywayAutoConfiguration.java index bf20e885d9b..90776bd26bc 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/flyway/FlywayAutoConfiguration.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/flyway/FlywayAutoConfiguration.java @@ -112,7 +112,7 @@ public class FlywayAutoConfiguration { @Bean @ConfigurationPropertiesBinding - public StringOrNumberToMigrationVersionConverter stringOrNumberMigrationVersionConverter() { + public static StringOrNumberToMigrationVersionConverter stringOrNumberMigrationVersionConverter() { return new StringOrNumberToMigrationVersionConverter(); } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/security/servlet/SecurityAutoConfigurationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/security/servlet/SecurityAutoConfigurationTests.java index 43aea7e4a11..79ae9792c99 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/security/servlet/SecurityAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/security/servlet/SecurityAutoConfigurationTests.java @@ -264,7 +264,7 @@ class SecurityAutoConfigurationTests { @Bean @ConfigurationPropertiesBinding - Converter targetTypeConverter() { + static Converter targetTypeConverter() { return new Converter<>() { @Override diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/ConfigurationPropertiesTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/ConfigurationPropertiesTests.java index f666137e211..9ff182223f4 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/ConfigurationPropertiesTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/ConfigurationPropertiesTests.java @@ -1560,7 +1560,7 @@ class ConfigurationPropertiesTests { @Bean @ConfigurationPropertiesBinding - Converter personConverter() { + static Converter personConverter() { return new PersonConverter(); } @@ -1571,7 +1571,7 @@ class ConfigurationPropertiesTests { @Bean @ConfigurationPropertiesBinding - Converter alienConverter() { + static Converter alienConverter() { return new AlienConverter(); } @@ -1592,7 +1592,7 @@ class ConfigurationPropertiesTests { @Bean @ConfigurationPropertiesBinding - GenericConverter genericPersonConverter() { + static GenericConverter genericPersonConverter() { return new GenericPersonConverter(); } @@ -1603,7 +1603,7 @@ class ConfigurationPropertiesTests { @Bean @ConfigurationPropertiesBinding - Formatter personFormatter() { + static Formatter personFormatter() { return new PersonFormatter(); } @@ -3031,7 +3031,7 @@ class ConfigurationPropertiesTests { @Bean @ConfigurationPropertiesBinding - WithObjectToObjectMethodConverter withObjectToObjectMethodConverter() { + static WithObjectToObjectMethodConverter withObjectToObjectMethodConverter() { return new WithObjectToObjectMethodConverter(); } diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/ConversionServiceDeducerTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/ConversionServiceDeducerTests.java index 0027cb1d7cf..e861a8011d6 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/ConversionServiceDeducerTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/context/properties/ConversionServiceDeducerTests.java @@ -102,13 +102,13 @@ class ConversionServiceDeducerTests { @Bean @ConfigurationPropertiesBinding - TestConverter testConverter() { + static TestConverter testConverter() { return new TestConverter(); } @Bean @ConfigurationPropertiesBinding - StringConverter stringConverter() { + static StringConverter stringConverter() { return new StringConverter(); }