【问题标题】:Dropwizard + Guice Bundle getting Application Configuration in the ModuleDropwizard + Guice Bundle 在模块中获取应用程序配置
【发布时间】:2015-10-29 15:48:43
【问题描述】:

对于一般设置,我关注的是tutorial。现在,在我的 guice 模块中,我想根据服务器配置传递构造不同的单例。问题是我无法在此处获取配置。我怎样才能做到这一点?

public class ServerModule implements Module {  
    @Override
    public void configure(Binder binder) {
    }

    @Provides
    @Singleton
    public AnInterface provideSingleton() {
        return myServerConfiguration.isSomething() 
               ? new SomeObject() : new SomeOtherObject();
    }
}

【问题讨论】:

    标签: java guice dropwizard


    【解决方案1】:

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

    从给定链接复制的示例:

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

    【讨论】:

    • 但这似乎不适用于单例:1)尚未设置dropwizard环境。这可能是由于在引导阶段尝试访问 dropwizard 环境造成的。
    • 没错。 DW 抱怨环境尚未设置。
    • 我已经有一段时间没有使用 dropwizard 了,所以我无法确认,但我想这个答案被接受并同时被否决很奇怪......也许有人更有洞察力能想出新的答案吗?
    猜你喜欢
    • 2017-10-18
    • 1970-01-01
    • 2011-07-29
    • 1970-01-01
    • 2011-06-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-12
    相关资源
    最近更新 更多