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

Folders and files

NameName
Last commit message
Last commit date

Latest commit

author
Marshall Pierce
Feb 27, 2017
ad92d60 · Feb 27, 2017

History

24 Commits
Feb 25, 2017
Feb 25, 2017
Oct 17, 2015
Oct 16, 2015
Feb 27, 2017
Feb 26, 2017
Feb 26, 2017
Feb 25, 2017
Feb 25, 2017
Oct 17, 2015

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