diff --git a/spring-boot-modules/spring-boot-properties-2/README.md b/spring-boot-modules/spring-boot-properties-2/README.md index 01e2970e89..326e652af3 100644 --- a/spring-boot-modules/spring-boot-properties-2/README.md +++ b/spring-boot-modules/spring-boot-properties-2/README.md @@ -3,3 +3,4 @@ This module contains articles about Properties in Spring Boot. ### Relevant Articles: +- [Load Spring Boot Properties From a JSON File](https://www.baeldung.com/spring-boot-json-properties) \ No newline at end of file diff --git a/spring-boot-modules/spring-boot-properties/README.md b/spring-boot-modules/spring-boot-properties/README.md index f861a01d10..addfe01438 100644 --- a/spring-boot-modules/spring-boot-properties/README.md +++ b/spring-boot-modules/spring-boot-properties/README.md @@ -5,7 +5,6 @@ This module contains articles about Properties in Spring Boot. ### Relevant Articles: - [Reloading Properties Files in Spring](https://www.baeldung.com/spring-reloading-properties) - [Guide to @ConfigurationProperties in Spring Boot](https://www.baeldung.com/configuration-properties-in-spring-boot) -- [Load Spring Boot Properties From a JSON File](https://www.baeldung.com/spring-boot-json-properties) - [Guide to @EnableConfigurationProperties](https://www.baeldung.com/spring-enable-config-properties) - [Properties with Spring and Spring Boot](https://www.baeldung.com/properties-with-spring) - checkout the `com.baeldung.properties` package for all scenarios of properties injection and usage - [A Quick Guide to Spring @Value](https://www.baeldung.com/spring-value-annotation) diff --git a/spring-boot-modules/spring-boot-properties/src/main/java/com/baeldung/properties/ConfigPropertiesDemoApplication.java b/spring-boot-modules/spring-boot-properties/src/main/java/com/baeldung/properties/ConfigPropertiesDemoApplication.java index e280bbd79f..88e6f47e51 100644 --- a/spring-boot-modules/spring-boot-properties/src/main/java/com/baeldung/properties/ConfigPropertiesDemoApplication.java +++ b/spring-boot-modules/spring-boot-properties/src/main/java/com/baeldung/properties/ConfigPropertiesDemoApplication.java @@ -1,5 +1,7 @@ package com.baeldung.properties; +import com.baeldung.buildproperties.Application; +import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.context.annotation.ComponentScan; @@ -7,11 +9,10 @@ import org.springframework.context.annotation.ComponentScan; import com.baeldung.configurationproperties.ConfigProperties; @SpringBootApplication -@ComponentScan(basePackageClasses = { ConfigProperties.class, JsonProperties.class, CustomJsonProperties.class}) +@ComponentScan(basePackageClasses = ConfigProperties.class) public class ConfigPropertiesDemoApplication { public static void main(String[] args) { - new SpringApplicationBuilder(ConfigPropertiesDemoApplication.class).initializers(new JsonPropertyContextInitializer()) - .run(); + SpringApplication.run(ConfigPropertiesDemoApplication.class, args); } } diff --git a/spring-boot-modules/spring-boot-properties/src/main/java/com/baeldung/properties/CustomJsonProperties.java b/spring-boot-modules/spring-boot-properties/src/main/java/com/baeldung/properties/CustomJsonProperties.java deleted file mode 100644 index 084138ec6f..0000000000 --- a/spring-boot-modules/spring-boot-properties/src/main/java/com/baeldung/properties/CustomJsonProperties.java +++ /dev/null @@ -1,71 +0,0 @@ -package com.baeldung.properties; - -import org.springframework.boot.context.properties.ConfigurationProperties; -import org.springframework.stereotype.Component; - -@Component -@ConfigurationProperties(prefix = "custom") -public class CustomJsonProperties { - - private String host; - - private int port; - - private boolean resend; - - private Person sender; - - public static class Person { - - private String name; - private String address; - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - public String getAddress() { - return address; - } - - public void setAddress(String address) { - this.address = address; - } - } - - 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 boolean isResend() { - return resend; - } - - public void setResend(boolean resend) { - this.resend = resend; - } - - public Person getSender() { - return sender; - } - - public void setSender(Person sender) { - this.sender = sender; - } -} diff --git a/spring-boot-modules/spring-boot-properties/src/main/java/com/baeldung/properties/JsonProperties.java b/spring-boot-modules/spring-boot-properties/src/main/java/com/baeldung/properties/JsonProperties.java deleted file mode 100644 index 31b3be14b4..0000000000 --- a/spring-boot-modules/spring-boot-properties/src/main/java/com/baeldung/properties/JsonProperties.java +++ /dev/null @@ -1,64 +0,0 @@ -package com.baeldung.properties; - -import java.util.LinkedHashMap; -import java.util.List; - -import org.springframework.boot.context.properties.ConfigurationProperties; -import org.springframework.context.annotation.PropertySource; -import org.springframework.stereotype.Component; - -@Component -@PropertySource(value = "classpath:configprops.json", factory = JsonPropertySourceFactory.class) -@ConfigurationProperties -public class JsonProperties { - - private String host; - - private int port; - - private boolean resend; - - private List topics; - - private LinkedHashMap sender; - - public LinkedHashMap getSender() { - return sender; - } - - public void setSender(LinkedHashMap sender) { - this.sender = sender; - } - - public List getTopics() { - return topics; - } - - public void setTopics(List topics) { - this.topics = topics; - } - - public int getPort() { - return port; - } - - public void setPort(int port) { - this.port = port; - } - - public boolean isResend() { - return resend; - } - - public void setResend(boolean resend) { - this.resend = resend; - } - - public String getHost() { - return host; - } - - public void setHost(String host) { - this.host = host; - } -} diff --git a/spring-boot-modules/spring-boot-properties/src/main/java/com/baeldung/properties/JsonPropertyContextInitializer.java b/spring-boot-modules/spring-boot-properties/src/main/java/com/baeldung/properties/JsonPropertyContextInitializer.java deleted file mode 100644 index 0aee149123..0000000000 --- a/spring-boot-modules/spring-boot-properties/src/main/java/com/baeldung/properties/JsonPropertyContextInitializer.java +++ /dev/null @@ -1,68 +0,0 @@ -package com.baeldung.properties; - -import java.io.IOException; -import java.util.Collection; -import java.util.Collections; -import java.util.LinkedHashMap; -import java.util.List; -import java.util.Map; -import java.util.Optional; -import java.util.Set; -import java.util.stream.Collectors; - -import org.springframework.context.ApplicationContextInitializer; -import org.springframework.context.ConfigurableApplicationContext; -import org.springframework.core.env.MapPropertySource; -import org.springframework.core.env.PropertySource; -import org.springframework.core.io.Resource; - -import com.fasterxml.jackson.databind.ObjectMapper; - -public class JsonPropertyContextInitializer implements ApplicationContextInitializer { - - private final static String CUSTOM_PREFIX = "custom."; - - @Override - @SuppressWarnings("unchecked") - public void initialize(ConfigurableApplicationContext configurableApplicationContext) { - try { - 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()); - for (PropertySource propertySource : propertySources) { - configurableApplicationContext.getEnvironment() - .getPropertySources() - .addFirst(propertySource); - } - } catch (IOException e) { - throw new RuntimeException(e); - } - } - - private static List convertEntrySet(Set entrySet, Optional parentKey) { - return entrySet.stream() - .map((Map.Entry e) -> convertToPropertySourceList(e, parentKey)) - .flatMap(Collection::stream) - .collect(Collectors.toList()); - } - - private static List convertToPropertySourceList(Map.Entry e, Optional parentKey) { - String key = parentKey.map(s -> s + ".") - .orElse("") + (String) e.getKey(); - Object value = e.getValue(); - return covertToPropertySourceList(key, value); - } - - @SuppressWarnings("unchecked") - private static List covertToPropertySourceList(String key, Object value) { - if (value instanceof LinkedHashMap) { - LinkedHashMap map = (LinkedHashMap) value; - Set entrySet = map.entrySet(); - return convertEntrySet(entrySet, Optional.ofNullable(key)); - } - String finalKey = CUSTOM_PREFIX + key; - return Collections.singletonList(new MapPropertySource(finalKey, Collections.singletonMap(finalKey, value))); - } - -} \ No newline at end of file diff --git a/spring-boot-modules/spring-boot-properties/src/main/java/com/baeldung/properties/JsonPropertySourceFactory.java b/spring-boot-modules/spring-boot-properties/src/main/java/com/baeldung/properties/JsonPropertySourceFactory.java deleted file mode 100644 index c14d3faea5..0000000000 --- a/spring-boot-modules/spring-boot-properties/src/main/java/com/baeldung/properties/JsonPropertySourceFactory.java +++ /dev/null @@ -1,21 +0,0 @@ -package com.baeldung.properties; - -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; - -import com.fasterxml.jackson.databind.ObjectMapper; - -public class JsonPropertySourceFactory implements PropertySourceFactory { - - @Override - public PropertySource createPropertySource(String name, EncodedResource resource) throws IOException { - Map readValue = new ObjectMapper().readValue(resource.getInputStream(), Map.class); - return new MapPropertySource("json-property", readValue); - } - -} \ No newline at end of file diff --git a/spring-boot-modules/spring-boot-properties/src/main/resources/configprops.json b/spring-boot-modules/spring-boot-properties/src/main/resources/configprops.json deleted file mode 100644 index 1602663775..0000000000 --- a/spring-boot-modules/spring-boot-properties/src/main/resources/configprops.json +++ /dev/null @@ -1,10 +0,0 @@ -{ - "host" : "mailer@mail.com", - "port" : 9090, - "resend" : true, - "topics" : ["spring", "boot"], - "sender" : { - "name": "sender", - "address": "street" - } -} diff --git a/spring-boot-modules/spring-boot-properties/src/test/java/com/baeldung/properties/JsonPropertiesIntegrationTest.java b/spring-boot-modules/spring-boot-properties/src/test/java/com/baeldung/properties/JsonPropertiesIntegrationTest.java deleted file mode 100644 index 2f0e5ae408..0000000000 --- a/spring-boot-modules/spring-boot-properties/src/test/java/com/baeldung/properties/JsonPropertiesIntegrationTest.java +++ /dev/null @@ -1,59 +0,0 @@ -package com.baeldung.properties; - -import java.util.Arrays; - -import org.hamcrest.Matchers; -import org.junit.Assert; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.test.context.ContextConfiguration; -import org.springframework.test.context.junit4.SpringRunner; - -@RunWith(SpringRunner.class) -@ContextConfiguration(classes = ConfigPropertiesDemoApplication.class, initializers = JsonPropertyContextInitializer.class) -public class JsonPropertiesIntegrationTest { - - @Autowired - private JsonProperties jsonProperties; - - @Autowired - private CustomJsonProperties customJsonProperties; - - @Test - public void whenPropertiesLoadedViaJsonPropertySource_thenLoadFlatValues() { - Assert.assertEquals("mailer@mail.com", jsonProperties.getHost()); - Assert.assertEquals(9090, jsonProperties.getPort()); - Assert.assertTrue(jsonProperties.isResend()); - } - - @Test - public void whenPropertiesLoadedViaJsonPropertySource_thenLoadListValues() { - Assert.assertThat(jsonProperties.getTopics(), Matchers.is(Arrays.asList("spring", "boot"))); - } - - @Test - public void whenPropertiesLoadedViaJsonPropertySource_thenNestedLoadedAsMap() { - Assert.assertEquals("sender", jsonProperties.getSender() - .get("name")); - Assert.assertEquals("street", jsonProperties.getSender() - .get("address")); - } - - @Test - public void whenLoadedIntoEnvironment_thenFlatValuesPopulated() { - Assert.assertEquals("mailer@mail.com", customJsonProperties.getHost()); - Assert.assertEquals(9090, customJsonProperties.getPort()); - Assert.assertTrue(customJsonProperties.isResend()); - } - - @Test - public void whenLoadedIntoEnvironment_thenValuesLoadedIntoClassObject() { - Assert.assertNotNull(customJsonProperties.getSender()); - Assert.assertEquals("sender", customJsonProperties.getSender() - .getName()); - Assert.assertEquals("street", customJsonProperties.getSender() - .getAddress()); - } - -} diff --git a/spring-boot-modules/spring-boot-properties/src/test/java/com/baeldung/value/PriorityProviderIntegrationTest.java b/spring-boot-modules/spring-boot-properties/src/test/java/com/baeldung/value/PriorityProviderIntegrationTest.java index 19b5a36ff5..aa365a4b6a 100644 --- a/spring-boot-modules/spring-boot-properties/src/test/java/com/baeldung/value/PriorityProviderIntegrationTest.java +++ b/spring-boot-modules/spring-boot-properties/src/test/java/com/baeldung/value/PriorityProviderIntegrationTest.java @@ -17,6 +17,6 @@ public class PriorityProviderIntegrationTest { @Test public void givenPropertyFileWhenConstructorInjectionUsedThenValueInjected() { - assertThat(priorityProvider.getPriority()).isEqualTo("Properties file"); + assertThat(priorityProvider.getPriority()).isEqualTo("high"); } } \ No newline at end of file