Guice 从系统属性加载属性

Guice load properties from System Properties

我正在使用没有任何 spring 依赖项的 Guice。

有没有办法在模块中将系统属性传递给 java (java -Dfoo="asda")。 目标是从 jar 文件外部加载属性。

目前大多数示例都使用预先存在的属性文件

类似于这个问题: Guice properties injection

使用 System.getProperty() 作为临时解决方案。但希望有更优雅的方式

使用Names.bindProperties()

用法很简单:

@Override protected void configure() {
  Names.bindProperties(binder(), System.getProperties());
}

然后每次注入 @Named("java.user.home") String 你会得到 System.getProperty("java.user.home") 的值。