editorial review
This commit is contained in:
parent
9ab810aba1
commit
ae8efcd328
|
@ -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) {
|
||||
|
|
|
@ -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 {
|
||||
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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<Map.Entry> set = readValue.entrySet();
|
||||
List<MapPropertySource> propertySources = convertEntrySet(set, Optional.empty());
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
|
||||
@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());
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue