我正在为Uni的决赛项目做准备,但遇到了一个特殊的问题。我正在测试某些网站如何使用TestNG和Selenium在localhost中工作。现在,我具有不同的集成,这意味着它使用在属性文件中配置的不同数据库。我希望我可以在JVM或命令行中传递参数,例如说“ integration1”,它将从属性文件中捕获该字段。我在网上发现的唯一内容与Spring配置文件有关,但这无济于事,因为它是一个普通的Java项目。这是代码:
default.properties
foreach ($tokens as $name => $original) {
switch ($name) {
// Simple key values on the comment.
case 'flag-mark-message-link':
$replacements[$original] = $message->field_mark_message();
break;
user.properties(它会检查user.properties文件中是否存在某些字段,并且是否使用该字段代替默认字段,这对于团队中的其他成员很有用,因为每种配置都不同)
db_driver = com.mysql.jdbc.Driver
db_path = jdbc:mysql://localhost:3306/dana?useUnicode=true&characterEncoding=UTF-8
这些属性文件在ConfigurationService类中配置
ConfigurationService.java
db_driver = com.mysql.jdbc.Driver
db_path_elixir = jdbc:mysql://localhost:3306/elixir?useUnicode=true&characterEncoding=UTF-8
db_path_dana = jdbc:mysql://localhost:3306/dana?useUnicode=true&characterEncoding=UTF-8
#db_path - actual path which will be used if user passes "dana" or "elixir" as arguments
#my logic would be something like db_path = jdbc:mysql://localhost:3306/ + ${integration} + ?useUnicode=true&characterEncoding=UTF-8
答案 0 :(得分:0)
您可以按以下方式修改ConfigurationService,并在命令行中将属性作为java -Dkey=value
传递。
private String getProperty(String key){
String value = null;
if (System.getProperties().contains(key))
value = System.getProperty(key);
else if (userProperties.containsKey(key))
value = userProperties.getProperty(key);
else
value = defaultProperties.getProperty(key);
}
您还可以如下初始化您的ConfigurationService实例
Properties properties;
public void init(){
properties.putAll(defaultProperties);
properties.putAll(userProperties);
properties.putAll(System.getProperties());
}
然后按如下所示修改您的getProperty方法
private String getProperty(String key){
return properties.getProperty(key);
}
这里putAll
调用的顺序很重要。当您再次输入相同的键值时,先前的值将被覆盖。