Skip to content

A library to make it easy to inject configuration data into your classes with Guice and config-magic.

License

Notifications You must be signed in to change notification settings

palominolabs/config-inject

Repository files navigation

config-inject

Build Status Download

config-inject is a library to make it easy to inject configuration data into your classes with Guice and config-magic.

Artifacts are published to Bintray, so use jcenter() in your Gradle dependencies block (or analogous Maven config).

You can stack multiple sources of configuration data.

ConfigModuleBuilder builder = new ConfigModuleBuilder();
// load from a file
builder.addPropertiesFile(new File("/foo/bar"))
        // or input stream
       .addPropertiesInputStream(getClass().getResourceAsStream("/some/other/config")
       // or system properties, or any other commons-configuration Configuration
       .addConfiguration(new SystemConfiguration());

Once you've set up your config data, use config-magic to access it. Define a config interface or abstract class:

public interface SomeAppConfig {
    @Config("com.app.service.host")
    @Default("localhost")
    String getHost();

    @Config("com.app.service.port")
    @Default("1234")
    int getPort();
}

Bind up the necessary stuff in Guice:

Guice.createInjector(builder.build(), new AbstractModule() {
        @Override
        protected void configure() {
            ConfigModule.bindConfigBean(binder(), SomeAppConfig.class);
        }
});

Now you can inject the config interface into your classes as needed.

class SomeClass {
    @Inject
    SomeClass(SomeAppConfig config) {
        // do something configurable
    }
}

About

A library to make it easy to inject configuration data into your classes with Guice and config-magic.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages