Skip to content

Commit 89fbcce

Browse files
authored
Merge pull request eugenp#5085 from Vizsoro/spring-boot-json-properties
loading json props
2 parents f47113f + 692e1cb commit 89fbcce

15 files changed

Lines changed: 764 additions & 5 deletions
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
package com.baeldung.properties;
2+
3+
import java.util.List;
4+
import java.util.Map;
5+
6+
import javax.validation.constraints.Max;
7+
import javax.validation.constraints.Min;
8+
import javax.validation.constraints.Pattern;
9+
10+
import org.hibernate.validator.constraints.Length;
11+
import org.hibernate.validator.constraints.NotBlank;
12+
import org.springframework.boot.context.properties.ConfigurationProperties;
13+
import org.springframework.context.annotation.Configuration;
14+
import org.springframework.context.annotation.PropertySource;
15+
import org.springframework.validation.annotation.Validated;
16+
17+
@Configuration
18+
@PropertySource("classpath:configprops.properties")
19+
@ConfigurationProperties(prefix = "mail")
20+
@Validated
21+
public class ConfigProperties {
22+
23+
@Validated
24+
public static class Credentials {
25+
26+
@Length(max = 4, min = 1)
27+
private String authMethod;
28+
private String username;
29+
private String password;
30+
31+
public String getAuthMethod() {
32+
return authMethod;
33+
}
34+
35+
public void setAuthMethod(String authMethod) {
36+
this.authMethod = authMethod;
37+
}
38+
39+
public String getUsername() {
40+
return username;
41+
}
42+
43+
public void setUsername(String username) {
44+
this.username = username;
45+
}
46+
47+
public String getPassword() {
48+
return password;
49+
}
50+
51+
public void setPassword(String password) {
52+
this.password = password;
53+
}
54+
}
55+
56+
@NotBlank
57+
private String host;
58+
59+
@Min(1025)
60+
@Max(65536)
61+
private int port;
62+
63+
@Pattern(regexp = "^[a-z0-9._%+-]+@[a-z0-9.-]+\\.[a-z]{2,6}$")
64+
private String from;
65+
66+
private Credentials credentials;
67+
private List<String> defaultRecipients;
68+
private Map<String, String> additionalHeaders;
69+
70+
public String getHost() {
71+
return host;
72+
}
73+
74+
public void setHost(String host) {
75+
this.host = host;
76+
}
77+
78+
public int getPort() {
79+
return port;
80+
}
81+
82+
public void setPort(int port) {
83+
this.port = port;
84+
}
85+
86+
public String getFrom() {
87+
return from;
88+
}
89+
90+
public void setFrom(String from) {
91+
this.from = from;
92+
}
93+
94+
public Credentials getCredentials() {
95+
return credentials;
96+
}
97+
98+
public void setCredentials(Credentials credentials) {
99+
this.credentials = credentials;
100+
}
101+
102+
public List<String> getDefaultRecipients() {
103+
return defaultRecipients;
104+
}
105+
106+
public void setDefaultRecipients(List<String> defaultRecipients) {
107+
this.defaultRecipients = defaultRecipients;
108+
}
109+
110+
public Map<String, String> getAdditionalHeaders() {
111+
return additionalHeaders;
112+
}
113+
114+
public void setAdditionalHeaders(Map<String, String> additionalHeaders) {
115+
this.additionalHeaders = additionalHeaders;
116+
}
117+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.baeldung.properties;
2+
3+
import org.springframework.boot.autoconfigure.SpringBootApplication;
4+
import org.springframework.boot.builder.SpringApplicationBuilder;
5+
import org.springframework.context.annotation.ComponentScan;
6+
7+
@SpringBootApplication
8+
@ComponentScan(basePackageClasses = { ConfigProperties.class, JsonProperties.class, CustomJsonProperties.class })
9+
public class ConfigPropertiesDemoApplication {
10+
public static void main(String[] args) {
11+
new SpringApplicationBuilder(ConfigPropertiesDemoApplication.class).initializers(new JsonPropertyContextInitializer())
12+
.run();
13+
}
14+
15+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package com.baeldung.properties;
2+
3+
import org.springframework.boot.context.properties.ConfigurationProperties;
4+
import org.springframework.stereotype.Component;
5+
6+
@Component
7+
@ConfigurationProperties(prefix = "custom")
8+
public class CustomJsonProperties {
9+
10+
private String host;
11+
12+
private int port;
13+
14+
private boolean resend;
15+
16+
private Person sender;
17+
18+
public static class Person {
19+
20+
private String name;
21+
private String address;
22+
23+
public String getName() {
24+
return name;
25+
}
26+
27+
public void setName(String name) {
28+
this.name = name;
29+
}
30+
31+
public String getAddress() {
32+
return address;
33+
}
34+
35+
public void setAddress(String address) {
36+
this.address = address;
37+
}
38+
}
39+
40+
public String getHost() {
41+
return host;
42+
}
43+
44+
public void setHost(String host) {
45+
this.host = host;
46+
}
47+
48+
public int getPort() {
49+
return port;
50+
}
51+
52+
public void setPort(int port) {
53+
this.port = port;
54+
}
55+
56+
public boolean isResend() {
57+
return resend;
58+
}
59+
60+
public void setResend(boolean resend) {
61+
this.resend = resend;
62+
}
63+
64+
public Person getSender() {
65+
return sender;
66+
}
67+
68+
public void setSender(Person sender) {
69+
this.sender = sender;
70+
}
71+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package com.baeldung.properties;
2+
3+
import java.util.LinkedHashMap;
4+
import java.util.List;
5+
6+
import org.springframework.boot.context.properties.ConfigurationProperties;
7+
import org.springframework.context.annotation.PropertySource;
8+
import org.springframework.stereotype.Component;
9+
10+
@Component
11+
@PropertySource(value = "classpath:configprops.json", factory = JsonPropertySourceFactory.class)
12+
@ConfigurationProperties
13+
public class JsonProperties {
14+
15+
private String host;
16+
17+
private int port;
18+
19+
private boolean resend;
20+
21+
private List<String> topics;
22+
23+
private LinkedHashMap<String, ?> sender;
24+
25+
public LinkedHashMap<String, ?> getSender() {
26+
return sender;
27+
}
28+
29+
public void setSender(LinkedHashMap<String, ?> sender) {
30+
this.sender = sender;
31+
}
32+
33+
public List<String> getTopics() {
34+
return topics;
35+
}
36+
37+
public void setTopics(List<String> topics) {
38+
this.topics = topics;
39+
}
40+
41+
public int getPort() {
42+
return port;
43+
}
44+
45+
public void setPort(int port) {
46+
this.port = port;
47+
}
48+
49+
public boolean isResend() {
50+
return resend;
51+
}
52+
53+
public void setResend(boolean resend) {
54+
this.resend = resend;
55+
}
56+
57+
public String getHost() {
58+
return host;
59+
}
60+
61+
public void setHost(String host) {
62+
this.host = host;
63+
}
64+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package com.baeldung.properties;
2+
3+
import java.io.IOException;
4+
import java.util.Collection;
5+
import java.util.Collections;
6+
import java.util.LinkedHashMap;
7+
import java.util.List;
8+
import java.util.Map;
9+
import java.util.Optional;
10+
import java.util.Set;
11+
import java.util.stream.Collectors;
12+
13+
import org.springframework.context.ApplicationContextInitializer;
14+
import org.springframework.context.ConfigurableApplicationContext;
15+
import org.springframework.core.env.MapPropertySource;
16+
import org.springframework.core.env.PropertySource;
17+
import org.springframework.core.io.Resource;
18+
19+
import com.fasterxml.jackson.databind.ObjectMapper;
20+
21+
public class JsonPropertyContextInitializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {
22+
23+
private final static String CUSTOM_PREFIX = "custom.";
24+
25+
@Override
26+
@SuppressWarnings("unchecked")
27+
public void initialize(ConfigurableApplicationContext configurableApplicationContext) {
28+
try {
29+
Resource resource = configurableApplicationContext.getResource("classpath:configprops.json");
30+
Map readValue = new ObjectMapper().readValue(resource.getInputStream(), Map.class);
31+
Set<Map.Entry> set = readValue.entrySet();
32+
List<MapPropertySource> propertySources = convertEntrySet(set, Optional.empty());
33+
for (PropertySource propertySource : propertySources) {
34+
configurableApplicationContext.getEnvironment()
35+
.getPropertySources()
36+
.addFirst(propertySource);
37+
}
38+
} catch (IOException e) {
39+
throw new RuntimeException(e);
40+
}
41+
}
42+
43+
private static List<MapPropertySource> convertEntrySet(Set<Map.Entry> entrySet, Optional<String> parentKey) {
44+
return entrySet.stream()
45+
.map((Map.Entry e) -> convertToPropertySourceList(e, parentKey))
46+
.flatMap(Collection::stream)
47+
.collect(Collectors.toList());
48+
}
49+
50+
private static List<MapPropertySource> convertToPropertySourceList(Map.Entry e, Optional<String> parentKey) {
51+
String key = parentKey.map(s -> s + ".")
52+
.orElse("") + (String) e.getKey();
53+
Object value = e.getValue();
54+
return covertToPropertySourceList(key, value);
55+
}
56+
57+
@SuppressWarnings("unchecked")
58+
private static List<MapPropertySource> covertToPropertySourceList(String key, Object value) {
59+
if (value instanceof LinkedHashMap) {
60+
LinkedHashMap map = (LinkedHashMap) value;
61+
Set<Map.Entry> entrySet = map.entrySet();
62+
return convertEntrySet(entrySet, Optional.ofNullable(key));
63+
}
64+
String finalKey = CUSTOM_PREFIX + key;
65+
return Collections.singletonList(new MapPropertySource(finalKey, Collections.singletonMap(finalKey, value)));
66+
}
67+
68+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.baeldung.properties;
2+
3+
import java.io.IOException;
4+
import java.util.Map;
5+
6+
import org.springframework.core.env.MapPropertySource;
7+
import org.springframework.core.env.PropertySource;
8+
import org.springframework.core.io.support.EncodedResource;
9+
import org.springframework.core.io.support.PropertySourceFactory;
10+
11+
import com.fasterxml.jackson.databind.ObjectMapper;
12+
13+
public class JsonPropertySourceFactory implements PropertySourceFactory {
14+
15+
@Override
16+
public PropertySource<?> createPropertySource(String name, EncodedResource resource) throws IOException {
17+
Map readValue = new ObjectMapper().readValue(resource.getInputStream(), Map.class);
18+
return new MapPropertySource("json-property", readValue);
19+
}
20+
21+
}
Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
package org.baeldung.properties;
22

3-
import org.springframework.boot.SpringApplication;
4-
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
3+
import org.springframework.boot.autoconfigure.SpringBootApplication;
4+
import org.springframework.boot.builder.SpringApplicationBuilder;
55
import org.springframework.context.annotation.ComponentScan;
66

7-
@EnableAutoConfiguration
8-
@ComponentScan(basePackageClasses = ConfigProperties.class)
7+
@SpringBootApplication
8+
@ComponentScan(basePackageClasses = { ConfigProperties.class, JsonProperties.class, CustomJsonProperties.class })
99
public class ConfigPropertiesDemoApplication {
1010
public static void main(String[] args) {
11-
SpringApplication.run(ConfigPropertiesDemoApplication.class);
11+
new SpringApplicationBuilder(ConfigPropertiesDemoApplication.class).initializers(new JsonPropertyContextInitializer())
12+
.run();
1213
}
14+
1315
}

0 commit comments

Comments
 (0)