因为您负责创建模块实例,所以您可以将它们传递给您想要的任何构造函数参数.您在此处所要做的就是创建一个模块,将您的配置作为构造函数参数,然后在该模块中绑定它.
class YourMainModule() {
public static void main(String[] args) {
MyAppConfig config = createAppConfig(); // define this yourself
Injector injector = Guice.createInjector(
new ConfigModule(config),
new YourOtherModule(),
new YetAnotherModule());
injector.getInstance(YourApplication.class).run();
}
}
class ConfigModule extends AbstractModule {
private final MyAppConfig config;
ConfigModule(MyAppConfig config) {
this.config = config;
}
@Override public void configure() {
// Because you're binding to a single instance, you always get the
// exact same one back from Guice. This makes it implicitly a singleton.
bind(MyAppConfig.class).toInstance(config);
}
}