Java 通过使用properties的子类重写getProperty()方法,尝试在另一个properties键中使用另一个properties键值
我试图使用properties类的子类ovveride getProperty()方法在另一个properties键中使用另一个properties键值,但我无法从属性文件中获得所需的键值。这就是我得到子类代码的地方 子类Java 通过使用properties的子类重写getProperty()方法,尝试在另一个properties键中使用另一个properties键值,java,properties,subclass,Java,Properties,Subclass,我试图使用properties类的子类ovveride getProperty()方法在另一个properties键中使用另一个properties键值,但我无法从属性文件中获得所需的键值。这就是我得到子类代码的地方 子类 import java.util.Properties; /** * * A subclass of Properties that allows recursive * references for property values. For example, * * &
import java.util.Properties;
/**
*
* A subclass of Properties that allows recursive
* references for property values. For example,
*
* <pre><code>
* A=12345678
* B={A}90
* C={B} plus more
* </code></pre>
*
* will result in <code>getProperty("C")</code>
* returning the value "1234567890 plus more".
*
* @author: Chris Mair
*
* Copied from: http://www2.sys-con.com/ITSG/virtualcd/Java/archives/0612/mair/index.html
*/
//@SuppressWarnings("serial")
public class XProperties extends Properties {
private static final long serialVersionUID = 1L;
private static final String START_DELIMITER = "${";
private static final String END_DELIMITER = "}";
@Override
public String getProperty(String key) {
String value = super.getProperty(key);
if (value != null) {
int startIndex = 0;
int endIndex = 0;
while ( (startIndex = value.indexOf(START_DELIMITER, endIndex)) >= 0
&& (endIndex = value.indexOf(END_DELIMITER, startIndex) ) >= 0) {
String variableName = value.substring(startIndex + START_DELIMITER.length(), endIndex);
// now call getProperty recursively to have this looked up
String variableValue = null;
if (!variableName.equals(key)) {
// only recurse if the variable does not equal our own original key
variableValue = this.getProperty(variableName);
}
if (variableValue == null) {
// when unable to find the variable value, just return it as the variable name
variableValue = START_DELIMITER + variableName + END_DELIMITER;
}
value = value.replace(START_DELIMITER + variableName + END_DELIMITER, variableValue);
} // while matches.
}
return value;
}
}
*
*将导致getProperty(“C”)
*返回值“1234567890加上更多”。
*
*@作者:克里斯·梅尔
*
*抄袭自:http://www2.sys-con.com/ITSG/virtualcd/Java/archives/0612/mair/index.html
*/
//@抑制警告(“串行”)
公共类XProperty扩展了属性{
私有静态最终长serialVersionUID=1L;
私有静态最终字符串起始分隔符=“${”;
私有静态最终字符串结尾_分隔符=“}”;
@凌驾
公共字符串getProperty(字符串键){
字符串值=super.getProperty(键);
if(值!=null){
int startIndex=0;
int-endIndex=0;
而((startIndex=value.indexOf(START_分隔符,endIndex))>=0
&&(endIndex=value.indexOf(END_分隔符,startIndex))>=0){
String variableName=value.substring(startIndex+START_DELIMITER.length(),endIndex);
//现在递归调用getProperty以查找该属性
字符串variableValue=null;
如果(!variableName.equals(键)){
//仅当变量不等于我们自己的原始键时才递归
variableValue=this.getProperty(variableName);
}
if(variableValue==null){
//当找不到变量值时,只需将其作为变量名返回即可
variableValue=开始\分隔符+variableName+结束\分隔符;
}
value=value.replace(开始分隔符+变量名+结束分隔符,变量值);
}//当匹配时。
}
返回值;
}
}
属性文件
import java.util.Properties;
public class GitClone {
static XProperties a = new XProperties();
Properties prop = new Properties();
try {
a.load(GitClone.class.getClassLoader().getResourceAsStream("config.properties"));
} catch (IOException e) {
e.printStackTrace();
}
File cfoWebDir = new File (a.getProperty("LOCALDIRWEB"));
System.out.println(cfoWebDir);
上述类的实现
NOTHING PRINTED OUT
打印输出时的输出
C:\test_PROJECT\cfo_web
预期产量
有什么帮助吗?我是否执行了错误的实现?在
GitClone
类中,加载prop
实例,然后读取a
实例(该实例为空)。您应该执行a.load(…)
,并删除prop
@Tiggerpls向上投票我的问题,因为我已使用解决方案对其进行了修改。或者,我必须先问一下,但它现在起作用了吗?@Tigger是的,一旦更改为a.load,它现在起作用了。所以现在,这不再是一个问题。我认为这只是一个小错误,所以它永远不会对其他人有帮助。如果你同意的话,也许你可以直接删除这篇文章?如果你不想,你可以回到问题的前一个版本,发表你自己的答案,我不介意。
C:\test_PROJECT\cfo_web