From 5ab3de4965aead4cc30ed2027638fd49a480383b Mon Sep 17 00:00:00 2001 From: Krzysztof Woyke Date: Wed, 20 May 2020 13:36:48 +0200 Subject: [PATCH 1/6] JAVA-1526: Copy spring-boot-json-properties to spring-boot-properties-2 --- .../spring-boot-properties-2/pom.xml | 4 ++ .../json/ConfigPropertiesDemoApplication.java | 16 +++++ .../properties/json/CustomJsonProperties.java | 71 +++++++++++++++++++ .../properties/json/JsonProperties.java | 65 +++++++++++++++++ .../json/JsonPropertyContextInitializer.java | 67 +++++++++++++++++ .../factory/JsonPropertySourceFactory.java | 20 ++++++ .../src/main/resources/configprops.json | 10 +++ .../json/JsonPropertiesIntegrationTest.java | 59 +++++++++++++++ 8 files changed, 312 insertions(+) create mode 100644 spring-boot-modules/spring-boot-properties-2/src/main/java/com/baeldung/properties/json/ConfigPropertiesDemoApplication.java create mode 100644 spring-boot-modules/spring-boot-properties-2/src/main/java/com/baeldung/properties/json/CustomJsonProperties.java create mode 100644 spring-boot-modules/spring-boot-properties-2/src/main/java/com/baeldung/properties/json/JsonProperties.java create mode 100644 spring-boot-modules/spring-boot-properties-2/src/main/java/com/baeldung/properties/json/JsonPropertyContextInitializer.java create mode 100644 spring-boot-modules/spring-boot-properties-2/src/main/java/com/baeldung/properties/json/factory/JsonPropertySourceFactory.java create mode 100644 spring-boot-modules/spring-boot-properties-2/src/main/resources/configprops.json create mode 100644 spring-boot-modules/spring-boot-properties-2/src/test/java/com/baeldung/properties/json/JsonPropertiesIntegrationTest.java diff --git a/spring-boot-modules/spring-boot-properties-2/pom.xml b/spring-boot-modules/spring-boot-properties-2/pom.xml index c3c3e57251..bd2a35b19d 100644 --- a/spring-boot-modules/spring-boot-properties-2/pom.xml +++ b/spring-boot-modules/spring-boot-properties-2/pom.xml @@ -20,6 +20,10 @@ org.springframework.boot spring-boot-starter + + org.springframework.boot + spring-boot-starter-web + diff --git a/spring-boot-modules/spring-boot-properties-2/src/main/java/com/baeldung/properties/json/ConfigPropertiesDemoApplication.java b/spring-boot-modules/spring-boot-properties-2/src/main/java/com/baeldung/properties/json/ConfigPropertiesDemoApplication.java new file mode 100644 index 0000000000..a1e2584b2c --- /dev/null +++ b/spring-boot-modules/spring-boot-properties-2/src/main/java/com/baeldung/properties/json/ConfigPropertiesDemoApplication.java @@ -0,0 +1,16 @@ +package com.baeldung.properties.json; + +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.builder.SpringApplicationBuilder; +import org.springframework.context.annotation.ComponentScan; + +@SpringBootApplication +@ComponentScan(basePackageClasses = {JsonProperties.class, CustomJsonProperties.class}) +public class ConfigPropertiesDemoApplication { + + public static void main(String[] args) { + new SpringApplicationBuilder(ConfigPropertiesDemoApplication.class).initializers(new JsonPropertyContextInitializer()) + .run(); + } + +} diff --git a/spring-boot-modules/spring-boot-properties-2/src/main/java/com/baeldung/properties/json/CustomJsonProperties.java b/spring-boot-modules/spring-boot-properties-2/src/main/java/com/baeldung/properties/json/CustomJsonProperties.java new file mode 100644 index 0000000000..555711c49b --- /dev/null +++ b/spring-boot-modules/spring-boot-properties-2/src/main/java/com/baeldung/properties/json/CustomJsonProperties.java @@ -0,0 +1,71 @@ +package com.baeldung.properties.json; + +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-2/src/main/java/com/baeldung/properties/json/JsonProperties.java b/spring-boot-modules/spring-boot-properties-2/src/main/java/com/baeldung/properties/json/JsonProperties.java new file mode 100644 index 0000000000..6ada770e3b --- /dev/null +++ b/spring-boot-modules/spring-boot-properties-2/src/main/java/com/baeldung/properties/json/JsonProperties.java @@ -0,0 +1,65 @@ +package com.baeldung.properties.json; + +import com.baeldung.properties.json.factory.JsonPropertySourceFactory; +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.context.annotation.PropertySource; +import org.springframework.stereotype.Component; + +import java.util.LinkedHashMap; +import java.util.List; + +@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-2/src/main/java/com/baeldung/properties/json/JsonPropertyContextInitializer.java b/spring-boot-modules/spring-boot-properties-2/src/main/java/com/baeldung/properties/json/JsonPropertyContextInitializer.java new file mode 100644 index 0000000000..e3b713f29b --- /dev/null +++ b/spring-boot-modules/spring-boot-properties-2/src/main/java/com/baeldung/properties/json/JsonPropertyContextInitializer.java @@ -0,0 +1,67 @@ +package com.baeldung.properties.json; + +import com.fasterxml.jackson.databind.ObjectMapper; +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 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; + +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-2/src/main/java/com/baeldung/properties/json/factory/JsonPropertySourceFactory.java b/spring-boot-modules/spring-boot-properties-2/src/main/java/com/baeldung/properties/json/factory/JsonPropertySourceFactory.java new file mode 100644 index 0000000000..dccaae4ad2 --- /dev/null +++ b/spring-boot-modules/spring-boot-properties-2/src/main/java/com/baeldung/properties/json/factory/JsonPropertySourceFactory.java @@ -0,0 +1,20 @@ +package com.baeldung.properties.json.factory; + +import com.fasterxml.jackson.databind.ObjectMapper; +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 java.io.IOException; +import java.util.Map; + +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-2/src/main/resources/configprops.json b/spring-boot-modules/spring-boot-properties-2/src/main/resources/configprops.json new file mode 100644 index 0000000000..1602663775 --- /dev/null +++ b/spring-boot-modules/spring-boot-properties-2/src/main/resources/configprops.json @@ -0,0 +1,10 @@ +{ + "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-2/src/test/java/com/baeldung/properties/json/JsonPropertiesIntegrationTest.java b/spring-boot-modules/spring-boot-properties-2/src/test/java/com/baeldung/properties/json/JsonPropertiesIntegrationTest.java new file mode 100644 index 0000000000..6b00489b5c --- /dev/null +++ b/spring-boot-modules/spring-boot-properties-2/src/test/java/com/baeldung/properties/json/JsonPropertiesIntegrationTest.java @@ -0,0 +1,59 @@ +package com.baeldung.properties.json; + +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; + +import java.util.Arrays; + +@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()); + } + +} From 8afe64de3e82fda4a9333611ae2c3b6c8366e490 Mon Sep 17 00:00:00 2001 From: Krzysztof Woyke Date: Wed, 20 May 2020 14:37:43 +0200 Subject: [PATCH 2/6] JAVA-1526: Remove spring-boot-json-properties from spring-boot-properties --- .../spring-boot-properties-2/README.md | 1 + .../spring-boot-properties/README.md | 1 - .../ConfigPropertiesDemoApplication.java | 7 +- .../properties/CustomJsonProperties.java | 71 ------------------- .../baeldung/properties/JsonProperties.java | 64 ----------------- .../JsonPropertyContextInitializer.java | 68 ------------------ .../properties/JsonPropertySourceFactory.java | 21 ------ .../src/main/resources/configprops.json | 10 --- .../JsonPropertiesIntegrationTest.java | 59 --------------- .../PriorityProviderIntegrationTest.java | 2 +- 10 files changed, 6 insertions(+), 298 deletions(-) delete mode 100644 spring-boot-modules/spring-boot-properties/src/main/java/com/baeldung/properties/CustomJsonProperties.java delete mode 100644 spring-boot-modules/spring-boot-properties/src/main/java/com/baeldung/properties/JsonProperties.java delete mode 100644 spring-boot-modules/spring-boot-properties/src/main/java/com/baeldung/properties/JsonPropertyContextInitializer.java delete mode 100644 spring-boot-modules/spring-boot-properties/src/main/java/com/baeldung/properties/JsonPropertySourceFactory.java delete mode 100644 spring-boot-modules/spring-boot-properties/src/main/resources/configprops.json delete mode 100644 spring-boot-modules/spring-boot-properties/src/test/java/com/baeldung/properties/JsonPropertiesIntegrationTest.java 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 From c4101d7df754baa839dc6433804198197ad7e4cc Mon Sep 17 00:00:00 2001 From: Krzysztof Woyke Date: Wed, 20 May 2020 14:50:33 +0200 Subject: [PATCH 3/6] JAVA-1526: Fix test configuration --- .../baeldung/properties/ConfigPropertiesDemoApplication.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 88e6f47e51..1e5e88921a 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 @@ -9,7 +9,7 @@ import org.springframework.context.annotation.ComponentScan; import com.baeldung.configurationproperties.ConfigProperties; @SpringBootApplication -@ComponentScan(basePackageClasses = ConfigProperties.class) +@ComponentScan(basePackageClasses = {ConfigProperties.class, AdditionalProperties.class}) public class ConfigPropertiesDemoApplication { public static void main(String[] args) { SpringApplication.run(ConfigPropertiesDemoApplication.class, args); From 304a6fb6ccfb37a63b18b0f2534acb90cbd5b7d9 Mon Sep 17 00:00:00 2001 From: Krzysztof Woyke Date: Wed, 20 May 2020 15:51:11 +0200 Subject: [PATCH 4/6] JAVA-1526: Copy three @Value related articles to spring-boot-properties-2 --- .../spring-boot-properties-2/README.md | 5 +- .../value/ClassNotManagedBySpring.java | 28 +++++ .../properties/value/CollectionProvider.java | 27 +++++ .../properties/value/InitializerBean.java | 21 ++++ .../properties/value/PriorityProvider.java | 22 ++++ .../baeldung/properties/value/SomeBean.java | 17 +++ .../baeldung/properties/value/ValuesApp.java | 101 ++++++++++++++++++ .../value/defaults/ValuesWithDefaultsApp.java | 71 ++++++++++++ .../src/main/resources/values.properties | 4 + .../resources/valueswithdefaults.properties | 0 ...lassNotManagedBySpringIntegrationTest.java | 40 +++++++ .../CollectionProviderIntegrationTest.java | 22 ++++ .../PriorityProviderIntegrationTest.java | 22 ++++ .../spring-boot-properties/README.md | 3 - 14 files changed, 379 insertions(+), 4 deletions(-) create mode 100644 spring-boot-modules/spring-boot-properties-2/src/main/java/com/baeldung/properties/value/ClassNotManagedBySpring.java create mode 100644 spring-boot-modules/spring-boot-properties-2/src/main/java/com/baeldung/properties/value/CollectionProvider.java create mode 100644 spring-boot-modules/spring-boot-properties-2/src/main/java/com/baeldung/properties/value/InitializerBean.java create mode 100644 spring-boot-modules/spring-boot-properties-2/src/main/java/com/baeldung/properties/value/PriorityProvider.java create mode 100644 spring-boot-modules/spring-boot-properties-2/src/main/java/com/baeldung/properties/value/SomeBean.java create mode 100644 spring-boot-modules/spring-boot-properties-2/src/main/java/com/baeldung/properties/value/ValuesApp.java create mode 100644 spring-boot-modules/spring-boot-properties-2/src/main/java/com/baeldung/properties/value/defaults/ValuesWithDefaultsApp.java create mode 100644 spring-boot-modules/spring-boot-properties-2/src/main/resources/values.properties create mode 100644 spring-boot-modules/spring-boot-properties-2/src/main/resources/valueswithdefaults.properties create mode 100644 spring-boot-modules/spring-boot-properties-2/src/test/java/com/baeldung/properties/value/ClassNotManagedBySpringIntegrationTest.java create mode 100644 spring-boot-modules/spring-boot-properties-2/src/test/java/com/baeldung/properties/value/CollectionProviderIntegrationTest.java create mode 100644 spring-boot-modules/spring-boot-properties-2/src/test/java/com/baeldung/properties/value/PriorityProviderIntegrationTest.java diff --git a/spring-boot-modules/spring-boot-properties-2/README.md b/spring-boot-modules/spring-boot-properties-2/README.md index 326e652af3..7373932e4f 100644 --- a/spring-boot-modules/spring-boot-properties-2/README.md +++ b/spring-boot-modules/spring-boot-properties-2/README.md @@ -3,4 +3,7 @@ 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 +- [Load Spring Boot Properties From a JSON File](https://www.baeldung.com/spring-boot-json-properties) +- [A Quick Guide to Spring @Value](https://www.baeldung.com/spring-value-annotation) +- [Using Spring @Value with Defaults](https://www.baeldung.com/spring-value-defaults) +- [How to Inject a Property Value Into a Class Not Managed by Spring?](https://www.baeldung.com/inject-properties-value-non-spring-class) \ No newline at end of file diff --git a/spring-boot-modules/spring-boot-properties-2/src/main/java/com/baeldung/properties/value/ClassNotManagedBySpring.java b/spring-boot-modules/spring-boot-properties-2/src/main/java/com/baeldung/properties/value/ClassNotManagedBySpring.java new file mode 100644 index 0000000000..59ee73a07b --- /dev/null +++ b/spring-boot-modules/spring-boot-properties-2/src/main/java/com/baeldung/properties/value/ClassNotManagedBySpring.java @@ -0,0 +1,28 @@ +package com.baeldung.properties.value; + +public class ClassNotManagedBySpring { + + private String customVariable; + private String anotherCustomVariable; + + public ClassNotManagedBySpring(String someInitialValue, String anotherManagedValue) { + this.customVariable = someInitialValue; + this.anotherCustomVariable = anotherManagedValue; + } + + public String getCustomVariable() { + return customVariable; + } + + public void setCustomVariable(String customVariable) { + this.customVariable = customVariable; + } + + public String getAnotherCustomVariable() { + return anotherCustomVariable; + } + + public void setAnotherCustomVariable(String anotherCustomVariable) { + this.anotherCustomVariable = anotherCustomVariable; + } +} diff --git a/spring-boot-modules/spring-boot-properties-2/src/main/java/com/baeldung/properties/value/CollectionProvider.java b/spring-boot-modules/spring-boot-properties-2/src/main/java/com/baeldung/properties/value/CollectionProvider.java new file mode 100644 index 0000000000..6327e688e8 --- /dev/null +++ b/spring-boot-modules/spring-boot-properties-2/src/main/java/com/baeldung/properties/value/CollectionProvider.java @@ -0,0 +1,27 @@ +package com.baeldung.properties.value; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.PropertySource; +import org.springframework.stereotype.Component; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.List; + +@Component +@PropertySource("classpath:values.properties") +public class CollectionProvider { + + private final List values = new ArrayList<>(); + + public Collection getValues() { + return Collections.unmodifiableCollection(values); + } + + @Autowired + public void setValues(@Value("#{'${listOfValues}'.split(',')}") List values) { + this.values.addAll(values); + } +} diff --git a/spring-boot-modules/spring-boot-properties-2/src/main/java/com/baeldung/properties/value/InitializerBean.java b/spring-boot-modules/spring-boot-properties-2/src/main/java/com/baeldung/properties/value/InitializerBean.java new file mode 100644 index 0000000000..b06cfa12ea --- /dev/null +++ b/spring-boot-modules/spring-boot-properties-2/src/main/java/com/baeldung/properties/value/InitializerBean.java @@ -0,0 +1,21 @@ +package com.baeldung.properties.value; + +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Component; + +@Component +public class InitializerBean { + + private String someInitialValue; + private String anotherManagedValue; + + public InitializerBean(@Value("someInitialValue") String someInitialValue, @Value("anotherValue") String anotherManagedValue) { + this.someInitialValue = someInitialValue; + this.anotherManagedValue = anotherManagedValue; + } + + public ClassNotManagedBySpring initClass() { + return new ClassNotManagedBySpring(this.someInitialValue, this.anotherManagedValue); + } + +} diff --git a/spring-boot-modules/spring-boot-properties-2/src/main/java/com/baeldung/properties/value/PriorityProvider.java b/spring-boot-modules/spring-boot-properties-2/src/main/java/com/baeldung/properties/value/PriorityProvider.java new file mode 100644 index 0000000000..892118eb06 --- /dev/null +++ b/spring-boot-modules/spring-boot-properties-2/src/main/java/com/baeldung/properties/value/PriorityProvider.java @@ -0,0 +1,22 @@ +package com.baeldung.properties.value; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.PropertySource; +import org.springframework.stereotype.Component; + +@Component +@PropertySource("classpath:values.properties") +public class PriorityProvider { + + private final String priority; + + @Autowired + public PriorityProvider(@Value("${priority:normal}") String priority) { + this.priority = priority; + } + + public String getPriority() { + return priority; + } +} diff --git a/spring-boot-modules/spring-boot-properties-2/src/main/java/com/baeldung/properties/value/SomeBean.java b/spring-boot-modules/spring-boot-properties-2/src/main/java/com/baeldung/properties/value/SomeBean.java new file mode 100644 index 0000000000..b3346a96dd --- /dev/null +++ b/spring-boot-modules/spring-boot-properties-2/src/main/java/com/baeldung/properties/value/SomeBean.java @@ -0,0 +1,17 @@ +package com.baeldung.properties.value; + +public class SomeBean { + private int someValue; + + public SomeBean(int someValue) { + this.someValue = someValue; + } + + public int getSomeValue() { + return someValue; + } + + public void setSomeValue(int someValue) { + this.someValue = someValue; + } +} diff --git a/spring-boot-modules/spring-boot-properties-2/src/main/java/com/baeldung/properties/value/ValuesApp.java b/spring-boot-modules/spring-boot-properties-2/src/main/java/com/baeldung/properties/value/ValuesApp.java new file mode 100644 index 0000000000..67547199a6 --- /dev/null +++ b/spring-boot-modules/spring-boot-properties-2/src/main/java/com/baeldung/properties/value/ValuesApp.java @@ -0,0 +1,101 @@ +package com.baeldung.properties.value; + +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.ApplicationContext; +import org.springframework.context.annotation.AnnotationConfigApplicationContext; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.PropertySource; + +import javax.annotation.PostConstruct; +import java.util.Arrays; +import java.util.List; +import java.util.Map; + +@Configuration +@PropertySource(name = "myProperties", value = "values.properties") +public class ValuesApp { + + @Value("string value") + private String stringValue; + + @Value("${value.from.file}") + private String valueFromFile; + + @Value("${systemValue}") + private String systemValue; + + @Value("${unknown_param:some default}") + private String someDefault; + + @Value("${priority}") + private String prioritySystemProperty; + + @Value("${listOfValues}") + private String[] valuesArray; + + @Value("#{systemProperties['priority']}") + private String spelValue; + + @Value("#{systemProperties['unknown'] ?: 'some default'}") + private String spelSomeDefault; + + @Value("#{someBean.someValue}") + private Integer someBeanValue; + + @Value("#{'${listOfValues}'.split(',')}") + private List valuesList; + + @Value("#{${valuesMap}}") + private Map valuesMap; + + @Value("#{${valuesMap}.key1}") + private Integer valuesMapKey1; + + @Value("#{${valuesMap}['unknownKey']}") + private Integer unknownMapKey; + + @Value("#{${unknownMap : {key1:'1', key2 : '2'}}}") + private Map unknownMap; + + @Value("#{${valuesMap}['unknownKey'] ?: 5}") + private Integer unknownMapKeyWithDefaultValue; + + @Value("#{${valuesMap}.?[value>'1']}") + private Map valuesMapFiltered; + + @Value("#{systemProperties}") + private Map systemPropertiesMap; + + public static void main(String[] args) { + System.setProperty("systemValue", "Some system parameter value"); + System.setProperty("priority", "System property"); + ApplicationContext applicationContext = new AnnotationConfigApplicationContext(ValuesApp.class); + } + + @Bean + public SomeBean someBean() { + return new SomeBean(10); + } + + @PostConstruct + public void afterInitialize() { + System.out.println(stringValue); + System.out.println(valueFromFile); + System.out.println(systemValue); + System.out.println(someDefault); + System.out.println(prioritySystemProperty); + System.out.println(Arrays.toString(valuesArray)); + System.out.println(spelValue); + System.out.println(spelSomeDefault); + System.out.println(someBeanValue); + System.out.println(valuesList); + System.out.println(valuesMap); + System.out.println(valuesMapKey1); + System.out.println(unknownMapKey); + System.out.println(unknownMap); + System.out.println(unknownMapKeyWithDefaultValue); + System.out.println(valuesMapFiltered); + System.out.println(systemPropertiesMap); + } +} diff --git a/spring-boot-modules/spring-boot-properties-2/src/main/java/com/baeldung/properties/value/defaults/ValuesWithDefaultsApp.java b/spring-boot-modules/spring-boot-properties-2/src/main/java/com/baeldung/properties/value/defaults/ValuesWithDefaultsApp.java new file mode 100644 index 0000000000..72fa0e03c0 --- /dev/null +++ b/spring-boot-modules/spring-boot-properties-2/src/main/java/com/baeldung/properties/value/defaults/ValuesWithDefaultsApp.java @@ -0,0 +1,71 @@ +package com.baeldung.properties.value.defaults; + +import org.apache.commons.lang3.ArrayUtils; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.ApplicationContext; +import org.springframework.context.annotation.AnnotationConfigApplicationContext; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.PropertySource; +import org.springframework.util.Assert; + +import javax.annotation.PostConstruct; +import java.util.Arrays; +import java.util.List; + +/** + * Demonstrates setting defaults for @Value annotation. Note that there are no properties + * defined in the specified property source. We also assume that the user here + * does not have a system property named some.key. + * + */ +@Configuration +@PropertySource(name = "myProperties", value = "valueswithdefaults.properties") +public class ValuesWithDefaultsApp { + + @Value("${some.key:my default value}") + private String stringWithDefaultValue; + + @Value("${some.key:}") + private String stringWithBlankDefaultValue; + + @Value("${some.key:true}") + private boolean booleanWithDefaultValue; + + @Value("${some.key:42}") + private int intWithDefaultValue; + + @Value("${some.key:one,two,three}") + private String[] stringArrayWithDefaults; + + @Value("${some.key:1,2,3}") + private int[] intArrayWithDefaults; + + @Value("#{systemProperties['some.key'] ?: 'my default system property value'}") + private String spelWithDefaultValue; + + + public static void main(String[] args) { + ApplicationContext applicationContext = new AnnotationConfigApplicationContext(ValuesWithDefaultsApp.class); + } + + @PostConstruct + public void afterInitialize() { + // strings + Assert.isTrue(stringWithDefaultValue.equals("my default value")); + Assert.isTrue(stringWithBlankDefaultValue.equals("")); + + // other primitives + Assert.isTrue(booleanWithDefaultValue); + Assert.isTrue(intWithDefaultValue == 42); + + // arrays + List stringListValues = Arrays.asList("one", "two", "three"); + Assert.isTrue(Arrays.asList(stringArrayWithDefaults).containsAll(stringListValues)); + + List intListValues = Arrays.asList(1, 2, 3); + Assert.isTrue(Arrays.asList(ArrayUtils.toObject(intArrayWithDefaults)).containsAll(intListValues)); + + // SpEL + Assert.isTrue(spelWithDefaultValue.equals("my default system property value")); + } +} diff --git a/spring-boot-modules/spring-boot-properties-2/src/main/resources/values.properties b/spring-boot-modules/spring-boot-properties-2/src/main/resources/values.properties new file mode 100644 index 0000000000..9c85893d5f --- /dev/null +++ b/spring-boot-modules/spring-boot-properties-2/src/main/resources/values.properties @@ -0,0 +1,4 @@ +value.from.file=Value got from the file +priority=high +listOfValues=A,B,C +valuesMap={key1:'1', key2 : '2', key3 : '3'} \ No newline at end of file diff --git a/spring-boot-modules/spring-boot-properties-2/src/main/resources/valueswithdefaults.properties b/spring-boot-modules/spring-boot-properties-2/src/main/resources/valueswithdefaults.properties new file mode 100644 index 0000000000..e69de29bb2 diff --git a/spring-boot-modules/spring-boot-properties-2/src/test/java/com/baeldung/properties/value/ClassNotManagedBySpringIntegrationTest.java b/spring-boot-modules/spring-boot-properties-2/src/test/java/com/baeldung/properties/value/ClassNotManagedBySpringIntegrationTest.java new file mode 100644 index 0000000000..271242d4e8 --- /dev/null +++ b/spring-boot-modules/spring-boot-properties-2/src/test/java/com/baeldung/properties/value/ClassNotManagedBySpringIntegrationTest.java @@ -0,0 +1,40 @@ +package com.baeldung.properties.value; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.test.context.junit4.SpringRunner; + +import static junit.framework.TestCase.assertEquals; +import static org.mockito.Mockito.when; + +@RunWith(SpringRunner.class) +public class ClassNotManagedBySpringIntegrationTest { + + @MockBean + private InitializerBean initializerBean; + + @Before + public void init() { + when(initializerBean.initClass()) + .thenReturn(new ClassNotManagedBySpring("This is only sample value", "Another configured value")); + } + + @Test + public void givenInitializerBean_whenInvokedInitClass_shouldInitialize() throws Exception { + + //given + ClassNotManagedBySpring classNotManagedBySpring = initializerBean.initClass(); + + //when + String initializedValue = classNotManagedBySpring.getCustomVariable(); + String anotherCustomVariable = classNotManagedBySpring.getAnotherCustomVariable(); + + //then + assertEquals("This is only sample value", initializedValue); + assertEquals("Another configured value", anotherCustomVariable); + + } + +} \ No newline at end of file diff --git a/spring-boot-modules/spring-boot-properties-2/src/test/java/com/baeldung/properties/value/CollectionProviderIntegrationTest.java b/spring-boot-modules/spring-boot-properties-2/src/test/java/com/baeldung/properties/value/CollectionProviderIntegrationTest.java new file mode 100644 index 0000000000..b045786a29 --- /dev/null +++ b/spring-boot-modules/spring-boot-properties-2/src/test/java/com/baeldung/properties/value/CollectionProviderIntegrationTest.java @@ -0,0 +1,22 @@ +package com.baeldung.properties.value; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; + +import static org.assertj.core.api.Assertions.assertThat; + +@RunWith(SpringRunner.class) +@SpringBootTest(classes = CollectionProvider.class) +public class CollectionProviderIntegrationTest { + + @Autowired + private CollectionProvider collectionProvider; + + @Test + public void givenPropertyFileWhenSetterInjectionUsedThenValueInjected() { + assertThat(collectionProvider.getValues()).contains("A", "B", "C"); + } +} \ No newline at end of file diff --git a/spring-boot-modules/spring-boot-properties-2/src/test/java/com/baeldung/properties/value/PriorityProviderIntegrationTest.java b/spring-boot-modules/spring-boot-properties-2/src/test/java/com/baeldung/properties/value/PriorityProviderIntegrationTest.java new file mode 100644 index 0000000000..d7d1e7d78b --- /dev/null +++ b/spring-boot-modules/spring-boot-properties-2/src/test/java/com/baeldung/properties/value/PriorityProviderIntegrationTest.java @@ -0,0 +1,22 @@ +package com.baeldung.properties.value; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; + +import static org.assertj.core.api.Assertions.assertThat; + +@RunWith(SpringRunner.class) +@SpringBootTest(classes = PriorityProvider.class) +public class PriorityProviderIntegrationTest { + + @Autowired + private PriorityProvider priorityProvider; + + @Test + public void givenPropertyFileWhenConstructorInjectionUsedThenValueInjected() { + assertThat(priorityProvider.getPriority()).isEqualTo("high"); + } +} \ 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 addfe01438..bea3d6a293 100644 --- a/spring-boot-modules/spring-boot-properties/README.md +++ b/spring-boot-modules/spring-boot-properties/README.md @@ -7,9 +7,6 @@ This module contains articles about Properties in Spring Boot. - [Guide to @ConfigurationProperties in Spring Boot](https://www.baeldung.com/configuration-properties-in-spring-boot) - [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) - [Spring YAML Configuration](https://www.baeldung.com/spring-yaml) -- [Using Spring @Value with Defaults](https://www.baeldung.com/spring-value-defaults) -- [How to Inject a Property Value Into a Class Not Managed by Spring?](https://www.baeldung.com/inject-properties-value-non-spring-class) - [Add Build Properties to a Spring Boot Application](https://www.baeldung.com/spring-boot-build-properties) - [IntelliJ – Cannot Resolve Spring Boot Configuration Properties Error](https://www.baeldung.com/intellij-resolve-spring-boot-configuration-properties) From 7c0fa2dbcf183bcec2dace829746f874a47e527e Mon Sep 17 00:00:00 2001 From: Krzysztof Woyke Date: Wed, 20 May 2020 15:58:43 +0200 Subject: [PATCH 5/6] JAVA-1526: Delete moved articles from spring-boot-properties --- .../value/ClassNotManagedBySpring.java | 28 ----- .../baeldung/value/CollectionProvider.java | 27 ----- .../com/baeldung/value/InitializerBean.java | 21 ---- .../com/baeldung/value/PriorityProvider.java | 22 ---- .../java/com/baeldung/value/SomeBean.java | 17 --- .../java/com/baeldung/value/ValuesApp.java | 102 ------------------ .../ValuesWithDefaultsApp.java | 75 ------------- .../src/main/resources/values.properties | 4 - .../resources/valueswithdefaults.properties | 0 ...lassNotManagedBySpringIntegrationTest.java | 40 ------- .../CollectionProviderIntegrationTest.java | 22 ---- .../PriorityProviderIntegrationTest.java | 22 ---- 12 files changed, 380 deletions(-) delete mode 100644 spring-boot-modules/spring-boot-properties/src/main/java/com/baeldung/value/ClassNotManagedBySpring.java delete mode 100644 spring-boot-modules/spring-boot-properties/src/main/java/com/baeldung/value/CollectionProvider.java delete mode 100644 spring-boot-modules/spring-boot-properties/src/main/java/com/baeldung/value/InitializerBean.java delete mode 100644 spring-boot-modules/spring-boot-properties/src/main/java/com/baeldung/value/PriorityProvider.java delete mode 100644 spring-boot-modules/spring-boot-properties/src/main/java/com/baeldung/value/SomeBean.java delete mode 100644 spring-boot-modules/spring-boot-properties/src/main/java/com/baeldung/value/ValuesApp.java delete mode 100644 spring-boot-modules/spring-boot-properties/src/main/java/com/baeldung/valuewithdefaults/ValuesWithDefaultsApp.java delete mode 100644 spring-boot-modules/spring-boot-properties/src/main/resources/values.properties delete mode 100644 spring-boot-modules/spring-boot-properties/src/main/resources/valueswithdefaults.properties delete mode 100644 spring-boot-modules/spring-boot-properties/src/test/java/com/baeldung/value/ClassNotManagedBySpringIntegrationTest.java delete mode 100644 spring-boot-modules/spring-boot-properties/src/test/java/com/baeldung/value/CollectionProviderIntegrationTest.java delete mode 100644 spring-boot-modules/spring-boot-properties/src/test/java/com/baeldung/value/PriorityProviderIntegrationTest.java diff --git a/spring-boot-modules/spring-boot-properties/src/main/java/com/baeldung/value/ClassNotManagedBySpring.java b/spring-boot-modules/spring-boot-properties/src/main/java/com/baeldung/value/ClassNotManagedBySpring.java deleted file mode 100644 index 0329769d3c..0000000000 --- a/spring-boot-modules/spring-boot-properties/src/main/java/com/baeldung/value/ClassNotManagedBySpring.java +++ /dev/null @@ -1,28 +0,0 @@ -package com.baeldung.value; - -public class ClassNotManagedBySpring { - - private String customVariable; - private String anotherCustomVariable; - - public ClassNotManagedBySpring(String someInitialValue, String anotherManagedValue) { - this.customVariable = someInitialValue; - this.anotherCustomVariable = anotherManagedValue; - } - - public String getCustomVariable() { - return customVariable; - } - - public void setCustomVariable(String customVariable) { - this.customVariable = customVariable; - } - - public String getAnotherCustomVariable() { - return anotherCustomVariable; - } - - public void setAnotherCustomVariable(String anotherCustomVariable) { - this.anotherCustomVariable = anotherCustomVariable; - } -} diff --git a/spring-boot-modules/spring-boot-properties/src/main/java/com/baeldung/value/CollectionProvider.java b/spring-boot-modules/spring-boot-properties/src/main/java/com/baeldung/value/CollectionProvider.java deleted file mode 100644 index fdc1f8ee03..0000000000 --- a/spring-boot-modules/spring-boot-properties/src/main/java/com/baeldung/value/CollectionProvider.java +++ /dev/null @@ -1,27 +0,0 @@ -package com.baeldung.value; - -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.beans.factory.annotation.Value; -import org.springframework.context.annotation.PropertySource; -import org.springframework.stereotype.Component; - -import java.util.ArrayList; -import java.util.Collection; -import java.util.Collections; -import java.util.List; - -@Component -@PropertySource("classpath:values.properties") -public class CollectionProvider { - - private final List values = new ArrayList<>(); - - public Collection getValues() { - return Collections.unmodifiableCollection(values); - } - - @Autowired - public void setValues(@Value("#{'${listOfValues}'.split(',')}") List values) { - this.values.addAll(values); - } -} diff --git a/spring-boot-modules/spring-boot-properties/src/main/java/com/baeldung/value/InitializerBean.java b/spring-boot-modules/spring-boot-properties/src/main/java/com/baeldung/value/InitializerBean.java deleted file mode 100644 index 8c8634c767..0000000000 --- a/spring-boot-modules/spring-boot-properties/src/main/java/com/baeldung/value/InitializerBean.java +++ /dev/null @@ -1,21 +0,0 @@ -package com.baeldung.value; - -import org.springframework.beans.factory.annotation.Value; -import org.springframework.stereotype.Component; - -@Component -public class InitializerBean { - - private String someInitialValue; - private String anotherManagedValue; - - public InitializerBean(@Value("someInitialValue") String someInitialValue, @Value("anotherValue") String anotherManagedValue) { - this.someInitialValue = someInitialValue; - this.anotherManagedValue = anotherManagedValue; - } - - public ClassNotManagedBySpring initClass() { - return new ClassNotManagedBySpring(this.someInitialValue, this.anotherManagedValue); - } - -} diff --git a/spring-boot-modules/spring-boot-properties/src/main/java/com/baeldung/value/PriorityProvider.java b/spring-boot-modules/spring-boot-properties/src/main/java/com/baeldung/value/PriorityProvider.java deleted file mode 100644 index 9d4b105afa..0000000000 --- a/spring-boot-modules/spring-boot-properties/src/main/java/com/baeldung/value/PriorityProvider.java +++ /dev/null @@ -1,22 +0,0 @@ -package com.baeldung.value; - -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.beans.factory.annotation.Value; -import org.springframework.context.annotation.PropertySource; -import org.springframework.stereotype.Component; - -@Component -@PropertySource("classpath:values.properties") -public class PriorityProvider { - - private final String priority; - - @Autowired - public PriorityProvider(@Value("${priority:normal}") String priority) { - this.priority = priority; - } - - public String getPriority() { - return priority; - } -} diff --git a/spring-boot-modules/spring-boot-properties/src/main/java/com/baeldung/value/SomeBean.java b/spring-boot-modules/spring-boot-properties/src/main/java/com/baeldung/value/SomeBean.java deleted file mode 100644 index 39d5245049..0000000000 --- a/spring-boot-modules/spring-boot-properties/src/main/java/com/baeldung/value/SomeBean.java +++ /dev/null @@ -1,17 +0,0 @@ -package com.baeldung.value; - -public class SomeBean { - private int someValue; - - public SomeBean(int someValue) { - this.someValue = someValue; - } - - public int getSomeValue() { - return someValue; - } - - public void setSomeValue(int someValue) { - this.someValue = someValue; - } -} diff --git a/spring-boot-modules/spring-boot-properties/src/main/java/com/baeldung/value/ValuesApp.java b/spring-boot-modules/spring-boot-properties/src/main/java/com/baeldung/value/ValuesApp.java deleted file mode 100644 index 80893c1adf..0000000000 --- a/spring-boot-modules/spring-boot-properties/src/main/java/com/baeldung/value/ValuesApp.java +++ /dev/null @@ -1,102 +0,0 @@ -package com.baeldung.value; - -import java.util.Arrays; -import java.util.List; -import java.util.Map; - -import javax.annotation.PostConstruct; - -import org.springframework.beans.factory.annotation.Value; -import org.springframework.context.ApplicationContext; -import org.springframework.context.annotation.AnnotationConfigApplicationContext; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; -import org.springframework.context.annotation.PropertySource; - -@Configuration -@PropertySource(name = "myProperties", value = "values.properties") -public class ValuesApp { - - @Value("string value") - private String stringValue; - - @Value("${value.from.file}") - private String valueFromFile; - - @Value("${systemValue}") - private String systemValue; - - @Value("${unknown_param:some default}") - private String someDefault; - - @Value("${priority}") - private String prioritySystemProperty; - - @Value("${listOfValues}") - private String[] valuesArray; - - @Value("#{systemProperties['priority']}") - private String spelValue; - - @Value("#{systemProperties['unknown'] ?: 'some default'}") - private String spelSomeDefault; - - @Value("#{someBean.someValue}") - private Integer someBeanValue; - - @Value("#{'${listOfValues}'.split(',')}") - private List valuesList; - - @Value("#{${valuesMap}}") - private Map valuesMap; - - @Value("#{${valuesMap}.key1}") - private Integer valuesMapKey1; - - @Value("#{${valuesMap}['unknownKey']}") - private Integer unknownMapKey; - - @Value("#{${unknownMap : {key1:'1', key2 : '2'}}}") - private Map unknownMap; - - @Value("#{${valuesMap}['unknownKey'] ?: 5}") - private Integer unknownMapKeyWithDefaultValue; - - @Value("#{${valuesMap}.?[value>'1']}") - private Map valuesMapFiltered; - - @Value("#{systemProperties}") - private Map systemPropertiesMap; - - public static void main(String[] args) { - System.setProperty("systemValue", "Some system parameter value"); - System.setProperty("priority", "System property"); - ApplicationContext applicationContext = new AnnotationConfigApplicationContext(ValuesApp.class); - } - - @Bean - public SomeBean someBean() { - return new SomeBean(10); - } - - @PostConstruct - public void afterInitialize() { - System.out.println(stringValue); - System.out.println(valueFromFile); - System.out.println(systemValue); - System.out.println(someDefault); - System.out.println(prioritySystemProperty); - System.out.println(Arrays.toString(valuesArray)); - System.out.println(spelValue); - System.out.println(spelSomeDefault); - System.out.println(someBeanValue); - System.out.println(valuesList); - System.out.println(valuesMap); - System.out.println(valuesMapKey1); - System.out.println(unknownMapKey); - System.out.println(unknownMap); - System.out.println(unknownMapKeyWithDefaultValue); - System.out.println(valuesMapFiltered); - System.out.println(systemPropertiesMap); - } -} diff --git a/spring-boot-modules/spring-boot-properties/src/main/java/com/baeldung/valuewithdefaults/ValuesWithDefaultsApp.java b/spring-boot-modules/spring-boot-properties/src/main/java/com/baeldung/valuewithdefaults/ValuesWithDefaultsApp.java deleted file mode 100644 index 589f891e6b..0000000000 --- a/spring-boot-modules/spring-boot-properties/src/main/java/com/baeldung/valuewithdefaults/ValuesWithDefaultsApp.java +++ /dev/null @@ -1,75 +0,0 @@ -package com.baeldung.valuewithdefaults; - -import java.util.Arrays; -import java.util.List; - -import javax.annotation.PostConstruct; - -import org.springframework.beans.factory.annotation.Value; -import org.springframework.context.ApplicationContext; -import org.springframework.context.annotation.AnnotationConfigApplicationContext; -import org.springframework.context.annotation.Configuration; -import org.springframework.context.annotation.PropertySource; -import org.springframework.util.Assert; - -import com.google.common.collect.Lists; -import com.google.common.primitives.Ints; - -/** - * Demonstrates setting defaults for @Value annotation. Note that there are no properties - * defined in the specified property source. We also assume that the user here - * does not have a system property named some.key. - * - */ -@Configuration -@PropertySource(name = "myProperties", value = "valueswithdefaults.properties") -public class ValuesWithDefaultsApp { - - @Value("${some.key:my default value}") - private String stringWithDefaultValue; - - @Value("${some.key:}") - private String stringWithBlankDefaultValue; - - @Value("${some.key:true}") - private boolean booleanWithDefaultValue; - - @Value("${some.key:42}") - private int intWithDefaultValue; - - @Value("${some.key:one,two,three}") - private String[] stringArrayWithDefaults; - - @Value("${some.key:1,2,3}") - private int[] intArrayWithDefaults; - - @Value("#{systemProperties['some.key'] ?: 'my default system property value'}") - private String spelWithDefaultValue; - - - public static void main(String[] args) { - ApplicationContext applicationContext = new AnnotationConfigApplicationContext(ValuesWithDefaultsApp.class); - } - - @PostConstruct - public void afterInitialize() { - // strings - Assert.isTrue(stringWithDefaultValue.equals("my default value")); - Assert.isTrue(stringWithBlankDefaultValue.equals("")); - - // other primitives - Assert.isTrue(booleanWithDefaultValue); - Assert.isTrue(intWithDefaultValue == 42); - - // arrays - List stringListValues = Lists.newArrayList("one", "two", "three"); - Assert.isTrue(Arrays.asList(stringArrayWithDefaults).containsAll(stringListValues)); - - List intListValues = Lists.newArrayList(1, 2, 3); - Assert.isTrue(Ints.asList(intArrayWithDefaults).containsAll(intListValues)); - - // SpEL - Assert.isTrue(spelWithDefaultValue.equals("my default system property value")); - - } -} diff --git a/spring-boot-modules/spring-boot-properties/src/main/resources/values.properties b/spring-boot-modules/spring-boot-properties/src/main/resources/values.properties deleted file mode 100644 index 9c85893d5f..0000000000 --- a/spring-boot-modules/spring-boot-properties/src/main/resources/values.properties +++ /dev/null @@ -1,4 +0,0 @@ -value.from.file=Value got from the file -priority=high -listOfValues=A,B,C -valuesMap={key1:'1', key2 : '2', key3 : '3'} \ No newline at end of file diff --git a/spring-boot-modules/spring-boot-properties/src/main/resources/valueswithdefaults.properties b/spring-boot-modules/spring-boot-properties/src/main/resources/valueswithdefaults.properties deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/spring-boot-modules/spring-boot-properties/src/test/java/com/baeldung/value/ClassNotManagedBySpringIntegrationTest.java b/spring-boot-modules/spring-boot-properties/src/test/java/com/baeldung/value/ClassNotManagedBySpringIntegrationTest.java deleted file mode 100644 index 689801bece..0000000000 --- a/spring-boot-modules/spring-boot-properties/src/test/java/com/baeldung/value/ClassNotManagedBySpringIntegrationTest.java +++ /dev/null @@ -1,40 +0,0 @@ -package com.baeldung.value; - -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.springframework.boot.test.mock.mockito.MockBean; -import org.springframework.test.context.junit4.SpringRunner; - -import static junit.framework.TestCase.assertEquals; -import static org.mockito.Mockito.when; - -@RunWith(SpringRunner.class) -public class ClassNotManagedBySpringIntegrationTest { - - @MockBean - private InitializerBean initializerBean; - - @Before - public void init() { - when(initializerBean.initClass()) - .thenReturn(new ClassNotManagedBySpring("This is only sample value", "Another configured value")); - } - - @Test - public void givenInitializerBean_whenInvokedInitClass_shouldInitialize() throws Exception { - - //given - ClassNotManagedBySpring classNotManagedBySpring = initializerBean.initClass(); - - //when - String initializedValue = classNotManagedBySpring.getCustomVariable(); - String anotherCustomVariable = classNotManagedBySpring.getAnotherCustomVariable(); - - //then - assertEquals("This is only sample value", initializedValue); - assertEquals("Another configured value", anotherCustomVariable); - - } - -} \ No newline at end of file diff --git a/spring-boot-modules/spring-boot-properties/src/test/java/com/baeldung/value/CollectionProviderIntegrationTest.java b/spring-boot-modules/spring-boot-properties/src/test/java/com/baeldung/value/CollectionProviderIntegrationTest.java deleted file mode 100644 index 33552f2e1a..0000000000 --- a/spring-boot-modules/spring-boot-properties/src/test/java/com/baeldung/value/CollectionProviderIntegrationTest.java +++ /dev/null @@ -1,22 +0,0 @@ -package com.baeldung.value; - -import org.junit.Test; -import org.junit.runner.RunWith; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.test.context.junit4.SpringRunner; - -import static org.assertj.core.api.Assertions.assertThat; - -@RunWith(SpringRunner.class) -@SpringBootTest(classes = CollectionProvider.class) -public class CollectionProviderIntegrationTest { - - @Autowired - private CollectionProvider collectionProvider; - - @Test - public void givenPropertyFileWhenSetterInjectionUsedThenValueInjected() { - assertThat(collectionProvider.getValues()).contains("A", "B", "C"); - } -} \ No newline at end of file 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 deleted file mode 100644 index aa365a4b6a..0000000000 --- a/spring-boot-modules/spring-boot-properties/src/test/java/com/baeldung/value/PriorityProviderIntegrationTest.java +++ /dev/null @@ -1,22 +0,0 @@ -package com.baeldung.value; - -import org.junit.Test; -import org.junit.runner.RunWith; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.test.context.junit4.SpringRunner; - -import static org.assertj.core.api.Assertions.assertThat; - -@RunWith(SpringRunner.class) -@SpringBootTest(classes = PriorityProvider.class) -public class PriorityProviderIntegrationTest { - - @Autowired - private PriorityProvider priorityProvider; - - @Test - public void givenPropertyFileWhenConstructorInjectionUsedThenValueInjected() { - assertThat(priorityProvider.getPriority()).isEqualTo("high"); - } -} \ No newline at end of file From adb8dd9fe2bebd2752b626e9bd811891010d7f87 Mon Sep 17 00:00:00 2001 From: Krzysztof Woyke Date: Wed, 20 May 2020 16:03:49 +0200 Subject: [PATCH 6/6] JAVA-1526: Add pagination to the README files --- spring-boot-modules/spring-boot-properties-2/README.md | 3 ++- spring-boot-modules/spring-boot-properties/README.md | 1 + 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/spring-boot-modules/spring-boot-properties-2/README.md b/spring-boot-modules/spring-boot-properties-2/README.md index 7373932e4f..cd0fd5e5ee 100644 --- a/spring-boot-modules/spring-boot-properties-2/README.md +++ b/spring-boot-modules/spring-boot-properties-2/README.md @@ -6,4 +6,5 @@ This module contains articles about Properties in Spring Boot. - [Load Spring Boot Properties From a JSON File](https://www.baeldung.com/spring-boot-json-properties) - [A Quick Guide to Spring @Value](https://www.baeldung.com/spring-value-annotation) - [Using Spring @Value with Defaults](https://www.baeldung.com/spring-value-defaults) -- [How to Inject a Property Value Into a Class Not Managed by Spring?](https://www.baeldung.com/inject-properties-value-non-spring-class) \ No newline at end of file +- [How to Inject a Property Value Into a Class Not Managed by Spring?](https://www.baeldung.com/inject-properties-value-non-spring-class) +- More articles: [[<-- prev]](../spring-boot-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 bea3d6a293..b6685c7587 100644 --- a/spring-boot-modules/spring-boot-properties/README.md +++ b/spring-boot-modules/spring-boot-properties/README.md @@ -10,3 +10,4 @@ This module contains articles about Properties in Spring Boot. - [Spring YAML Configuration](https://www.baeldung.com/spring-yaml) - [Add Build Properties to a Spring Boot Application](https://www.baeldung.com/spring-boot-build-properties) - [IntelliJ – Cannot Resolve Spring Boot Configuration Properties Error](https://www.baeldung.com/intellij-resolve-spring-boot-configuration-properties) +- More articles: [[more -->]](../spring-boot-properties-2) \ No newline at end of file