简体   繁体   English

为单个常量设置多个绑定

[英]guice multiple bindings for a single constant

I have a java app that uses guice to do configuration.. I dont think this is what it is intended for but its what has been done and I only need to make a small change so I would prefer not to remove guice.我有一个使用 guice 进行配置的 java 应用程序。我不认为这是它的目的,但它已经完成了,我只需要做一个小的改变,所以我不想删除 guice。 Basically, java properties are bound to variables, I want to bind some to an environmental variables or to a java property.基本上,java 属性绑定到变量,我想将一些绑定到环境变量或 java 属性。 This is what I currently have这是我目前拥有的

bindConstant().annotatedWith(Names.named("value")).to(properties.getProperty("java.property.value"));

this is what I would like to do这就是我想做的

bindConstant().annotatedWith(Names.named("value")).to(System.getenv("JAVA_PROPERTY_VALUE"));

Is there a way to combine the two?有没有办法将两者结合起来? I cannot do both.我不能两者兼得。 Or, is this just a default and I basically have what I need already?或者,这只是默认设置,我基本上已经有了我需要的东西? ie if I do bindConstant to System.getenv that value will be used unless its overwritten in the properties file (in my case the string constant is not the full property name so I am unsure how it works now).即,如果我将 bindConstant 绑定到 System.getenv,除非它在属性文件中被覆盖,否则将使用该值(在我的情况下,字符串常量不是完整的属性名称,所以我不确定它现在是如何工作的)。

I really do not know much about how guice works, I believe an injector is created where this code is and later used to do things like...我真的不太了解 guice 的工作原理,我相信在此代码所在的位置创建了一个注入器,然后用于执行诸如...

@Inject(optional = true)
@Named("value)
private String value;

I basically want that value to default to the one in the properties file, but be overridden by the env property value if its present.我基本上希望该值默认为属性文件中的值,但如果 env 属性值存在,则将其覆盖。

I have tried simply using the env var value if it exists otherwise the property value, ie我曾尝试简单地使用 env var 值(如果它存在),否则使用属性值,即

bindConstant().annotatedWith(Names.named("value")).to(System.getenv(envVarName) != null && !System.getenv(envVarName).trim().isEmpty() ? System.getenv(envVarName) : properties.getProperty(propertyName));

Which works as expected when the environmental variable is defined and the property is not defined, but when both are defined the property is always used.当定义环境变量而未定义属性时,它按预期工作,但是当两者都定义时,总是使用属性。 Which just leads me to the fact that I know very little about guice and how it works, I have in code a very explicit binding between the property name and this method, but, it just seems to be a default value, something after that is overwriting my value with the one from the property file.这只是让我知道我对 guice 及其工作原理知之甚少,我在代码中在属性名称和此方法之间有一个非常明确的绑定,但是,它似乎只是一个默认值,之后是用属性文件中的值覆盖我的值。

This is super basic, but it's how we do things:这是非常基本的,但这就是我们做事的方式:

  • get Properties (sys.properties)获取属性(sys.properties)

some.random.prop=localhost

  • iterate through System.getEnv() overriding all the Properties遍历 System.getEnv() 覆盖所有属性
// Convert SOME_RANDOM_PROP to some.random.prop
properties.put(parseKey(entry.getKey()), entry.getValue());

Now your properties should be defaulted to app.properties and overwritten with matching env.properties, then just bind all he properties.现在你的属性应该默认为 app.properties 并用匹配的 env.properties 覆盖,然后绑定所有他的属性。

Names.bindProperties(binder(), properties);

The caveat here is that now System.getEnv() pointless, but since you're using Guice for all your injections this shouldn't really be an issue.这里需要注意的是,现在System.getEnv()毫无意义,但由于您使用 Guice 进行所有注入,这应该不是问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM