模块输出上的Guice Names.bindProperties(binder(),properties)?
我使用一个外部服务来提供属性,但希望将这些属性作为@Named(..)变量提供。尝试在配置方法中执行此操作失败,npe为:模块输出上的Guice Names.bindProperties(binder(),properties)?,properties,guice,Properties,Guice,我使用一个外部服务来提供属性,但希望将这些属性作为@Named(..)变量提供。尝试在配置方法中执行此操作失败,npe为: Names.bindProperties(binder(), myPropRetriever.getProperties()); 正在失败,因为myPropRetriever在guice完成其工作之前不会出现。我能理解为什么这是有意义的——有人知道任何可能奏效的时髦黑客吗?在这种情况下会很方便。多亏durron597提供了指向相关问题的指针,这足以让我弄明白。答案是使用子
Names.bindProperties(binder(), myPropRetriever.getProperties());
正在失败,因为myPropRetriever在guice完成其工作之前不会出现。我能理解为什么这是有意义的——有人知道任何可能奏效的时髦黑客吗?在这种情况下会很方便。多亏durron597提供了指向相关问题的指针,这足以让我弄明白。答案是使用子喷油器对以前的喷油器输出执行操作。示例如下:
Injector propInjector = Guice.createInjector(new PropertiesModule());
PropertiesService propService = propInjector.getInstance(PropertiesService.class);
Injector injector = propInjector.createChildInjector(new MyModule(Objects.firstNonNull(propService.getProperties(), new Properties())));
注入器现在是应用程序其余部分的注入器
然后在MyModule中,您可以对创建的对象执行操作:
public class MyModule extends AbstractModule {
private final Properties properties;
public MyModule(Properties properties){
this.properties=properties;
}
@Override
protected void configure() {
// export all the properties as bindings
Names.bindProperties(binder(), properties);
// move on to bindings
// bind(..);
}
}
以防对其他人有帮助 相关人员:嘿,谢谢!这让我找到了正确的方向!如果其他人遇到同样的问题,我将在下面发布修复程序。关于您的系统,我没有足够的信息来回答您的问题,例如,您有什么代码“应该”创建
myPropRetriever
实例,但我认为这可能会有所帮助。