Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save danieldbower/2702482 to your computer and use it in GitHub Desktop.
Save danieldbower/2702482 to your computer and use it in GitHub Desktop.
ApacheCommonsConfigPropertySource for using Apache Commons Config with Spring Property Sources
package com.bowerstudios.config;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.apache.commons.configuration.Configuration;
import org.springframework.core.env.EnumerablePropertySource;
/**
* Allow use of Apache Commons Configuration Objects as Spring PropertySources
*/
public class ApacheCommonsConfigPropertySource extends
EnumerablePropertySource<Configuration> {
public ApacheCommonsConfigPropertySource(final String name,
final Configuration source) {
super(name, source);
}
@Override
public Object getProperty(final String arg0) {
return source.getProperty(arg0);
}
@Override
public String[] getPropertyNames() {
final List<String> keys = new ArrayList<String>();
final Iterator<String> keysIterator = source.getKeys();
while (keysIterator.hasNext()) {
keys.add(keysIterator.next());
}
return keys.toArray(new String[keys.size()]);
}
}
@deki
Copy link

deki commented Apr 19, 2016

Still works with commons-configuration 2, just update the imports.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment