public void init(ConfigurableEnvironment environment) { try { configuration = (Configuration) getContext().getBean("configuration"); } catch (BeansException e) { if(environment != null){ Map<String, Object> allProperties = getAllProperties(); configuration = new MapConfiguration(allProperties); }else{ throw new RuntimeException("can't load commons config. error: " + e); } } }
public Map<String,Object> getAllProperties() { Map<String,Object> result = new HashMap<>(); environment.getPropertySources().forEach( ps -> addAll( result, getAllProperties( ps ) ) ); return result; }
public Map<String,Object> getAllProperties( PropertySource<?> aPropSource ) { Map<String,Object> result = new HashMap<>(); if ( aPropSource instanceof CompositePropertySource) { CompositePropertySource cps = (CompositePropertySource) aPropSource; cps.getPropertySources().forEach( ps -> addAll( result, getAllProperties( ps ) ) ); return result; } if ( aPropSource instanceof EnumerablePropertySource<?> ) { EnumerablePropertySource<?> ps = (EnumerablePropertySource<?>) aPropSource; Arrays.asList( ps.getPropertyNames() ).forEach(key -> result.put( key, ps.getProperty( key ) ) ); return result; } return result; }