BAEL-2914: Immutable @ConfigurationProperties binding (#8623)

This commit is contained in:
kwoyke 2020-01-29 07:06:48 +01:00 committed by GitHub
parent 22c3abfc3a
commit 577bdf4631
20 changed files with 74 additions and 2 deletions

View File

@ -652,6 +652,7 @@
<module>spring-boot-ctx-fluent</module>
<module>spring-boot-custom-starter</module>
<module>spring-boot-deployment</module>
<module>spring-boot-di</module>
<module>spring-boot-environment</module>
<module>spring-boot-flowable</module>
<!-- <module>spring-boot-gradle</module> --> <!-- Not a maven project -->
@ -1185,6 +1186,7 @@
<module>spring-boot-ctx-fluent</module>
<module>spring-boot-custom-starter</module>
<module>spring-boot-deployment</module>
<module>spring-boot-di</module>
<module>spring-boot-environment</module>
<module>spring-boot-flowable</module>
<!-- <module>spring-boot-gradle</module> --> <!-- Not a maven project -->

View File

@ -12,7 +12,7 @@
<groupId>com.baeldung</groupId>
<artifactId>parent-boot-2</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../../parent-boot-2</relativePath>
<relativePath>../parent-boot-2</relativePath>
</parent>
<dependencies>

View File

@ -16,7 +16,6 @@
<modules>
<module>spring-boot-artifacts</module>
<module>spring-boot-data</module>
<module>spring-boot-di</module>
<module>spring-boot-mvc-birt</module>
<module>spring-boot-properties</module>
<module>spring-boot-testing</module>

View File

@ -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);
}
}

View File

@ -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;
}
}

View File

@ -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");
}
}