java .properties 文件中的条件语句
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10899910/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
conditional statement in .properties file
提问by Piyush
Is there a way we can have conditional statement inside a .properties
file?
有没有办法在.properties
文件中包含条件语句?
like:
喜欢:
if(condition1)
xyz = abc
else if(condition2)
xyz = efg
回答by Jigar Joshi
NoThere is no such conditional statement in properties file, May be you would write a wrapper over Properties
to encapsulate your logic
否属性文件中没有这样的条件语句,可能您会编写一个包装器Properties
来封装您的逻辑
Example:
例子:
class MyProperties extends Properties {
/**
*
*
*
* @param key
* @param conditionalMap
* @return
*/
public String getProperty(String key, List<Decision> decisionList) {
if (decisionList == null) {
return getProperty(key);
}
Long value = Long.parseLong(getProperty(key));
for (Decision decision : decisionList) {
if (Condition.EQUALS == decision.getCondition() && decision.getValue().equals(value)) {
return getProperty(decision.getTargetKey());
}
}
return super.getProperty(key);
}
}
and
和
enum Condition {
EQUALS, GREATER, LESSER
}
and
和
class Decision {
Condition condition;
String targetKey;
Long value;
//accessor
public Condition getCondition() {
return condition;
}
public void setCondition(Condition condition) {
this.condition = condition;
}
public String getTargetKey() {
return targetKey;
}
public void setTargetKey(String targetKey) {
this.targetKey = targetKey;
}
}
so now for example if you want to read the properties file, get category of age, if it is greater than 0 and less than 10 read kid
所以现在例如如果你想读取属性文件,获取年龄类别,如果它大于 0 且小于 10 读取 kid
so may be you could pass the list of such conditions,
所以也许你可以通过这些条件的列表,
note: This design can go under much improvement (not good design), it is just to illustrate how to wrap properties and add stuff that OP wants
注意:这个设计可以有很大的改进(不是好的设计),它只是为了说明如何包装属性和添加 OP 想要的东西
回答by JB Nizet
No, it's not possible. The file format is freely available: http://docs.oracle.com/javase/6/docs/api/java/util/Properties.html#load%28java.io.Reader%29.
不,这不可能。文件格式可免费获得:http: //docs.oracle.com/javase/6/docs/api/java/util/Properties.html#load%28java.io.Reader%29。
Do this in Java code:
在 Java 代码中执行此操作:
if (condition1) {
return properties.getProperty("xyz.1");
}
else if (condition2) {
return properties.getProperty("xyz.2");
}
回答by duffymo
If your problem is different properties for different environments (e.g. devl, test, perf, prod), the common solution is a different version of the properties file for each environment. Communicate environment info to your app and look for a file with the correct name appended to the file name.
如果您的问题是针对不同环境(例如 devl、test、perf、prod)的不同属性,常见的解决方案是针对每个环境使用不同版本的属性文件。将环境信息传达给您的应用程序,并查找在文件名后附加了正确名称的文件。
Something like this for Spring:
春天的事情是这样的:
Environment-specific configuration for a Spring-based web application?
回答by user949300
As @Jigar said, there is no conditional logic in a properties file. But you could have two lines in there, e.g. something like:
正如@Jigar 所说,属性文件中没有条件逻辑。但是你可以在那里有两行,例如:
xyz.condition1 = abs
xyz.condition2 = efg
and, in your code to access the property, append the proper condition to the key.
并且,在您访问该属性的代码中,将正确的条件附加到密钥。
回答by Alessandro
this is the way:
这是方法:
xyz[condition1]=abs
xyz[condition2]=efg