From ae8efcd3286782e3dbf22782fa62dfebb492cb45 Mon Sep 17 00:00:00 2001 From: vizsoro Date: Thu, 30 Aug 2018 22:08:38 +0200 Subject: [PATCH] editorial review --- .../ConfigPropertiesDemoApplication.java | 4 ++-- .../properties/CustomJsonProperties.java | 4 ++-- .../baeldung/properties/JsonProperties.java | 4 ++-- .../JsonPropertyContextInitializer.java | 2 +- .../properties/JsonPropertySourceFactory.java | 3 ++- .../src/main/resources/configpropscustom.json | 9 --------- .../JsonPropertiesIntegrationTest.java | 18 +++++++++--------- 7 files changed, 18 insertions(+), 26 deletions(-) delete mode 100644 spring-boot/src/main/resources/configpropscustom.json 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 7ca0ab4f60..395d68060b 100644 --- a/spring-boot/src/main/java/org/baeldung/properties/ConfigPropertiesDemoApplication.java +++ b/spring-boot/src/main/java/org/baeldung/properties/ConfigPropertiesDemoApplication.java @@ -1,10 +1,10 @@ package org.baeldung.properties; -import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.context.annotation.ComponentScan; -@EnableAutoConfiguration +@SpringBootApplication @ComponentScan(basePackageClasses = { ConfigProperties.class, JsonProperties.class, CustomJsonProperties.class }) public class ConfigPropertiesDemoApplication { public static void main(String[] args) { diff --git a/spring-boot/src/main/java/org/baeldung/properties/CustomJsonProperties.java b/spring-boot/src/main/java/org/baeldung/properties/CustomJsonProperties.java index 9d5e8ce780..3fae8a8e98 100644 --- a/spring-boot/src/main/java/org/baeldung/properties/CustomJsonProperties.java +++ b/spring-boot/src/main/java/org/baeldung/properties/CustomJsonProperties.java @@ -1,9 +1,9 @@ package org.baeldung.properties; import org.springframework.boot.context.properties.ConfigurationProperties; -import org.springframework.context.annotation.Configuration; +import org.springframework.stereotype.Component; -@Configuration +@Component @ConfigurationProperties(prefix = "custom") public class CustomJsonProperties { diff --git a/spring-boot/src/main/java/org/baeldung/properties/JsonProperties.java b/spring-boot/src/main/java/org/baeldung/properties/JsonProperties.java index 2e6e24036e..5c31cd1344 100644 --- a/spring-boot/src/main/java/org/baeldung/properties/JsonProperties.java +++ b/spring-boot/src/main/java/org/baeldung/properties/JsonProperties.java @@ -4,10 +4,10 @@ import java.util.LinkedHashMap; import java.util.List; import org.springframework.boot.context.properties.ConfigurationProperties; -import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.PropertySource; +import org.springframework.stereotype.Component; -@Configuration +@Component @PropertySource(value = "classpath:configprops.json", factory = JsonPropertySourceFactory.class) @ConfigurationProperties public class JsonProperties { diff --git a/spring-boot/src/main/java/org/baeldung/properties/JsonPropertyContextInitializer.java b/spring-boot/src/main/java/org/baeldung/properties/JsonPropertyContextInitializer.java index 27abadb1e1..fd9b3f35a5 100644 --- a/spring-boot/src/main/java/org/baeldung/properties/JsonPropertyContextInitializer.java +++ b/spring-boot/src/main/java/org/baeldung/properties/JsonPropertyContextInitializer.java @@ -26,7 +26,7 @@ public class JsonPropertyContextInitializer implements ApplicationContextInitial @SuppressWarnings("unchecked") public void initialize(ConfigurableApplicationContext configurableApplicationContext) { try { - Resource resource = configurableApplicationContext.getResource("classpath:configpropscustom.json"); + Resource resource = configurableApplicationContext.getResource("classpath:configprops.json"); Map readValue = new ObjectMapper().readValue(resource.getInputStream(), Map.class); Set set = readValue.entrySet(); List propertySources = convertEntrySet(set, Optional.empty()); diff --git a/spring-boot/src/main/java/org/baeldung/properties/JsonPropertySourceFactory.java b/spring-boot/src/main/java/org/baeldung/properties/JsonPropertySourceFactory.java index 7e5739fd41..9578179519 100644 --- a/spring-boot/src/main/java/org/baeldung/properties/JsonPropertySourceFactory.java +++ b/spring-boot/src/main/java/org/baeldung/properties/JsonPropertySourceFactory.java @@ -4,6 +4,7 @@ import java.io.IOException; import java.util.Map; import org.springframework.core.env.MapPropertySource; +import org.springframework.core.env.PropertySource; import org.springframework.core.io.support.EncodedResource; import org.springframework.core.io.support.PropertySourceFactory; @@ -12,7 +13,7 @@ import com.fasterxml.jackson.databind.ObjectMapper; public class JsonPropertySourceFactory implements PropertySourceFactory { @Override - public org.springframework.core.env.PropertySource createPropertySource(String name, EncodedResource resource) throws IOException { + public PropertySource createPropertySource(String name, EncodedResource resource) throws IOException { Map readValue = new ObjectMapper().readValue(resource.getInputStream(), Map.class); return new MapPropertySource("json-property", readValue); } diff --git a/spring-boot/src/main/resources/configpropscustom.json b/spring-boot/src/main/resources/configpropscustom.json deleted file mode 100644 index 5449876be3..0000000000 --- a/spring-boot/src/main/resources/configpropscustom.json +++ /dev/null @@ -1,9 +0,0 @@ -{ - "host" : "mailer.custom@mail.com", - "port" : 8081, - "resend" : true, - "sender" : { - "name": "sender.custom", - "address": "street.custom" - } -} diff --git a/spring-boot/src/test/java/org/baeldung/properties/JsonPropertiesIntegrationTest.java b/spring-boot/src/test/java/org/baeldung/properties/JsonPropertiesIntegrationTest.java index 20a19e043a..e3d4c62953 100644 --- a/spring-boot/src/test/java/org/baeldung/properties/JsonPropertiesIntegrationTest.java +++ b/spring-boot/src/test/java/org/baeldung/properties/JsonPropertiesIntegrationTest.java @@ -21,19 +21,19 @@ public class JsonPropertiesIntegrationTest { private CustomJsonProperties customJsonProperties; @Test - public void givenJsonPropertySource_whenPropertySourceFactoryUsed_thenLoadFlatValues() { + public void whenPropertiesLoadedViaJsonPropertySource_thenLoadFlatValues() { Assert.assertEquals("mailer@mail.com", jsonProperties.getHost()); Assert.assertEquals(9090, jsonProperties.getPort()); Assert.assertTrue(jsonProperties.isResend()); } @Test - public void givenJsonPropertySource_whenPropertySourceFactoryUsed_thenLoadListValues() { + public void whenPropertiesLoadedViaJsonPropertySource_thenLoadListValues() { Assert.assertThat(jsonProperties.getTopics(), Matchers.is(Arrays.asList("spring", "boot"))); } @Test - public void givenJsonPropertySource_whenPropertySourceFactoryUsed_thenNestedValuesLoadedAsMap() { + public void whenPropertiesLoadedViaJsonPropertySource_thenNestedLoadedAsMap() { Assert.assertEquals("sender", jsonProperties.getSender() .get("name")); Assert.assertEquals("street", jsonProperties.getSender() @@ -41,18 +41,18 @@ public class JsonPropertiesIntegrationTest { } @Test - public void givenCustomJsonPropertySource_whenLoadedIntoEnvironment_thenFlatValuesPopulated() { - Assert.assertEquals("mailer.custom@mail.com", customJsonProperties.getHost()); - Assert.assertEquals(8081, customJsonProperties.getPort()); + public void whenLoadedIntoEnvironment_thenFlatValuesPopulated() { + Assert.assertEquals("mailer@mail.com", customJsonProperties.getHost()); + Assert.assertEquals(9090, customJsonProperties.getPort()); Assert.assertTrue(customJsonProperties.isResend()); } @Test - public void givenCustomJsonPropertySource_whenLoadedIntoEnvironment_thenValuesLoadedIntoClassObject() { + public void whenLoadedIntoEnvironment_thenValuesLoadedIntoClassObject() { Assert.assertNotNull(customJsonProperties.getSender()); - Assert.assertEquals("sender.custom", customJsonProperties.getSender() + Assert.assertEquals("sender", customJsonProperties.getSender() .getName()); - Assert.assertEquals("street.custom", customJsonProperties.getSender() + Assert.assertEquals("street", customJsonProperties.getSender() .getAddress()); }