For the general setup I was following this tutorial. Now, in my guice module I would like to pass construct different Singletons based on the server configuration. The problem is that I can not get the configuration here. How could I achieve this?
public class ServerModule implements Module {
@Override
public void configure(Binder binder) {
}
@Provides
@Singleton
public AnInterface provideSingleton() {
return myServerConfiguration.isSomething()
? new SomeObject() : new SomeOtherObject();
}
}
答案 0 :(得分:-2)
如果您再次仔细阅读本教程,您将看到他们如何做到这一点:让guice将配置注入到提供方法中。
从给定链接复制的示例:
@Provides
@Named("message")
public String provideMessage(ServerConfiguration serverConfiguration) {
return serverConfiguration.getMessage();
}