Merge pull request #9330 from kwoyke/JAVA-1526

JAVA-1526: Split or move spring-boot-modules/spring-boot-properties module
This commit is contained in:
Josh Cummings 2020-05-20 10:52:54 -06:00 committed by GitHub
commit b3dd36cb24
23 changed files with 74 additions and 57 deletions

View File

@ -3,3 +3,8 @@
This module contains articles about Properties in Spring Boot. This module contains articles about Properties in Spring Boot.
### Relevant Articles: ### Relevant Articles:
- [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)
- More articles: [[<-- prev]](../spring-boot-properties)

View File

@ -20,6 +20,10 @@
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId> <artifactId>spring-boot-starter</artifactId>
</dependency> </dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies> </dependencies>
</project> </project>

View File

@ -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();
}
}

View File

@ -1,4 +1,4 @@
package com.baeldung.properties; package com.baeldung.properties.json;
import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;

View File

@ -1,12 +1,13 @@
package com.baeldung.properties; package com.baeldung.properties.json;
import java.util.LinkedHashMap;
import java.util.List;
import com.baeldung.properties.json.factory.JsonPropertySourceFactory;
import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource; import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import java.util.LinkedHashMap;
import java.util.List;
@Component @Component
@PropertySource(value = "classpath:configprops.json", factory = JsonPropertySourceFactory.class) @PropertySource(value = "classpath:configprops.json", factory = JsonPropertySourceFactory.class)
@ConfigurationProperties @ConfigurationProperties

View File

@ -1,4 +1,11 @@
package com.baeldung.properties; 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.io.IOException;
import java.util.Collection; import java.util.Collection;
@ -10,14 +17,6 @@ import java.util.Optional;
import java.util.Set; import java.util.Set;
import java.util.stream.Collectors; 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<ConfigurableApplicationContext> { public class JsonPropertyContextInitializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {
private final static String CUSTOM_PREFIX = "custom."; private final static String CUSTOM_PREFIX = "custom.";

View File

@ -1,14 +1,13 @@
package com.baeldung.properties; package com.baeldung.properties.json.factory;
import java.io.IOException;
import java.util.Map;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.core.env.MapPropertySource; import org.springframework.core.env.MapPropertySource;
import org.springframework.core.env.PropertySource; import org.springframework.core.env.PropertySource;
import org.springframework.core.io.support.EncodedResource; import org.springframework.core.io.support.EncodedResource;
import org.springframework.core.io.support.PropertySourceFactory; import org.springframework.core.io.support.PropertySourceFactory;
import com.fasterxml.jackson.databind.ObjectMapper; import java.io.IOException;
import java.util.Map;
public class JsonPropertySourceFactory implements PropertySourceFactory { public class JsonPropertySourceFactory implements PropertySourceFactory {

View File

@ -1,4 +1,4 @@
package com.baeldung.value; package com.baeldung.properties.value;
public class ClassNotManagedBySpring { public class ClassNotManagedBySpring {

View File

@ -1,4 +1,4 @@
package com.baeldung.value; package com.baeldung.properties.value;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;

View File

@ -1,4 +1,4 @@
package com.baeldung.value; package com.baeldung.properties.value;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;

View File

@ -1,4 +1,4 @@
package com.baeldung.value; package com.baeldung.properties.value;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;

View File

@ -1,4 +1,4 @@
package com.baeldung.value; package com.baeldung.properties.value;
public class SomeBean { public class SomeBean {
private int someValue; private int someValue;

View File

@ -1,10 +1,4 @@
package com.baeldung.value; package com.baeldung.properties.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.beans.factory.annotation.Value;
import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContext;
@ -13,6 +7,11 @@ import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource; import org.springframework.context.annotation.PropertySource;
import javax.annotation.PostConstruct;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
@Configuration @Configuration
@PropertySource(name = "myProperties", value = "values.properties") @PropertySource(name = "myProperties", value = "values.properties")
public class ValuesApp { public class ValuesApp {

View File

@ -1,10 +1,6 @@
package com.baeldung.valuewithdefaults; package com.baeldung.properties.value.defaults;
import java.util.Arrays;
import java.util.List;
import javax.annotation.PostConstruct;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext;
@ -12,8 +8,9 @@ import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource; import org.springframework.context.annotation.PropertySource;
import org.springframework.util.Assert; import org.springframework.util.Assert;
import com.google.common.collect.Lists; import javax.annotation.PostConstruct;
import com.google.common.primitives.Ints; import java.util.Arrays;
import java.util.List;
/** /**
* Demonstrates setting defaults for @Value annotation. Note that there are no properties * Demonstrates setting defaults for @Value annotation. Note that there are no properties
@ -62,14 +59,13 @@ public class ValuesWithDefaultsApp {
Assert.isTrue(intWithDefaultValue == 42); Assert.isTrue(intWithDefaultValue == 42);
// arrays // arrays
List<String> stringListValues = Lists.newArrayList("one", "two", "three"); List<String> stringListValues = Arrays.asList("one", "two", "three");
Assert.isTrue(Arrays.asList(stringArrayWithDefaults).containsAll(stringListValues)); Assert.isTrue(Arrays.asList(stringArrayWithDefaults).containsAll(stringListValues));
List<Integer> intListValues = Lists.newArrayList(1, 2, 3); List<Integer> intListValues = Arrays.asList(1, 2, 3);
Assert.isTrue(Ints.asList(intArrayWithDefaults).containsAll(intListValues)); Assert.isTrue(Arrays.asList(ArrayUtils.toObject(intArrayWithDefaults)).containsAll(intListValues));
// SpEL // SpEL
Assert.isTrue(spelWithDefaultValue.equals("my default system property value")); Assert.isTrue(spelWithDefaultValue.equals("my default system property value"));
} }
} }

View File

@ -1,6 +1,4 @@
package com.baeldung.properties; package com.baeldung.properties.json;
import java.util.Arrays;
import org.hamcrest.Matchers; import org.hamcrest.Matchers;
import org.junit.Assert; import org.junit.Assert;
@ -10,6 +8,8 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.context.junit4.SpringRunner;
import java.util.Arrays;
@RunWith(SpringRunner.class) @RunWith(SpringRunner.class)
@ContextConfiguration(classes = ConfigPropertiesDemoApplication.class, initializers = JsonPropertyContextInitializer.class) @ContextConfiguration(classes = ConfigPropertiesDemoApplication.class, initializers = JsonPropertyContextInitializer.class)
public class JsonPropertiesIntegrationTest { public class JsonPropertiesIntegrationTest {

View File

@ -1,4 +1,4 @@
package com.baeldung.value; package com.baeldung.properties.value;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;

View File

@ -1,4 +1,4 @@
package com.baeldung.value; package com.baeldung.properties.value;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;

View File

@ -1,4 +1,4 @@
package com.baeldung.value; package com.baeldung.properties.value;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
@ -17,6 +17,6 @@ public class PriorityProviderIntegrationTest {
@Test @Test
public void givenPropertyFileWhenConstructorInjectionUsedThenValueInjected() { public void givenPropertyFileWhenConstructorInjectionUsedThenValueInjected() {
assertThat(priorityProvider.getPriority()).isEqualTo("Properties file"); assertThat(priorityProvider.getPriority()).isEqualTo("high");
} }
} }

View File

@ -5,12 +5,9 @@ This module contains articles about Properties in Spring Boot.
### Relevant Articles: ### Relevant Articles:
- [Reloading Properties Files in Spring](https://www.baeldung.com/spring-reloading-properties) - [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) - [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) - [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 - [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) - [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) - [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) - [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)

View File

@ -1,5 +1,7 @@
package com.baeldung.properties; package com.baeldung.properties;
import com.baeldung.buildproperties.Application;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.ComponentScan;
@ -7,11 +9,10 @@ import org.springframework.context.annotation.ComponentScan;
import com.baeldung.configurationproperties.ConfigProperties; import com.baeldung.configurationproperties.ConfigProperties;
@SpringBootApplication @SpringBootApplication
@ComponentScan(basePackageClasses = { ConfigProperties.class, JsonProperties.class, CustomJsonProperties.class}) @ComponentScan(basePackageClasses = {ConfigProperties.class, AdditionalProperties.class})
public class ConfigPropertiesDemoApplication { public class ConfigPropertiesDemoApplication {
public static void main(String[] args) { public static void main(String[] args) {
new SpringApplicationBuilder(ConfigPropertiesDemoApplication.class).initializers(new JsonPropertyContextInitializer()) SpringApplication.run(ConfigPropertiesDemoApplication.class, args);
.run();
} }
} }