diff --git a/spring-boot/src/main/java/org/baeldung/properties/AdditionalConfiguration.java b/spring-boot/src/main/java/org/baeldung/properties/AdditionalConfiguration.java new file mode 100644 index 0000000000..499666c143 --- /dev/null +++ b/spring-boot/src/main/java/org/baeldung/properties/AdditionalConfiguration.java @@ -0,0 +1,14 @@ +package org.baeldung.properties; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.context.properties.EnableConfigurationProperties; +import org.springframework.context.annotation.Configuration; + +@Configuration +@EnableConfigurationProperties(AdditionalProperties.class) +public class AdditionalConfiguration { + + @Autowired + private AdditionalProperties additionalProperties; + +} diff --git a/spring-boot/src/main/java/org/baeldung/properties/AdditionalProperties.java b/spring-boot/src/main/java/org/baeldung/properties/AdditionalProperties.java new file mode 100644 index 0000000000..64e39b1475 --- /dev/null +++ b/spring-boot/src/main/java/org/baeldung/properties/AdditionalProperties.java @@ -0,0 +1,26 @@ +package org.baeldung.properties; + +import org.springframework.boot.context.properties.ConfigurationProperties; + +@ConfigurationProperties(prefix = "additional") +public class AdditionalProperties { + + private String unit; + private int max; + + public String getUnit() { + return unit; + } + + public void setUnit(String unit) { + this.unit = unit; + } + + public int getMax() { + return max; + } + + public void setMax(int max) { + this.max = max; + } +} diff --git a/spring-boot/src/main/java/org/baeldung/properties/ConfigPropertiesDemoApplication.java b/spring-boot/src/main/java/org/baeldung/properties/ConfigPropertiesDemoApplication.java index 395d68060b..7b8f2c3411 100644 --- a/spring-boot/src/main/java/org/baeldung/properties/ConfigPropertiesDemoApplication.java +++ b/spring-boot/src/main/java/org/baeldung/properties/ConfigPropertiesDemoApplication.java @@ -5,11 +5,14 @@ import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.context.annotation.ComponentScan; @SpringBootApplication -@ComponentScan(basePackageClasses = { ConfigProperties.class, JsonProperties.class, CustomJsonProperties.class }) +@ComponentScan(basePackageClasses = {ConfigProperties.class, + JsonProperties.class, + CustomJsonProperties.class, + AdditionalConfiguration.class}) public class ConfigPropertiesDemoApplication { public static void main(String[] args) { new SpringApplicationBuilder(ConfigPropertiesDemoApplication.class).initializers(new JsonPropertyContextInitializer()) - .run(); + .run(); } } diff --git a/spring-boot/src/test/java/com/baeldung/properties/ConfigPropertiesIntegrationTest.java b/spring-boot/src/test/java/com/baeldung/properties/ConfigPropertiesIntegrationTest.java index 3a4b6551b1..8f07b2da35 100644 --- a/spring-boot/src/test/java/com/baeldung/properties/ConfigPropertiesIntegrationTest.java +++ b/spring-boot/src/test/java/com/baeldung/properties/ConfigPropertiesIntegrationTest.java @@ -1,5 +1,6 @@ package com.baeldung.properties; +import org.baeldung.properties.AdditionalProperties; import org.baeldung.properties.ConfigProperties; import org.baeldung.properties.ConfigPropertiesDemoApplication; import org.junit.Assert; @@ -18,6 +19,9 @@ public class ConfigPropertiesIntegrationTest { @Autowired private ConfigProperties properties; + @Autowired + private AdditionalProperties additionalProperties; + @Test public void whenSimplePropertyQueriedthenReturnsProperty() throws Exception { Assert.assertTrue("From address is read as null!", properties.getFrom() != null); @@ -42,4 +46,10 @@ public class ConfigPropertiesIntegrationTest { Assert.assertTrue("Incorrectly bound object property!", properties.getCredentials().getUsername().equals("john")); Assert.assertTrue("Incorrectly bound object property!", properties.getCredentials().getPassword().equals("password")); } + + @Test + public void whenAdditionalPropertyQueriedthenReturnsProperty() { + Assert.assertTrue(additionalProperties.getUnit().equals("km")); + Assert.assertTrue(additionalProperties.getMax() == 100); + } } diff --git a/spring-boot/src/test/resources/configprops-test.properties b/spring-boot/src/test/resources/configprops-test.properties index ea11f2159e..5eed93a22b 100644 --- a/spring-boot/src/test/resources/configprops-test.properties +++ b/spring-boot/src/test/resources/configprops-test.properties @@ -20,3 +20,7 @@ mail.credentials.authMethod=SHA1 #Bean method properties item.name=Test item name item.size=21 + +#Additional properties +additional.unit=km +additional.max=100