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