Dropwizard + Guice Bundle getting Application Configuration in the Module

时间:2015-10-29 15:48:44

标签: java guice dropwizard

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();
    }
}

1 个答案:

答案 0 :(得分:-2)

如果您再次仔细阅读本教程,您将看到他们如何做到这一点:让guice将配置注入到提供方法中。

从给定链接复制的示例:

@Provides
@Named("message")
public String provideMessage(ServerConfiguration serverConfiguration) {
    return serverConfiguration.getMessage();
}