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 671fb6032db1..e1118ec3d634 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 @@ -64,6 +64,7 @@ * @author Scott Frederick * @author Ivan Malutin * @author Phillip Webb + * @author Ngoc Nhan */ final class ArchitectureRules { @@ -96,6 +97,7 @@ static List standard() { rules.add(classLevelConfigurationPropertiesShouldNotSpecifyOnlyPrefixAttribute()); rules.add(methodLevelConfigurationPropertiesShouldNotSpecifyOnlyPrefixAttribute()); rules.add(conditionsShouldNotBePublic()); + rules.add(allConfigurationPropertiesBindingBeanMethodsShouldBeStatic()); return List.copyOf(rules); } @@ -309,6 +311,14 @@ private static ArchRule conditionsShouldNotBePublic() { .allowEmptyShould(true); } + 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]); } 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 be73b2b392a0..a4066afe5edd 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 @@ -113,7 +113,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 43aea7e4a11a..79ae9792c99d 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 @@ static class ConverterConfiguration { @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 a223cfa1a66e..f8d0910ff6c9 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 @@ -1593,7 +1593,7 @@ static class PersonConverterConfiguration { @Bean @ConfigurationPropertiesBinding - Converter personConverter() { + static Converter personConverter() { return new PersonConverter(); } @@ -1604,7 +1604,7 @@ static class AlienConverterConfiguration { @Bean @ConfigurationPropertiesBinding - Converter alienConverter() { + static Converter alienConverter() { return new AlienConverter(); } @@ -1625,7 +1625,7 @@ static class GenericConverterConfiguration { @Bean @ConfigurationPropertiesBinding - GenericConverter genericPersonConverter() { + static GenericConverter genericPersonConverter() { return new GenericPersonConverter(); } @@ -1636,7 +1636,7 @@ static class FormatterConfiguration { @Bean @ConfigurationPropertiesBinding - Formatter personFormatter() { + static Formatter personFormatter() { return new PersonFormatter(); } @@ -3064,7 +3064,7 @@ static class WithCustomConverterAndObjectToObjectMethodConfiguration { @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 482e81917d47..73b5144ca3fb 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 @@ -129,13 +129,13 @@ static class CustomConverterConfiguration { @Bean @ConfigurationPropertiesBinding - TestConverter testConverter() { + static TestConverter testConverter() { return new TestConverter(); } @Bean @ConfigurationPropertiesBinding - StringConverter stringConverter() { + static StringConverter stringConverter() { return new StringConverter(); } @@ -146,13 +146,13 @@ static class CustomLambdaConverterConfiguration { @Bean @ConfigurationPropertiesBinding - Converter testConverter() { + static Converter testConverter() { return (source) -> new TestConverter().convert(source); } @Bean @ConfigurationPropertiesBinding - Converter stringConverter() { + static Converter stringConverter() { return (source) -> new StringConverter().convert(source); } @@ -163,7 +163,7 @@ static class PrinterConfiguration { @Bean @ConfigurationPropertiesBinding - Printer inputStreamPrinter() { + static Printer inputStreamPrinter() { return (source, locale) -> ThrowingSupplier .of(() -> StreamUtils.copyToString(source, StandardCharsets.UTF_8)) .get();