将值绑定到Guice中的两种可能性之一 [英] Binding a value to one of two possibilities in Guice

查看:55
本文介绍了将值绑定到Guice中的两种可能性之一的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个默认值,如果设置了 System.getProperty("foo"),则可以将其覆盖.我有一个我要的模块

Suppose I have a value for which I have a default, which can be overridden if System.getProperty("foo") is set. I have one module for which I have

bindConstant().annotatedWith(Names.named("Default foo")).to(defaultValue);

我想知道实现模块的最佳方法,该模块是我想将带有"foo"注释的内容绑定到 System.getProperty("foo"),或者,如果没有的话存在,则为默认foo"绑定.

I'm wondering what the best way of implementing a module for which I want to bind something annotated with "foo" to System.getProperty("foo"), or, if it does not exist, the "Default foo" binding.

我想到了一个像这样的简单模块:

I've thought of a simple module like so:

public class SimpleIfBlockModule extends AbstractModule {
    @Override
    public void configure() {
        requireBinding(Key.get(String.class, Names.named("Default foo")));
        if (System.getProperties().containsKey("foo")) {
            bindConstant().annotatedWith(Names.named("foo")).to(System.getProperty("foo"));
        } else {
            bind(String.class).annotatedWith(Names.named("foo")).to(Key.get(String.class, Names.named("Default foo")));
        }
    }
}

我还考虑过像这样创建系统属性模块":

I've also considered creating a "system property module" like so:

public class SystemPropertyModule extends PrivateModule {
    @Override
    public void configure() {
        Names.bindProperties(binder(), System.getProperties());
        if (System.getProperties().contains("foo")) {
            expose(String.class).annotatedWith(Names.named("foo"));
        }
    }
}

并使用 SystemPropertyModule 创建一个第三个模块的注入器,该注入器执行"foo"的绑定.两者似乎都有其缺点,所以我想知道是否有什么我应该做不同的事情.我一直希望没有注射器的东西,并且可以合理地推广到多个"foo"属性.有什么想法吗?

And using SystemPropertyModule to create an injector that a third module, which does the binding of "foo". Both of these seem to have their downsides, so I'm wondering if there is anything I should be doing differently. I was hoping for something that's both injector-free and reasonably generalizable to multiple "foo" attributes. Any ideas?

推荐答案

如果您不需要在运行时更改绑定(即,在注入器创建时绑定是恒定的),那么您的第一个设计对我来说似乎是最好的选择).

Your first design seems like the best option to me if you don't need the binding to change at runtime (i.e. the binding is constant as of injector creation time).

对于您在运行时确定的任何值,您都需要提供程序或 @Provides 方法:

For any value you decide at runtime, you'll need a Provider or a @Provides method:

public class SimpleIfBlockModule extends AbstractModule {
    @Override public void configure() { }

    @Provides @Named("foo") String provideFoo(
             @Named("Default foo") String defaultFoo,
             AnyInjectableDependencyHere ifNeeded) {
        if (System.getProperties().containsKey("foo")) {
            return System.getProperty("foo");
        } else {
            return defaultFoo;
        }
    }
}

如果您需要在运行时基于参数进行决策,请使用此解决方案.

If you need to decide at runtime based on a parameter, use this solution.

这篇关于将值绑定到Guice中的两种可能性之一的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆