[BAEL-15990] - Moved properties articles

This commit is contained in:
amit2103 2019-08-11 18:30:21 +05:30
parent 32127cc5b5
commit 84364759d6
84 changed files with 55 additions and 516 deletions

View File

@ -11,7 +11,6 @@ The "REST With Spring" Classes: http://bit.ly/restwithspring
### Relevant articles:
- [Guide to Spring @Autowired](http://www.baeldung.com/spring-autowire)
- [Properties with Spring and Spring Boot](http://www.baeldung.com/properties-with-spring) - checkout the `org.baeldung.properties` package for all scenarios of properties injection and usage
- [Spring Profiles](http://www.baeldung.com/spring-profiles)
- [A Spring Custom Annotation for a Better DAO](http://www.baeldung.com/spring-annotation-bean-pre-processor)
- [Whats New in Spring 4.3?](http://www.baeldung.com/whats-new-in-spring-4-3)

View File

@ -1,2 +1,10 @@
### Relevant Articles:
- [Reloading Properties in Spring](https://www.baeldung.com/reloading-properties-files-in-spring/)
- [Guide to @ConfigurationProperties in Spring Boot](http://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](http://www.baeldung.com/properties-with-spring) - checkout the `org.baeldung.properties` package for all scenarios of properties injection and usage
- [A Quick Guide to Spring @Value](http://www.baeldung.com/spring-value-annotation)
- [Spring YAML Configuration](http://www.baeldung.com/spring-yaml)
- [Using Spring @Value with Defaults](http://www.baeldung.com/spring-value-defaults)
- [How to Inject a Property Value Into a Class Not Managed by Spring?](http://www.baeldung.com/inject-properties-value-non-spring-class)

View File

@ -37,6 +37,16 @@
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>${guava.version}</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
<version>${httpcore.version}</version>
</dependency>
</dependencies>
<dependencyManagement>
@ -99,6 +109,8 @@
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.SR1</spring-cloud.version>
<commons-configuration.version>1.10</commons-configuration.version>
<guava.version>20.0</guava.version>
<httpcore.version>4.4.11</httpcore.version>
</properties>
</project>

View File

@ -1,4 +1,4 @@
package org.baeldung.properties;
package com.baeldung.properties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;

View File

@ -1,7 +1,9 @@
package org.baeldung.properties;
package com.baeldung.properties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
@Configuration
@ConfigurationProperties(prefix = "additional")
public class AdditionalProperties {

View File

@ -10,6 +10,7 @@ import javax.validation.constraints.Pattern;
import org.hibernate.validator.constraints.Length;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.validation.annotation.Validated;
@ -54,7 +55,7 @@ public class ConfigProperties {
}
@NotBlank
private String host;
private String hostName;
@Min(1025)
@Max(65536)
@ -67,12 +68,12 @@ public class ConfigProperties {
private List<String> defaultRecipients;
private Map<String, String> additionalHeaders;
public String getHost() {
return host;
public String getHostName() {
return hostName;
}
public void setHost(String host) {
this.host = host;
public void setHostName(String hostName) {
this.hostName = hostName;
}
public int getPort() {
@ -114,4 +115,10 @@ public class ConfigProperties {
public void setAdditionalHeaders(Map<String, String> additionalHeaders) {
this.additionalHeaders = additionalHeaders;
}
@Bean
@ConfigurationProperties(prefix = "item")
public Item item(){
return new Item();
}
}

View File

@ -1,4 +1,4 @@
package org.baeldung.properties;
package com.baeldung.properties;
public class Item {

View File

@ -22,3 +22,6 @@ item.name=Item name
item.size=42
#Additional properties
additional.unit=km
additional.max=100

View File

@ -0,0 +1,2 @@
conversion.timeInDefaultUnit=10
conversion.timeInNano=9ns

View File

@ -1,8 +1,5 @@
package com.baeldung.properties;
import org.baeldung.properties.AdditionalProperties;
import org.baeldung.properties.ConfigProperties;
import org.baeldung.properties.ConfigPropertiesDemoApplication;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;

View File

@ -0,0 +1,11 @@
###### time unit
conversion.timeInDefaultUnit=10
conversion.timeInNano=9ns
conversion.timeInDays=2
###### data size
conversion.sizeInDefaultUnit=300
conversion.sizeInGB=2GB
conversion.sizeInTB=4
conversion.employee=john,2000

View File

@ -14,7 +14,6 @@ The "REST With Spring" Classes: http://bit.ly/restwithspring
- [Custom Information in Spring Boot Info Endpoint](http://www.baeldung.com/spring-boot-info-actuator-custom)
- [Using @JsonComponent in Spring Boot](http://www.baeldung.com/spring-boot-jsoncomponent)
- [Testing in Spring Boot](http://www.baeldung.com/spring-boot-testing)
- [Guide to @ConfigurationProperties in Spring Boot](http://www.baeldung.com/configuration-properties-in-spring-boot)
- [How to Get All Spring-Managed Beans?](http://www.baeldung.com/spring-show-all-beans)
- [Spring Boot and Togglz Aspect](http://www.baeldung.com/spring-togglz)
- [Getting Started with GraphQL and Spring Boot](http://www.baeldung.com/spring-graphql)
@ -29,9 +28,7 @@ The "REST With Spring" Classes: http://bit.ly/restwithspring
- [Spring Shutdown Callbacks](http://www.baeldung.com/spring-shutdown-callbacks)
- [Container Configuration in Spring Boot 2](http://www.baeldung.com/embeddedservletcontainercustomizer-configurableembeddedservletcontainer-spring-boot)
- [Introduction to Chaos Monkey](https://www.baeldung.com/spring-boot-chaos-monkey)
- [Load Spring Boot Properties From a JSON File](https://www.baeldung.com/spring-boot-json-properties)
- [Display Auto-Configuration Report in Spring Boot](https://www.baeldung.com/spring-boot-auto-configuration-report)
- [Injecting Git Information Into Spring](https://www.baeldung.com/spring-git-information)
- [Validation in Spring Boot](https://www.baeldung.com/spring-boot-bean-validation)
- [Guide to Creating and Running a Jar File in Java](https://www.baeldung.com/java-create-jar)
- [Guide to @EnableConfigurationProperties](https://www.baeldung.com/spring-enable-config-properties)

View File

@ -1,90 +0,0 @@
package org.baeldung.properties;
import java.util.List;
import java.util.Map;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Pattern;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.validation.annotation.Validated;
@Configuration
@PropertySource("classpath:configprops.properties")
@ConfigurationProperties(prefix = "mail")
@Validated
public class ConfigProperties {
@NotBlank
private String hostName;
@Min(1025)
@Max(65536)
private int port;
@Pattern(regexp = "^[a-z0-9._%+-]+@[a-z0-9.-]+\\.[a-z]{2,6}$")
private String from;
private List<String> defaultRecipients;
private Map<String, String> additionalHeaders;
private Credentials credentials;
public String getHostName() {
return hostName;
}
public void setHostName(String hostName) {
this.hostName = hostName;
}
public int getPort() {
return port;
}
public void setPort(int port) {
this.port = port;
}
public String getFrom() {
return from;
}
public void setFrom(String from) {
this.from = from;
}
public List<String> getDefaultRecipients() {
return defaultRecipients;
}
public void setDefaultRecipients(List<String> defaultRecipients) {
this.defaultRecipients = defaultRecipients;
}
public Map<String, String> getAdditionalHeaders() {
return additionalHeaders;
}
public void setAdditionalHeaders(Map<String, String> additionalHeaders) {
this.additionalHeaders = additionalHeaders;
}
public Credentials getCredentials() {
return credentials;
}
public void setCredentials(Credentials credentials) {
this.credentials = credentials;
}
@Bean
@ConfigurationProperties(prefix = "item")
public Item item(){
return new Item();
}
}

View File

@ -1,18 +0,0 @@
package org.baeldung.properties;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.context.annotation.ComponentScan;
@SpringBootApplication
@ComponentScan(basePackageClasses = {ConfigProperties.class,
JsonProperties.class,
CustomJsonProperties.class,
AdditionalConfiguration.class})
public class ConfigPropertiesDemoApplication {
public static void main(String[] args) {
new SpringApplicationBuilder(ConfigPropertiesDemoApplication.class).initializers(new JsonPropertyContextInitializer())
.run();
}
}

View File

@ -1,37 +0,0 @@
package org.baeldung.properties;
import org.hibernate.validator.constraints.Length;
import org.springframework.validation.annotation.Validated;
@Validated
public class Credentials {
@Length(max = 4, min = 1)
private String authMethod;
private String username;
private String password;
public String getAuthMethod() {
return authMethod;
}
public void setAuthMethod(String authMethod) {
this.authMethod = authMethod;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}

View File

@ -1,71 +0,0 @@
package org.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;
}
}

View File

@ -1,64 +0,0 @@
package org.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<String> topics;
private LinkedHashMap<String, ?> sender;
public LinkedHashMap<String, ?> getSender() {
return sender;
}
public void setSender(LinkedHashMap<String, ?> sender) {
this.sender = sender;
}
public List<String> getTopics() {
return topics;
}
public void setTopics(List<String> 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;
}
}

View File

@ -1,68 +0,0 @@
package org.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<ConfigurableApplicationContext> {
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<Map.Entry> set = readValue.entrySet();
List<MapPropertySource> propertySources = convertEntrySet(set, Optional.empty());
for (PropertySource propertySource : propertySources) {
configurableApplicationContext.getEnvironment()
.getPropertySources()
.addFirst(propertySource);
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
private static List<MapPropertySource> convertEntrySet(Set<Map.Entry> entrySet, Optional<String> parentKey) {
return entrySet.stream()
.map((Map.Entry e) -> convertToPropertySourceList(e, parentKey))
.flatMap(Collection::stream)
.collect(Collectors.toList());
}
private static List<MapPropertySource> convertToPropertySourceList(Map.Entry e, Optional<String> parentKey) {
String key = parentKey.map(s -> s + ".")
.orElse("") + (String) e.getKey();
Object value = e.getValue();
return covertToPropertySourceList(key, value);
}
@SuppressWarnings("unchecked")
private static List<MapPropertySource> covertToPropertySourceList(String key, Object value) {
if (value instanceof LinkedHashMap) {
LinkedHashMap map = (LinkedHashMap) value;
Set<Map.Entry> entrySet = map.entrySet();
return convertEntrySet(entrySet, Optional.ofNullable(key));
}
String finalKey = CUSTOM_PREFIX + key;
return Collections.singletonList(new MapPropertySource(finalKey, Collections.singletonMap(finalKey, value)));
}
}

View File

@ -1,21 +0,0 @@
package org.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);
}
}

View File

@ -1,63 +0,0 @@
package com.baeldung.properties;
import java.util.Arrays;
import org.baeldung.properties.ConfigPropertiesDemoApplication;
import org.baeldung.properties.CustomJsonProperties;
import org.baeldung.properties.JsonProperties;
import org.baeldung.properties.JsonPropertyContextInitializer;
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());
}
}

View File

@ -1,63 +0,0 @@
package org.baeldung.properties;
import java.util.List;
import java.util.Map;
import org.junit.Assert;
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.TestPropertySource;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = ConfigPropertiesDemoApplication.class)
@TestPropertySource("classpath:configprops-test.properties")
public class ConfigPropertiesIntegrationTest {
@Autowired
private ConfigProperties properties;
@Test
public void whenSimplePropertyQueriedthenReturnsProperty() throws Exception {
Assert.assertEquals("Incorrectly bound hostName property", "host@mail.com", properties.getHostName());
Assert.assertEquals("Incorrectly bound port property", 9000, properties.getPort());
Assert.assertEquals("Incorrectly bound from property", "mailer@mail.com", properties.getFrom());
}
@Test
public void whenListPropertyQueriedthenReturnsProperty() throws Exception {
List<String> defaultRecipients = properties.getDefaultRecipients();
Assert.assertTrue("Couldn't bind list property!", defaultRecipients.size() == 2);
Assert.assertTrue("Incorrectly bound list property. Expected 2 entries!", defaultRecipients.size() == 2);
Assert.assertEquals("Incorrectly bound list[0] property", "admin@mail.com", defaultRecipients.get(0));
Assert.assertEquals("Incorrectly bound list[1] property", "owner@mail.com", defaultRecipients.get(1));
}
@Test
public void whenMapPropertyQueriedthenReturnsProperty() throws Exception {
Map<String, String> additionalHeaders = properties.getAdditionalHeaders();
Assert.assertTrue("Couldn't bind map property!", additionalHeaders != null);
Assert.assertTrue("Incorrectly bound map property. Expected 3 Entries!", additionalHeaders.size() == 3);
Assert.assertEquals("Incorrectly bound map[redelivery] property", "true", additionalHeaders.get("redelivery"));
Assert.assertEquals("Incorrectly bound map[secure] property", "true", additionalHeaders.get("secure"));
Assert.assertEquals("Incorrectly bound map[p3] property", "value", additionalHeaders.get("p3"));
}
@Test
public void whenObjectPropertyQueriedthenReturnsProperty() throws Exception {
Credentials credentials = properties.getCredentials();
Assert.assertTrue("Couldn't bind map property!", credentials != null);
Assert.assertEquals("Incorrectly bound object property, authMethod", "SHA1", credentials.getAuthMethod());
Assert.assertEquals("Incorrectly bound object property, username", "john", credentials.getUsername());
Assert.assertEquals("Incorrectly bound object property, password", "password", credentials.getPassword());
}
@Test
public void whenBeanMethodAnnotatedThenPropertiesCorrectlyBound(){
Item item = properties.item();
Assert.assertEquals("Incorrectly bound object property, item.name","Test item name", item.getName());
Assert.assertEquals("Incorrectly bound object property, item.size", 21, item.getSize());
}
}

View File

@ -4,14 +4,10 @@
- [How to use the Spring FactoryBean?](http://www.baeldung.com/spring-factorybean)
- [Constructor Dependency Injection in Spring](http://www.baeldung.com/constructor-injection-in-spring)
- [Constructor Injection in Spring with Lombok](http://www.baeldung.com/spring-injection-lombok)
- [A Quick Guide to Spring @Value](http://www.baeldung.com/spring-value-annotation)
- [Spring YAML Configuration](http://www.baeldung.com/spring-yaml)
- [Introduction to Springs StreamUtils](http://www.baeldung.com/spring-stream-utils)
- [Using Spring @Value with Defaults](http://www.baeldung.com/spring-value-defaults)
- [XML-Based Injection in Spring](http://www.baeldung.com/spring-xml-injection)
- [A Quick Guide to the Spring @Lazy Annotation](http://www.baeldung.com/spring-lazy-annotation)
- [Injecting Prototype Beans into a Singleton Instance in Spring](http://www.baeldung.com/spring-inject-prototype-bean-into-singleton)
- [How to Inject a Property Value Into a Class Not Managed by Spring?](http://www.baeldung.com/inject-properties-value-non-spring-class)
- [@Lookup Annotation in Spring](http://www.baeldung.com/spring-lookup)
- [BeanNameAware and BeanFactoryAware Interfaces in Spring](http://www.baeldung.com/spring-bean-name-factory-aware)
- [Spring Injecting Collections](http://www.baeldung.com/spring-injecting-collections)