如何动态更新属性文件? [英] How do I update a properties file dynamically?

查看:36
本文介绍了如何动态更新属性文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序是一个批处理,它从 application.properties 文件中提取环境变量.我使用的密码必须每隔几个月更新一次.我想自动执行密码更新过程并将更新后的密码保存在属性文件中,以便在以后的运行中使用,但我尝试进行的任何更新都不会传播到 application.config 文件.

当我在 Intellij 中运行时,没有错误,但文件没有更新.当我将应用程序作为 jar 运行时,它给出:

java.io.FileNotFoundException: file:{localpath}\application.jar!\BOOT-INF\classes!\application.properties(文件名、目录名或卷标语法不正确)

如何在应用程序首次运行时动态更新 jar 中的 application.properties 文件,或者我是否需要在 jar 之外创建一个新的属性文件?

以下示例代码片段:

properties.config:

用户名=用户密码=密码123

Application.java:

//获取属性prop = 新属性();InputStream 输入 = 空;试试{输入 = 新的 FileInputStream(f);//加载属性文件prop.load(输入);} catch (IOException ex) {logger.error("读取application.properties 时出错", ex);ex.printStackTrace();} 最后{如果(输入!= null){试试{input.close();} catch (IOException e) {e.printStackTrace();}}}//更新属性prop.setProperty("TESTSTRING", "testvalue");//写入属性文件输出流操作系统 = 空;试试{File f = new File(Application.class.getClassLoader().getResource("application.properties").getFile());os = new FileOutputStream(f);prop.store(os, 评论);}catch(IOException ex){logger.error("更新application.properties 出错", ex);ex.printStackTrace();} 最后{如果(操作系统!= null){试试{os.close();} catch (IOException e){e.printStackTrace();}}

解决方案

根据运行环境可能会发生变化的应用程序的属性应该永远打包在打包组件中.
它不应该与组件硬耦合,并且如果需要,它也应该可以在不修改打包组件的情况下进行修改.
此外,属性可能包含机密信息(如您的情况:用户名和密码).
所以最好直接存储在目标环境中.

例如,来自 Heroku/12 个因素 的云原生应用程序原则之一(这也是很好的做法通常)是在环境中存储配置".

因此出于同样的原因,必须绝对避免更新打包组件内的属性文件.
除了这样做之外,如果发生错误,状态应用程序更难重现.

<小时>

在您的情况下,只需根据目标环境在特定的属性文件中填写 usernamepassword 属性并将此文件存储在环境中.

然后您只需指定此属性文件即可运行应用程序.
例如对于 Windows:

java -jar yourApp-1.0.jar --spring.config.location=file:///D:/application-target-env.properties

这是关于它的参考文档:24.2 访问命令行属性

My application is a batch process that pulls environment variables from an application.properties file. The password that I use must be updated every few months. I would like to automate the password update process and save the updated password in the properties file so it can be used in future runs, but any updates I try to make are not propagated to the application.config file.

When I run in Intellij, there are no errors, but the file is not updated. When I run the application as a jar, it gives:

java.io.FileNotFoundException: file:{localpath}\application.jar!\BOOT-INF\classes!\application.properties (The filename, directory name, or volume label syntax is incorrect)

How do I dynamically update the application.properties file within the jar or do I need to create a new properties file outside of the jar when the application first runs??

EXAMPLE CODE SNIPPETS BELOW:

properties.config:

username=user
password=password123

Application.java:

    //get properties
    prop = new Properties();
    InputStream input = null;
    try {
        input = new FileInputStream(f);
        // load a properties file
        prop.load(input);
    } catch (IOException ex) {
        logger.error("Error reading application.properties", ex);
        ex.printStackTrace();
    } finally {
        if (input != null) {
            try {
                input.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    //update properties
    prop.setProperty("TESTSTRING", "testvalue");

    //write to properties file
    OutputStream os = null;
    try{
        File f = new File(Application.class.getClassLoader().getResource("application.properties").getFile());
        os = new FileOutputStream(f);
        prop.store(os, comments);
    }catch(IOException ex){
        logger.error("Error updating application.properties", ex);
        ex.printStackTrace();
    } finally {
        if(os != null){
            try{
                os.close();
            } catch (IOException e){
                e.printStackTrace();
            }
        }

解决方案

Properties of an application that may change according to the running environment should never be packaged inside the packaged component.
It should be not hard coupled to the component and it should also be modifiable if required without modifying the packaged component.
Besides, properties may contain confidential information (as in your case : username and password).
So it should be preferably stored directly on the target environment.

For example, one of the cloud native application principles from Heroku/twelve factors (that are also good practices in general) is "storing the configuration in the environment".

So updating the properties file inside the packaged component has to be absolutely avoided for the same reasons.
Besides doing it makes the state application more hard to reproduce if an errors occurs.


In your case, simply fill username and password properties in a specific properties file according to the target environment and store this file on the environment.

Then you have just to run the application by specifying this properties file.
For example for windows :

java -jar yourApp-1.0.jar --spring.config.location=file:///D:/application-target-env.properties

Here is the reference documentation about it : 24.2 Accessing command line properties

这篇关于如何动态更新属性文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆