我有4个不同的配置要使用,这些配置值存储在一个属性文件中。所有配置的属性是相同的,但是每个配置的值是不同的。
交货:我使用的属性文件配置:
####Config1####
conf1.password=admin
conf1.username=admin
conf1.context=123
conf1.name=localhost
####config2####
conf2.username=app
conf2.password=app
conf2.context=com
conf2.name=localhost
####config3####
conf3.username=app
conf3.password=app
conf3.context=com
conf3.name=localhost
####config4####
conf4.username=app
conf4.password=app
conf4.context=com
conf4.name=localhost
我可以从属性文件中获得属性。是否有可能有一个单一的变量来存储基于配置的这些值,并以优化和可读的方式访问它们?
我尝试单独使用哈希映射每个配置,并从中获取它。但是它增加了我的代码冗余,就好像我对每个配置执行相同的步骤,并且如果不使用配置名称来获取hashmap值。
目前我正在使用hashmap的属性:
HashMap<String, String> conf1 = new HashMap<>();
HashMap<String, String> conf2 = new HashMap<>();
HashMap<String, String> conf3 = new HashMap<>();
HashMap<String, String> conf4 = new HashMap<>();
conf1.put("UserName", prop.getProperty(“conf1.username"));
conf1.put("Password",prop.getProperty("conf1.password"));
conf1.put(“name”,prop.getProperty("conf1.name"));
conf1.put("context”,”conf1,context”);
conf2.put("UserName", prop.getProperty(“conf2.username"));
conf2.put("Password",prop.getProperty("conf2.password"));
conf2.put(“name”,prop.getProperty("conf2.name"));
conf2.put("context”,”conf2,context”);
conf3...
conf4...
if (Conf.equalsIgnoreCase(“conf1”)) {
GenerateTestFile(
"Name:“ + conf1.get("Name") + “-UserName:” +
conf1.get("UserName") + “-Password:” + conf1.get("Password") +
"-Context:” + conf1.get(“Context”) ,FileName);
} else if (Conf.equalsIgnoreCase(“conf2”)) {
GenerateTestFile(
"Name:“ + conf2.get("Name") + “-UserName:” +
conf2.get("UserName") + “-Password:” + conf2.get("Password") +
"-Context:” + conf2.get(“Context”) ,FileName);
}
Else if(conf3){…}
Else if(conf4){…}
您可以像这样使用嵌套HashMap:
HashMap<String, HashMap<String, String>> conf = new HashMap<>();
for(int i = 1; i <= 4; i++) {
String currentConfName = "conf" + i;
HashMap<String, String> currentConf = new HashMap<>();
currentConf.put("UserName", prop.getProperty(currentConfName + ".username"));
//And everythin else you want to add
conf.put(currentConfName, currentConf);
}
同样,你可以通过迭代HashMap
来生成你的文件您可以使用Map<String,Map<String,String>>
,其中键是配置的名称,值(Map<String,String>
)是配置参数。
private static Pattern RE = Pattern.compile(
"([A-Za-z0-9_-]+)\.([A-Za-z0-9_-]+)");
private static Map<String,Map<String,String>> loadConfs(String name)
throws IOException {
Map<String,Map<String,String>> confs = new HashMap<>();
Properties props = new Properties();
ClassLoader cl = Thread.currentThread().getContextClassLoader();
try (InputStream in = cl.getResourceAsStream(name)) {
props.load(in);
}
for (String propName: props.stringPropertyNames()) {
Matcher matcher = RE.matcher(propName);
if (matcher.matches()) {
String confName = matcher.group(1);
String parmName = matcher.group(2);
Map<String,String> conf = confs.get(confName);
if (conf == null) {
conf = new HashMap<>();
confs.put(confName, conf);
}
conf.put(parmName, props.getProperty(propName));
}
}
return confs;
}
你可以这样做:
Map<String,Map<String,String>> confs = loadConfs("conf.properties");
...
Map<String,String> conf = confs.get(confName);
if (conf != null) {
GenerateTestFile(
"Name:“ + conf.get("Name")
+ “-UserName:” + conf.get("UserName")
+ “-Password:” + conf.get("Password")
+ "-Context:” + conf.get(“Context”),
FileName);
}