Java simple configuration mechanism with environment variable substitution/interpolation.
This is basically like Dropwizard configuration functionality, but standalone.
It uses Jackson for actually reading the config file (like Dropwizard), so you get all the amazing features of Jackson, on top of commons text string substitution functionality to pre-process the config file (also like Dropwizard).
The gist is so small and could probably be missing a feature or two that you desire. The thing is, after trying other dominent libraries, I didn't find anything that gave me the functionality that I got from Jackson + StringSubstitutor in 20 lines of code.
- Map configurations to POJOs, with all Jackson mapping goodness
- Supports variable substitution, from environment and you can tweak to add more sources
- Supports YAML, JSON and all the other formats Jackson supports
- Given a config file like this
config.yml
:
database:
host: ${DB_HOST:-localhost}
user: ${DB_USER:-defaultuser}
password: ${DB_PASSWORD}
... other config here ...
- You write POJOs reflecting your config. I'm demonstrating nested objects so it will be 2 POJOs, I'm also using Lombok here for brevity:
@Getter
public class AppConfiguration {
private DatabaseConfiguration database;
// ... other config fields here ...
}
@Getter
public class DatabaseConfiguration {
private String host;
private String user;
private String password;
}
- Now load your configuration into those POJOs:
ConfigurationLoader configLoader = new ConfigurationLoader();
AppConfiguration config =
configLoader.loadConfiguration(
new File("config.yml"), AppConfiguration.class);
Done
Thanks, it would be nice if you included the imports/required dependencies.