@Override public void postProcessBeanFactory( ConfigurableListableBeanFactory beanFactory) throws BeansException { try { JsonSimpleConfig config = new JsonSimpleConfig(); Properties configProperties = mapJsonToProperties(config); addConfigPropertiesToLocalPropertiesArray(configProperties); Properties mergedProps = mergeProperties(); // Convert the merged properties, if necessary. convertProperties(mergedProps); // Let the subclass process the properties. processProperties(beanFactory, mergedProps); } catch (IOException ex) { throw new BeanInitializationException("Could not load properties", ex); } }
private void processJsonObjectToProperties(JsonObject jsonObject, String parentKey, Properties properties) { for (Object o : jsonObject.keySet()) { String key = (String) o; Object value = jsonObject.get(key); if (value instanceof String) { properties.put(parentKey + "." + key, value); } else if (value instanceof JsonObject) { processJsonObjectToProperties((JsonObject) value, parentKey + "." + key, properties); } } } }
/** * Map the system-config.json to a properties file. Nested attributes are * mapped in dot notation. * * e.g. { "config": { "configItem": "sample config" } } becomes * config.configItem=sample config in the properties configuration * * * @param config * @return */ private Properties mapJsonToProperties(JsonSimpleConfig config) { Properties properties = new Properties(); JsonObject obj = config.getJsonObject(); for (Object o : obj.keySet()) { String key = (String) o; Object value = obj.get(key); if (value instanceof String) { properties.put(key, value); } else if (value instanceof JsonObject) { processJsonObjectToProperties((JsonObject) value, key, properties); } } return properties; }