将时间戳变量添加到应用程序属性中的文件夹路径值 [英] Add Timestamp Variable to Folder Path Value in Application Properties

查看:22
本文介绍了将时间戳变量添加到应用程序属性中的文件夹路径值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我需要在 app.properties 文件中设置一个文件夹路径名值.我还想以当前时间戳命名它,这样当它用于创建文件时,它也会创建文件夹.我目前拥有的不起作用.

So I need to set a folder path name value in the app.properties file. I also want to name it after the current timestamp so that when it's used to create a file it'll also create the folder. What I current have doesn't work.

screenshot.events = STARTED,SUCCEEDED,FAILED,STEP
screenshot.path = C:/Automation/${timestamp}
webdriver.type=CHROME

推荐答案

这里有 3 个选项:

您可以在启动 Spring Boot 应用程序时定义所需的 SystemProperty:

You can define the required SystemProperty when launching your Spring Boot Application:

public static void main(String[] args) {
        System.setProperty("timestamp",String.valueOf(System.currentTimeMillis()));

        new SpringApplicationBuilder() //
                .sources(Launcher.class)//
                .run(args);
    }

然后,按照您的方式在 application.properties 中定义您的属性:

Then, define your property in application.properties just the way you did:

screenshot.path = C:/Automation/${timestamp}

2.注射时

@Value("${screenshot.path}")
public void setScreenshotPath(String screenshotPath) {
    this.screenshotPath = 
         screenshotPath.replace("${timestamp}", System.currentTimeMillis());
}

3.使用时 - 执行时的动态时间戳

@Value("${screenshot.path}")
private String screenshotPath;
...
new File(screenshotPath.replace("${timestamp}", System.currentTimeMillis());

//or the following without the need for ${timestamp} in screenshot.path
//new File(screenshotPath + System.currentTimeMillis());

这篇关于将时间戳变量添加到应用程序属性中的文件夹路径值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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