java Guice 和一般应用程序配置
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4805874/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Guice and general application configuration
提问by mjn
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.
对于用 Java 编写的监控软件,我考虑使用 Google Guice 作为 DI 提供程序。项目需要从外部资源(文件或数据库)加载其配置。该应用程序旨在以独立模式或在 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:
我看到两个选项:
- to use another library, for example Apache Commons Configuration, which supports file and JDBC configuration sources (and many other)
- 使用另一个库,例如Apache Commons Configuration,它支持文件和 JDBC 配置源(以及许多其他)
or
或者
- to use a file based addon for Guice like guice-xml-configto 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? Which advantages and disadvantages would you consider the most important ones?
您会建议将 Guice 用于这两个任务,还是将一般应用程序配置与依赖注入分开?您认为哪些优点和缺点最重要?
回答by Landei
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")
.
回答by EIIPII
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. In code it helps to see where is You configuration parameters used:
您将获得一个 @Configuration 注释和几个配置提供程序。在代码中,它有助于查看您使用的配置参数的位置:
@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
回答by Yves Galante
Try Guice configurationavailable on maven central, it's support Properties, HOCON and JSON format.
试试maven central 上的Guice 配置,它支持 Properties、HOCON 和 JSON 格式。
You can inject properties from file application.confto 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 ConfigurationModuleas
您必须将模块ConfigurationModule安装为
public class GuiceModule extends AbstractModule {
@Override
protected void configure() {
install(ConfigurationModule.create());
requestInjection(Service.class);
}
}
回答by reap
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 Configurationand 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");
}
});
}