简体   繁体   English

Guice 和一般应用程序配置

[英]Guice and general application configuration

For a monitoring software written in Java I consider using Google Guice as DI provider.对于用 Java 编写的监控软件,我考虑使用 Google Guice 作为 DI 提供程序。 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.该应用程序旨在以独立模式或在 servlet 容器中运行。

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).目前配置不包含依赖注入的绑定或参数,只有一些全局应用程序设置(JDBC 连接定义和关联的数据库管理/监视对象)。

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).为 Guice 使用基于文件的插件,如guice-xml-config来存储应用程序选项(如果有必要,这将允许稍后配置 DI 部分)。

Would you recommend to use Guice for both tasks, or keep the general application configuration separate from the dependency injection?您会建议将 Guice 用于这两个任务,还是将一般应用程序配置与依赖注入分开? Which advantages and disadvantages would you consider the most important ones?您认为哪些优点和缺点最重要?

It's straightforward to slurp a property file in a Guice module:在 Guice 模块中 slurp 属性文件很简单:

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") .顺便说一句,您可以通过使用@Named("myKey")对其进行注释来获取注入的属性。

Check the governator library:检查州长库:

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

You will get a @Configuration annotation and several configuration providers.您将获得一个 @Configuration 注释和几个配置提供程序。 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 https://github.com/Netflix/governator/wiki/Configuration-Mapping#configuration-documentation

Try Guice configuration available on maven central, it's support Properties, HOCON and JSON format.试试 maven central 上的Guice 配置,它支持 Properties、HOCON 和 JSON 格式。

You can inject properties from file application.conf to your service as :您可以将文件application.conf 中的属性注入到您的服务中:

@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您必须将模块ConfigurationModule安装为

public class GuiceModule extends AbstractModule {
    @Override
    protected void configure() {
        install(ConfigurationModule.create());
        requestInjection(Service.class);
    }
}

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.我们已经选择了 Guice 作为 DI 框架,为了简单起见,我们还想在配置中使用它。

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?我们最终使用Apache Commons Configuration从属性文件中读取配置并将它们绑定到 Guice 注入器,如 Guice FAQ 如何注入配置参数? . .

@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.重新加载 Commons Configuration 支持的配置也很容易实现到 Guice 注入。

@Override public void configure() {
    bind(String.class).annotatedWith(ConfigurationAnnotation.class)
        .toProvider(new Provider<String>() {
            public String get() {
                return configuration.getString("configurationValue");
            }
    });    
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM