From 4d17d264052836ad1892a6b36432f98e0a37122d Mon Sep 17 00:00:00 2001 From: Chandravadan S Date: Mon, 24 Apr 2017 12:08:53 +0530 Subject: [PATCH] Bael 697 - @ConfigurationProperties in Spring Boot (#1712) * Initial commit for @ConfigurationProperties (BAEL-697) * Additional validations added (BAEL-697) * Formatting changes * Updated main properties file * Minor changes to pass TravisCI (BAEL-697) * Optimisation of imports (BAEL-697) * Renamed ConfigPropertiesTest to Integration test (BAEL-697) --- .../baeldung/properties/ConfigProperties.java | 117 ++++++++++++++++++ .../ConfigPropertiesDemoApplication.java | 15 +++ .../src/main/resources/configprops.properties | 20 +++ .../ConfigPropertiesIntegrationTest.java | 52 ++++++++ .../resources/configprops-test.properties | 19 +++ 5 files changed, 223 insertions(+) create mode 100644 spring-boot/src/main/java/org/baeldung/properties/ConfigProperties.java create mode 100644 spring-boot/src/main/java/org/baeldung/properties/ConfigPropertiesDemoApplication.java create mode 100644 spring-boot/src/main/resources/configprops.properties create mode 100644 spring-boot/src/test/java/org/baeldung/properties/ConfigPropertiesIntegrationTest.java create mode 100644 spring-boot/src/test/resources/configprops-test.properties diff --git a/spring-boot/src/main/java/org/baeldung/properties/ConfigProperties.java b/spring-boot/src/main/java/org/baeldung/properties/ConfigProperties.java new file mode 100644 index 0000000000..2c815fe998 --- /dev/null +++ b/spring-boot/src/main/java/org/baeldung/properties/ConfigProperties.java @@ -0,0 +1,117 @@ +package org.baeldung.properties; + +import java.util.List; +import java.util.Map; + +import javax.validation.constraints.Max; +import javax.validation.constraints.Min; +import javax.validation.constraints.Pattern; + +import org.hibernate.validator.constraints.Length; +import org.hibernate.validator.constraints.NotBlank; +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.PropertySource; +import org.springframework.validation.annotation.Validated; + +@Configuration +@PropertySource("classpath:configprops.properties") +@ConfigurationProperties(prefix = "mail") +@Validated +public class ConfigProperties { + + @Validated + public static class Credentials { + + @Length(max = 4, min = 1) + private String authMethod; + private String username; + private String password; + + public String getAuthMethod() { + return authMethod; + } + + public void setAuthMethod(String authMethod) { + this.authMethod = authMethod; + } + + public String getUsername() { + return username; + } + + public void setUsername(String username) { + this.username = username; + } + + public String getPassword() { + return password; + } + + public void setPassword(String password) { + this.password = password; + } + } + + @NotBlank + private String host; + + @Min(1025) + @Max(65536) + private int port; + + @Pattern(regexp = "^[a-z0-9._%+-]+@[a-z0-9.-]+\\.[a-z]{2,6}$") + private String from; + + private Credentials credentials; + private List defaultRecipients; + private Map additionalHeaders; + + public String getHost() { + return host; + } + + public void setHost(String host) { + this.host = host; + } + + public int getPort() { + return port; + } + + public void setPort(int port) { + this.port = port; + } + + public String getFrom() { + return from; + } + + public void setFrom(String from) { + this.from = from; + } + + public Credentials getCredentials() { + return credentials; + } + + public void setCredentials(Credentials credentials) { + this.credentials = credentials; + } + + public List getDefaultRecipients() { + return defaultRecipients; + } + + public void setDefaultRecipients(List defaultRecipients) { + this.defaultRecipients = defaultRecipients; + } + + public Map getAdditionalHeaders() { + return additionalHeaders; + } + + public void setAdditionalHeaders(Map additionalHeaders) { + this.additionalHeaders = additionalHeaders; + } +} diff --git a/spring-boot/src/main/java/org/baeldung/properties/ConfigPropertiesDemoApplication.java b/spring-boot/src/main/java/org/baeldung/properties/ConfigPropertiesDemoApplication.java new file mode 100644 index 0000000000..8ebda17f7d --- /dev/null +++ b/spring-boot/src/main/java/org/baeldung/properties/ConfigPropertiesDemoApplication.java @@ -0,0 +1,15 @@ +package org.baeldung.properties; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.context.annotation.ComponentScan; + +import com.baeldung.autoconfiguration.MySQLAutoconfiguration; + +@EnableAutoConfiguration(exclude = MySQLAutoconfiguration.class) +@ComponentScan(basePackageClasses = ConfigProperties.class) +public class ConfigPropertiesDemoApplication { + public static void main(String[] args) { + SpringApplication.run(ConfigPropertiesDemoApplication.class); + } +} diff --git a/spring-boot/src/main/resources/configprops.properties b/spring-boot/src/main/resources/configprops.properties new file mode 100644 index 0000000000..e5d9ae621d --- /dev/null +++ b/spring-boot/src/main/resources/configprops.properties @@ -0,0 +1,20 @@ +#Simple properties +mail.host=mailer@mail.com +mail.port=9000 +mail.from=mailer@mail.com + +#List properties +mail.defaultRecipients[0]=admin@mail.com +mail.defaultRecipients[1]=owner@mail.com + +#Map Properties +mail.additionalHeaders.redelivery=true +mail.additionalHeaders.secure=true +mail.additionalHeaders.p3=value + +#Object properties +mail.credentials.username=john +mail.credentials.password=password +mail.credentials.authMethod=SHA1 + + diff --git a/spring-boot/src/test/java/org/baeldung/properties/ConfigPropertiesIntegrationTest.java b/spring-boot/src/test/java/org/baeldung/properties/ConfigPropertiesIntegrationTest.java new file mode 100644 index 0000000000..ffd5bf55d6 --- /dev/null +++ b/spring-boot/src/test/java/org/baeldung/properties/ConfigPropertiesIntegrationTest.java @@ -0,0 +1,52 @@ +package org.baeldung.properties; + +import org.junit.Assert; +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; + +@RunWith(SpringRunner.class) +@SpringBootTest(classes = ConfigPropertiesDemoApplication.class) +@TestPropertySource("classpath:configprops-test.properties") +public class ConfigPropertiesIntegrationTest { + + @Autowired + private ConfigProperties properties; + + @Test + public void whenSimplePropertyQueriedthenReturnsProperty() throws Exception { + Assert.assertTrue("From address is read as null!", properties.getFrom() != null); + } + + @Test + public void whenListPropertyQueriedthenReturnsProperty() throws Exception { + Assert.assertTrue("Couldn't bind list property!", properties.getDefaultRecipients() + .size() == 2); + Assert.assertTrue("Incorrectly bound list property. Expected 2 entries!", properties.getDefaultRecipients() + .size() == 2); + } + + @Test + public void whenMapPropertyQueriedthenReturnsProperty() throws Exception { + Assert.assertTrue("Couldn't bind map property!", properties.getAdditionalHeaders() != null); + Assert.assertTrue("Incorrectly bound map property. Expected 3 Entries!", properties.getAdditionalHeaders() + .size() == 3); + } + + @Test + public void whenObjectPropertyQueriedthenReturnsProperty() throws Exception { + Assert.assertTrue("Couldn't bind map property!", properties.getCredentials() != null); + Assert.assertTrue("Incorrectly bound object property!", properties.getCredentials() + .getAuthMethod() + .equals("SHA1")); + Assert.assertTrue("Incorrectly bound object property!", properties.getCredentials() + .getUsername() + .equals("john")); + Assert.assertTrue("Incorrectly bound object property!", properties.getCredentials() + .getPassword() + .equals("password")); + } +} diff --git a/spring-boot/src/test/resources/configprops-test.properties b/spring-boot/src/test/resources/configprops-test.properties new file mode 100644 index 0000000000..b27cf2107a --- /dev/null +++ b/spring-boot/src/test/resources/configprops-test.properties @@ -0,0 +1,19 @@ +#Simple properties +mail.host=mailer@mail.com +mail.port=9000 +mail.from=mailer@mail.com + +#List properties +mail.defaultRecipients[0]=admin@mail.com +mail.defaultRecipients[1]=owner@mail.com + +#Map Properties +mail.additionalHeaders.redelivery=true +mail.additionalHeaders.secure=true +mail.additionalHeaders.p3=value + +#Object properties +mail.credentials.username=john +mail.credentials.password=password +mail.credentials.authMethod=SHA1 +