简体   繁体   English

java属性文件中是否可以将一个变量指向另一个变量?

[英]Is it possible to one variable points to other in java properties file?

I have a lot of rows/entry in my properties file. 我的属性文件中有很多行/条目。 Rows are grouped in categories to keep file easier to maintain. 行按类别分组,以使文件更易于维护。 I have two key which have the same value. 我有两个具有相同价值的钥匙。 I don't want to remove one because it makes my file harder to maintain. 我不想删除一个文件,因为它使我的文件难以维护。

#category1
var1=foo
....
#category2
var2=foo

Is there any way to make var1 points to var2? 有什么办法可以使var1指向var2? I think about sth like that: 我想到这样的事:

    #category1
    var1=->var2 #point to var2
    ....
    #category2
    var2=foo

java.util.Properties implements the Map interface so you can do everything that this interface offers with it. java.util.Properties实现了Map接口,因此您可以使用此接口提供的所有操作。 To allow value to represent a reference to an other key you could wrap the code for getting the value in such a ways that it check whether the value is actually key and then return the value that is mapped by that key. 为了允许值表示对另一个键的引用,您可以包装代码以获取值,方式是检查值是否实际上是键,然后返回由该键映射的值。

so you could implement something like this: 因此您可以实现以下内容:

String value1 = p.getProperty("var1");
while(p.containsKey(value1)) {
    value1 = p.getProperty(value1);
}

better: add a prefix - $ for example - to value to identify a reference, only if present then resolve: 更好:在值中添加前缀(例如$以标识引用,只有存在时才解析:

String value1 = p.getProperty("var1");
while(value1.startsWith("$")) {
    value1 = p.getProperty(value1.substring(1));
}

A good way for wrapping the retrieve-code is by extending the Properties class and override the method getProperty . 包装检索代码的一种好方法是扩展Properties类并重写getProperty方法。

For org.springframework.context.support.ReloadableResourceBundleMessageSource you could extend it, override which ever methods you want and configure spring to inject your version of the class. 对于org.springframework.context.support.ReloadableResourceBundleMessageSource您可以对其进行扩展,覆盖所需的任何方法,并将spring配置为注入类的版本。

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

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