editorial review
This commit is contained in:
parent
9ab810aba1
commit
ae8efcd328
|
@ -1,10 +1,10 @@
|
||||||
package org.baeldung.properties;
|
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.boot.builder.SpringApplicationBuilder;
|
||||||
import org.springframework.context.annotation.ComponentScan;
|
import org.springframework.context.annotation.ComponentScan;
|
||||||
|
|
||||||
@EnableAutoConfiguration
|
@SpringBootApplication
|
||||||
@ComponentScan(basePackageClasses = { ConfigProperties.class, JsonProperties.class, CustomJsonProperties.class })
|
@ComponentScan(basePackageClasses = { ConfigProperties.class, JsonProperties.class, CustomJsonProperties.class })
|
||||||
public class ConfigPropertiesDemoApplication {
|
public class ConfigPropertiesDemoApplication {
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
package org.baeldung.properties;
|
package org.baeldung.properties;
|
||||||
|
|
||||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
@Configuration
|
@Component
|
||||||
@ConfigurationProperties(prefix = "custom")
|
@ConfigurationProperties(prefix = "custom")
|
||||||
public class CustomJsonProperties {
|
public class CustomJsonProperties {
|
||||||
|
|
||||||
|
|
|
@ -4,10 +4,10 @@ import java.util.LinkedHashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||||
import org.springframework.context.annotation.Configuration;
|
|
||||||
import org.springframework.context.annotation.PropertySource;
|
import org.springframework.context.annotation.PropertySource;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
@Configuration
|
@Component
|
||||||
@PropertySource(value = "classpath:configprops.json", factory = JsonPropertySourceFactory.class)
|
@PropertySource(value = "classpath:configprops.json", factory = JsonPropertySourceFactory.class)
|
||||||
@ConfigurationProperties
|
@ConfigurationProperties
|
||||||
public class JsonProperties {
|
public class JsonProperties {
|
||||||
|
|
|
@ -26,7 +26,7 @@ public class JsonPropertyContextInitializer implements ApplicationContextInitial
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
public void initialize(ConfigurableApplicationContext configurableApplicationContext) {
|
public void initialize(ConfigurableApplicationContext configurableApplicationContext) {
|
||||||
try {
|
try {
|
||||||
Resource resource = configurableApplicationContext.getResource("classpath:configpropscustom.json");
|
Resource resource = configurableApplicationContext.getResource("classpath:configprops.json");
|
||||||
Map readValue = new ObjectMapper().readValue(resource.getInputStream(), Map.class);
|
Map readValue = new ObjectMapper().readValue(resource.getInputStream(), Map.class);
|
||||||
Set<Map.Entry> set = readValue.entrySet();
|
Set<Map.Entry> set = readValue.entrySet();
|
||||||
List<MapPropertySource> propertySources = convertEntrySet(set, Optional.empty());
|
List<MapPropertySource> propertySources = convertEntrySet(set, Optional.empty());
|
||||||
|
|
|
@ -4,6 +4,7 @@ import java.io.IOException;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
import org.springframework.core.env.MapPropertySource;
|
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.EncodedResource;
|
||||||
import org.springframework.core.io.support.PropertySourceFactory;
|
import org.springframework.core.io.support.PropertySourceFactory;
|
||||||
|
|
||||||
|
@ -12,7 +13,7 @@ import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
public class JsonPropertySourceFactory implements PropertySourceFactory {
|
public class JsonPropertySourceFactory implements PropertySourceFactory {
|
||||||
|
|
||||||
@Override
|
@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);
|
Map readValue = new ObjectMapper().readValue(resource.getInputStream(), Map.class);
|
||||||
return new MapPropertySource("json-property", readValue);
|
return new MapPropertySource("json-property", readValue);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,9 +0,0 @@
|
||||||
{
|
|
||||||
"host" : "mailer.custom@mail.com",
|
|
||||||
"port" : 8081,
|
|
||||||
"resend" : true,
|
|
||||||
"sender" : {
|
|
||||||
"name": "sender.custom",
|
|
||||||
"address": "street.custom"
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -21,19 +21,19 @@ public class JsonPropertiesIntegrationTest {
|
||||||
private CustomJsonProperties customJsonProperties;
|
private CustomJsonProperties customJsonProperties;
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenJsonPropertySource_whenPropertySourceFactoryUsed_thenLoadFlatValues() {
|
public void whenPropertiesLoadedViaJsonPropertySource_thenLoadFlatValues() {
|
||||||
Assert.assertEquals("mailer@mail.com", jsonProperties.getHost());
|
Assert.assertEquals("mailer@mail.com", jsonProperties.getHost());
|
||||||
Assert.assertEquals(9090, jsonProperties.getPort());
|
Assert.assertEquals(9090, jsonProperties.getPort());
|
||||||
Assert.assertTrue(jsonProperties.isResend());
|
Assert.assertTrue(jsonProperties.isResend());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenJsonPropertySource_whenPropertySourceFactoryUsed_thenLoadListValues() {
|
public void whenPropertiesLoadedViaJsonPropertySource_thenLoadListValues() {
|
||||||
Assert.assertThat(jsonProperties.getTopics(), Matchers.is(Arrays.asList("spring", "boot")));
|
Assert.assertThat(jsonProperties.getTopics(), Matchers.is(Arrays.asList("spring", "boot")));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenJsonPropertySource_whenPropertySourceFactoryUsed_thenNestedValuesLoadedAsMap() {
|
public void whenPropertiesLoadedViaJsonPropertySource_thenNestedLoadedAsMap() {
|
||||||
Assert.assertEquals("sender", jsonProperties.getSender()
|
Assert.assertEquals("sender", jsonProperties.getSender()
|
||||||
.get("name"));
|
.get("name"));
|
||||||
Assert.assertEquals("street", jsonProperties.getSender()
|
Assert.assertEquals("street", jsonProperties.getSender()
|
||||||
|
@ -41,18 +41,18 @@ public class JsonPropertiesIntegrationTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenCustomJsonPropertySource_whenLoadedIntoEnvironment_thenFlatValuesPopulated() {
|
public void whenLoadedIntoEnvironment_thenFlatValuesPopulated() {
|
||||||
Assert.assertEquals("mailer.custom@mail.com", customJsonProperties.getHost());
|
Assert.assertEquals("mailer@mail.com", customJsonProperties.getHost());
|
||||||
Assert.assertEquals(8081, customJsonProperties.getPort());
|
Assert.assertEquals(9090, customJsonProperties.getPort());
|
||||||
Assert.assertTrue(customJsonProperties.isResend());
|
Assert.assertTrue(customJsonProperties.isResend());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenCustomJsonPropertySource_whenLoadedIntoEnvironment_thenValuesLoadedIntoClassObject() {
|
public void whenLoadedIntoEnvironment_thenValuesLoadedIntoClassObject() {
|
||||||
Assert.assertNotNull(customJsonProperties.getSender());
|
Assert.assertNotNull(customJsonProperties.getSender());
|
||||||
Assert.assertEquals("sender.custom", customJsonProperties.getSender()
|
Assert.assertEquals("sender", customJsonProperties.getSender()
|
||||||
.getName());
|
.getName());
|
||||||
Assert.assertEquals("street.custom", customJsonProperties.getSender()
|
Assert.assertEquals("street", customJsonProperties.getSender()
|
||||||
.getAddress());
|
.getAddress());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue