From 577bdf4631d92225b11e88e68f4f01ecedb5338a Mon Sep 17 00:00:00 2001 From: kwoyke Date: Wed, 29 Jan 2020 07:06:48 +0100 Subject: [PATCH] BAEL-2914: Immutable @ConfigurationProperties binding (#8623) --- pom.xml | 2 ++ .../README.MD | 0 .../spring-boot-di => spring-boot-di}/pom.xml | 2 +- .../com/baeldung/SpringBootDiApplication.java | 0 .../baeldung/componentscan/ExampleBean.java | 0 .../filter/annotation/Animal.java | 0 .../ComponentScanAnnotationFilterApp.java | 0 .../filter/annotation/Elephant.java | 0 .../springapp/SpringComponentScanApp.java | 0 .../componentscan/springapp/animals/Cat.java | 0 .../componentscan/springapp/animals/Dog.java | 0 .../componentscan/springapp/flowers/Rose.java | 0 .../SpringBootComponentScanApp.java | 0 .../springbootapp/animals/Cat.java | 0 .../springbootapp/animals/Dog.java | 0 .../springbootapp/flowers/Rose.java | 0 spring-boot-modules/pom.xml | 1 - .../ImmutableConfigPropertiesApp.java | 14 +++++++++ .../ImmutableCredentials.java | 31 +++++++++++++++++++ ...onfigurationPropertiesIntegrationTest.java | 26 ++++++++++++++++ 20 files changed, 74 insertions(+), 2 deletions(-) rename {spring-boot-modules/spring-boot-di => spring-boot-di}/README.MD (100%) rename {spring-boot-modules/spring-boot-di => spring-boot-di}/pom.xml (97%) rename {spring-boot-modules/spring-boot-di => spring-boot-di}/src/main/java/com/baeldung/SpringBootDiApplication.java (100%) rename {spring-boot-modules/spring-boot-di => spring-boot-di}/src/main/java/com/baeldung/componentscan/ExampleBean.java (100%) rename {spring-boot-modules/spring-boot-di => spring-boot-di}/src/main/java/com/baeldung/componentscan/filter/annotation/Animal.java (100%) rename {spring-boot-modules/spring-boot-di => spring-boot-di}/src/main/java/com/baeldung/componentscan/filter/annotation/ComponentScanAnnotationFilterApp.java (100%) rename {spring-boot-modules/spring-boot-di => spring-boot-di}/src/main/java/com/baeldung/componentscan/filter/annotation/Elephant.java (100%) rename {spring-boot-modules/spring-boot-di => spring-boot-di}/src/main/java/com/baeldung/componentscan/springapp/SpringComponentScanApp.java (100%) rename {spring-boot-modules/spring-boot-di => spring-boot-di}/src/main/java/com/baeldung/componentscan/springapp/animals/Cat.java (100%) rename {spring-boot-modules/spring-boot-di => spring-boot-di}/src/main/java/com/baeldung/componentscan/springapp/animals/Dog.java (100%) rename {spring-boot-modules/spring-boot-di => spring-boot-di}/src/main/java/com/baeldung/componentscan/springapp/flowers/Rose.java (100%) rename {spring-boot-modules/spring-boot-di => spring-boot-di}/src/main/java/com/baeldung/componentscan/springbootapp/SpringBootComponentScanApp.java (100%) rename {spring-boot-modules/spring-boot-di => spring-boot-di}/src/main/java/com/baeldung/componentscan/springbootapp/animals/Cat.java (100%) rename {spring-boot-modules/spring-boot-di => spring-boot-di}/src/main/java/com/baeldung/componentscan/springbootapp/animals/Dog.java (100%) rename {spring-boot-modules/spring-boot-di => spring-boot-di}/src/main/java/com/baeldung/componentscan/springbootapp/flowers/Rose.java (100%) create mode 100644 spring-boot-modules/spring-boot-properties/src/main/java/com/baeldung/configurationproperties/ImmutableConfigPropertiesApp.java create mode 100644 spring-boot-modules/spring-boot-properties/src/main/java/com/baeldung/configurationproperties/ImmutableCredentials.java create mode 100644 spring-boot-modules/spring-boot-properties/src/test/java/com/baeldung/configurationproperties/ImmutableConfigurationPropertiesIntegrationTest.java diff --git a/pom.xml b/pom.xml index 95a3cb9e72..c189071b61 100644 --- a/pom.xml +++ b/pom.xml @@ -652,6 +652,7 @@ spring-boot-ctx-fluent spring-boot-custom-starter spring-boot-deployment + spring-boot-di spring-boot-environment spring-boot-flowable @@ -1185,6 +1186,7 @@ spring-boot-ctx-fluent spring-boot-custom-starter spring-boot-deployment + spring-boot-di spring-boot-environment spring-boot-flowable diff --git a/spring-boot-modules/spring-boot-di/README.MD b/spring-boot-di/README.MD similarity index 100% rename from spring-boot-modules/spring-boot-di/README.MD rename to spring-boot-di/README.MD diff --git a/spring-boot-modules/spring-boot-di/pom.xml b/spring-boot-di/pom.xml similarity index 97% rename from spring-boot-modules/spring-boot-di/pom.xml rename to spring-boot-di/pom.xml index b24e4a9037..61059630c4 100644 --- a/spring-boot-modules/spring-boot-di/pom.xml +++ b/spring-boot-di/pom.xml @@ -12,7 +12,7 @@ com.baeldung parent-boot-2 0.0.1-SNAPSHOT - ../../parent-boot-2 + ../parent-boot-2 diff --git a/spring-boot-modules/spring-boot-di/src/main/java/com/baeldung/SpringBootDiApplication.java b/spring-boot-di/src/main/java/com/baeldung/SpringBootDiApplication.java similarity index 100% rename from spring-boot-modules/spring-boot-di/src/main/java/com/baeldung/SpringBootDiApplication.java rename to spring-boot-di/src/main/java/com/baeldung/SpringBootDiApplication.java diff --git a/spring-boot-modules/spring-boot-di/src/main/java/com/baeldung/componentscan/ExampleBean.java b/spring-boot-di/src/main/java/com/baeldung/componentscan/ExampleBean.java similarity index 100% rename from spring-boot-modules/spring-boot-di/src/main/java/com/baeldung/componentscan/ExampleBean.java rename to spring-boot-di/src/main/java/com/baeldung/componentscan/ExampleBean.java diff --git a/spring-boot-modules/spring-boot-di/src/main/java/com/baeldung/componentscan/filter/annotation/Animal.java b/spring-boot-di/src/main/java/com/baeldung/componentscan/filter/annotation/Animal.java similarity index 100% rename from spring-boot-modules/spring-boot-di/src/main/java/com/baeldung/componentscan/filter/annotation/Animal.java rename to spring-boot-di/src/main/java/com/baeldung/componentscan/filter/annotation/Animal.java diff --git a/spring-boot-modules/spring-boot-di/src/main/java/com/baeldung/componentscan/filter/annotation/ComponentScanAnnotationFilterApp.java b/spring-boot-di/src/main/java/com/baeldung/componentscan/filter/annotation/ComponentScanAnnotationFilterApp.java similarity index 100% rename from spring-boot-modules/spring-boot-di/src/main/java/com/baeldung/componentscan/filter/annotation/ComponentScanAnnotationFilterApp.java rename to spring-boot-di/src/main/java/com/baeldung/componentscan/filter/annotation/ComponentScanAnnotationFilterApp.java diff --git a/spring-boot-modules/spring-boot-di/src/main/java/com/baeldung/componentscan/filter/annotation/Elephant.java b/spring-boot-di/src/main/java/com/baeldung/componentscan/filter/annotation/Elephant.java similarity index 100% rename from spring-boot-modules/spring-boot-di/src/main/java/com/baeldung/componentscan/filter/annotation/Elephant.java rename to spring-boot-di/src/main/java/com/baeldung/componentscan/filter/annotation/Elephant.java diff --git a/spring-boot-modules/spring-boot-di/src/main/java/com/baeldung/componentscan/springapp/SpringComponentScanApp.java b/spring-boot-di/src/main/java/com/baeldung/componentscan/springapp/SpringComponentScanApp.java similarity index 100% rename from spring-boot-modules/spring-boot-di/src/main/java/com/baeldung/componentscan/springapp/SpringComponentScanApp.java rename to spring-boot-di/src/main/java/com/baeldung/componentscan/springapp/SpringComponentScanApp.java diff --git a/spring-boot-modules/spring-boot-di/src/main/java/com/baeldung/componentscan/springapp/animals/Cat.java b/spring-boot-di/src/main/java/com/baeldung/componentscan/springapp/animals/Cat.java similarity index 100% rename from spring-boot-modules/spring-boot-di/src/main/java/com/baeldung/componentscan/springapp/animals/Cat.java rename to spring-boot-di/src/main/java/com/baeldung/componentscan/springapp/animals/Cat.java diff --git a/spring-boot-modules/spring-boot-di/src/main/java/com/baeldung/componentscan/springapp/animals/Dog.java b/spring-boot-di/src/main/java/com/baeldung/componentscan/springapp/animals/Dog.java similarity index 100% rename from spring-boot-modules/spring-boot-di/src/main/java/com/baeldung/componentscan/springapp/animals/Dog.java rename to spring-boot-di/src/main/java/com/baeldung/componentscan/springapp/animals/Dog.java diff --git a/spring-boot-modules/spring-boot-di/src/main/java/com/baeldung/componentscan/springapp/flowers/Rose.java b/spring-boot-di/src/main/java/com/baeldung/componentscan/springapp/flowers/Rose.java similarity index 100% rename from spring-boot-modules/spring-boot-di/src/main/java/com/baeldung/componentscan/springapp/flowers/Rose.java rename to spring-boot-di/src/main/java/com/baeldung/componentscan/springapp/flowers/Rose.java diff --git a/spring-boot-modules/spring-boot-di/src/main/java/com/baeldung/componentscan/springbootapp/SpringBootComponentScanApp.java b/spring-boot-di/src/main/java/com/baeldung/componentscan/springbootapp/SpringBootComponentScanApp.java similarity index 100% rename from spring-boot-modules/spring-boot-di/src/main/java/com/baeldung/componentscan/springbootapp/SpringBootComponentScanApp.java rename to spring-boot-di/src/main/java/com/baeldung/componentscan/springbootapp/SpringBootComponentScanApp.java diff --git a/spring-boot-modules/spring-boot-di/src/main/java/com/baeldung/componentscan/springbootapp/animals/Cat.java b/spring-boot-di/src/main/java/com/baeldung/componentscan/springbootapp/animals/Cat.java similarity index 100% rename from spring-boot-modules/spring-boot-di/src/main/java/com/baeldung/componentscan/springbootapp/animals/Cat.java rename to spring-boot-di/src/main/java/com/baeldung/componentscan/springbootapp/animals/Cat.java diff --git a/spring-boot-modules/spring-boot-di/src/main/java/com/baeldung/componentscan/springbootapp/animals/Dog.java b/spring-boot-di/src/main/java/com/baeldung/componentscan/springbootapp/animals/Dog.java similarity index 100% rename from spring-boot-modules/spring-boot-di/src/main/java/com/baeldung/componentscan/springbootapp/animals/Dog.java rename to spring-boot-di/src/main/java/com/baeldung/componentscan/springbootapp/animals/Dog.java diff --git a/spring-boot-modules/spring-boot-di/src/main/java/com/baeldung/componentscan/springbootapp/flowers/Rose.java b/spring-boot-di/src/main/java/com/baeldung/componentscan/springbootapp/flowers/Rose.java similarity index 100% rename from spring-boot-modules/spring-boot-di/src/main/java/com/baeldung/componentscan/springbootapp/flowers/Rose.java rename to spring-boot-di/src/main/java/com/baeldung/componentscan/springbootapp/flowers/Rose.java diff --git a/spring-boot-modules/pom.xml b/spring-boot-modules/pom.xml index 490ecb31f9..56fc9d7765 100644 --- a/spring-boot-modules/pom.xml +++ b/spring-boot-modules/pom.xml @@ -16,7 +16,6 @@ spring-boot-artifacts spring-boot-data - spring-boot-di spring-boot-mvc-birt spring-boot-properties spring-boot-testing diff --git a/spring-boot-modules/spring-boot-properties/src/main/java/com/baeldung/configurationproperties/ImmutableConfigPropertiesApp.java b/spring-boot-modules/spring-boot-properties/src/main/java/com/baeldung/configurationproperties/ImmutableConfigPropertiesApp.java new file mode 100644 index 0000000000..b9d84238c3 --- /dev/null +++ b/spring-boot-modules/spring-boot-properties/src/main/java/com/baeldung/configurationproperties/ImmutableConfigPropertiesApp.java @@ -0,0 +1,14 @@ +package com.baeldung.configurationproperties; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.context.properties.ConfigurationPropertiesScan; + +@SpringBootApplication +@ConfigurationPropertiesScan +public class ImmutableConfigPropertiesApp { + + public static void main(String[] args) { + SpringApplication.run(ImmutableConfigPropertiesApp.class, args); + } +} diff --git a/spring-boot-modules/spring-boot-properties/src/main/java/com/baeldung/configurationproperties/ImmutableCredentials.java b/spring-boot-modules/spring-boot-properties/src/main/java/com/baeldung/configurationproperties/ImmutableCredentials.java new file mode 100644 index 0000000000..a58e4143e5 --- /dev/null +++ b/spring-boot-modules/spring-boot-properties/src/main/java/com/baeldung/configurationproperties/ImmutableCredentials.java @@ -0,0 +1,31 @@ +package com.baeldung.configurationproperties; + +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.boot.context.properties.ConstructorBinding; + +@ConfigurationProperties(prefix = "mail.credentials") +@ConstructorBinding +public class ImmutableCredentials { + + private final String authMethod; + private final String username; + private final String password; + + public ImmutableCredentials(String authMethod, String username, String password) { + this.authMethod = authMethod; + this.username = username; + this.password = password; + } + + public String getAuthMethod() { + return authMethod; + } + + public String getUsername() { + return username; + } + + public String getPassword() { + return password; + } +} diff --git a/spring-boot-modules/spring-boot-properties/src/test/java/com/baeldung/configurationproperties/ImmutableConfigurationPropertiesIntegrationTest.java b/spring-boot-modules/spring-boot-properties/src/test/java/com/baeldung/configurationproperties/ImmutableConfigurationPropertiesIntegrationTest.java new file mode 100644 index 0000000000..a45932f7be --- /dev/null +++ b/spring-boot-modules/spring-boot-properties/src/test/java/com/baeldung/configurationproperties/ImmutableConfigurationPropertiesIntegrationTest.java @@ -0,0 +1,26 @@ +package com.baeldung.configurationproperties; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.TestPropertySource; +import org.springframework.test.context.junit4.SpringRunner; + +import static org.assertj.core.api.Assertions.assertThat; + +@RunWith(SpringRunner.class) +@SpringBootTest(classes = ImmutableConfigPropertiesApp.class) +@TestPropertySource("classpath:configprops-test.properties") +public class ImmutableConfigurationPropertiesIntegrationTest { + + @Autowired + private ImmutableCredentials immutableCredentials; + + @Test + public void whenConstructorBindingUsed_thenPropertiesCorrectlyBound() { + assertThat(immutableCredentials.getAuthMethod()).isEqualTo("SHA1"); + assertThat(immutableCredentials.getUsername()).isEqualTo("john"); + assertThat(immutableCredentials.getPassword()).isEqualTo("password"); + } +}