Guice and general application configuration

2019-03-12 05:08发布

For a monitoring software written in Java I consider using Google Guice as DI provider. The project needs to load its configuration from an external resource (file or database). The application is designed to run in standalone mode or in a servlet container.

At the moment the configuration does not contain bindings or parameters for dependency injection, only some global application settings (JDBC connection definitions and associated database management/monitoring objects).

I see two options:

or

  • to use a file based addon for Guice like guice-xml-config to store the application options (this would allow to configure the DI part later if it becomes necessary).

Would you recommend to use Guice for both tasks, or keep the general application configuration separate from the dependency injection? Which advantages and disadvantages would you consider the most important ones?

4条回答
Fickle 薄情
2楼-- · 2019-03-12 05:30

It's straightforward to slurp a property file in a Guice module:

public class MyModule extends AbstractModule {

  @Override
  protected void configure() {
    try {
        Properties properties = new Properties();
        properties.load(new FileReader("my.properties"));
        Names.bindProperties(binder(), properties);
    } catch (IOException ex) {
        //...
    }
  }
} 

Later it's easy to switch from Properties to other config sources.

[Edit]

BTW, you can get the injected properties by annotating it with @Named("myKey").

查看更多
看我几分像从前
3楼-- · 2019-03-12 05:32

I ran into the same problem in my own project. We had already chosen Guice as DI-framework and to keep things simple wanted to use it also with configuration.

We ended up reading the configuration from properties file using Apache Commons Configuration and binding them to Guice injector like suggested in Guice FAQ How do I inject configuration parameters?.

@Override public void configure() {
    bindConstant().annotatedWith(ConfigurationAnnotation.class)
        .to(configuration.getString("configurationValue"));    
}

Reloading of configuration supported by Commons Configuration is also quite easy implement into Guice injection.

@Override public void configure() {
    bind(String.class).annotatedWith(ConfigurationAnnotation.class)
        .toProvider(new Provider<String>() {
            public String get() {
                return configuration.getString("configurationValue");
            }
    });    
}
查看更多
ゆ 、 Hurt°
4楼-- · 2019-03-12 05:37

Try Guice configuration available on maven central, it's support Properties, HOCON and JSON format.

You can inject properties from file application.conf to your service as :

@BindConfig(value = "application")
public class Service {

    @InjectConfig
    private int port;

    @InjectConfig
    private String url;

    @InjectConfig
    private Optional<Integer> timeout;

    @InjectConfig("services")
    private ServiceConfiguration services;
}

You must install the modules ConfigurationModule as

public class GuiceModule extends AbstractModule {
    @Override
    protected void configure() {
        install(ConfigurationModule.create());
        requestInjection(Service.class);
    }
}
查看更多
霸刀☆藐视天下
5楼-- · 2019-03-12 05:55

Check the governator library:

https://github.com/Netflix/governator/wiki/Configuration-Mapping

You will get a @Configuration annotation and several configuration providers. In code it helps to see where is You configuration parameters used:

@Configuration("configs.qty.things")
private int   numberOfThings = 10;

Also, You will get a nice configuration report on startup:

https://github.com/Netflix/governator/wiki/Configuration-Mapping#configuration-documentation

查看更多
登录 后发表回答