简体   繁体   English

如果在命令行中提供了新值,如何覆盖从属性文件加载的值

[英]How to overwrite values loaded from properties file if new value is provided in command line

Funny enough there are 2 almost identical questions with a similar title that I found: 有趣的是,我发现了2个几乎相同且标题相似的问题:

Q1 Q1

Q2 Q2

however my case seems to be slightly different. 但是我的情况似乎有些不同。

So I have a config.properties file that is being loaded by ConfigurationGetter class like this: 所以我有一个由ConfigurationGetter类加载的config.properties文件,如下所示:

public class ConfigurationGetter {

    Properties prop = new Properties();
    InputStream inputStream = null;

    public Properties getPropValues() throws IOException {

        try {
            String fileName = "config.properties";

            inputStream = getClass().getClassLoader().getResourceAsStream(fileName);

            if (inputStream != null) {
                prop.load(inputStream);
            } else {
                throw new FileNotFoundException("property file '" + fileName + "' not found in the classpath");
            }

        } catch (Exception e) {
            System.out.println("Exception: " + e);
        } finally {
            inputStream.close();
        }

        return prop;

    }

Then I instantiate this class in another class like this: 然后,在另一个这样的类中实例化该类:

ConfigurationGetter config = new ConfigurationGetter();
props = config.getPropValues();

And then I can extract the properties by key like this: 然后我可以像这样通过键提取属性:

props.getProperty("keyName")

Whet I want to do is to overwrite this value I get from properties file if I provide it via command line. 如果我是通过命令行提供的,我想做的就是覆盖从属性文件中获得的该值。 For example, if I have a line like this in my config.properties that I loaded as explained above: 例如,如果在我的config.properties中有一条如上所述的行,则如上所述进行加载:

keyName=true

and I also run the code like this: 并且我还运行如下代码:

mvn test -DkeyName=false

then false will be the one resolved. 那么false将被解决。

After loading the properties file, I do the following: 加载属性文件后,请执行以下操作:

for (String propertyName : properties.stringPropertyNames()) {
    String systemPropertyValue = System.getProperty(propertyName);

    if (systemPropertyValue != null) {
        properties.setProperty(propertyName, systemPropertyValue);
    }
}

This gives you the behavior you want by overriding the value from the property file with the corresponding system property value if it is present. 通过使用相应的系统属性值(如果存在)覆盖属性文件中的值,可以提供所需的行为。

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

相关问题 如果存在命令行值,则覆盖属性文件 - Overwrite the properties file if command line value is present 当从命令行提供附加配置位置时,未从外部配置加载的属性 - properties not loaded from external configuration, when additional config location is provided from command line 如何用 Java 中的新值覆盖 CSV 文件 - How to overwrite a CSV file with new values in Java 如果在命令行参数中指定,则覆盖属性文件值 - Overwrite property file values if specified in the command line arguments 如何通过命令行参数覆盖属性文件的值? - How to override the properties file value through command line arguments? 使用 Spring 表达式语言断言从属性文件加载的属性(用逗号分隔的值)是否包含“值” - Using Spring expression language to assert if a property (with comma separated values) loaded from a properties file, contains “value” 从属性文件设置SuiteClassess并在命令行中强化属性文件 - Set SuiteClassess from Properties file and indtentify properties file in command line 如何停止覆盖txt文件并让它写入新行 - How to stop overwrite txt file and let it write in new line 如何从命令行为注释设置新值? - How set new value for the annotation from the command line? 使用值中的新行字符读取java中的.properties文件 - Read .properties file in java with new line characters in values
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM