将时间戳变量添加到应用程序属性中的文件夹路径值

cRO*_*BOT 0 java spring timestamp properties spring-boot

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

screenshot.events = STARTED,SUCCEEDED,FAILED,STEP
screenshot.path = C:/Automation/${timestamp}
webdriver.type=CHROME
Run Code Online (Sandbox Code Playgroud)

ale*_*xbt 6

这里有 3 个选项:

1. 启动时

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

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

        new SpringApplicationBuilder() //
                .sources(Launcher.class)//
                .run(args);
    }
Run Code Online (Sandbox Code Playgroud)

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

screenshot.path = C:/Automation/${timestamp}
Run Code Online (Sandbox Code Playgroud)

2. 注射时

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

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());
Run Code Online (Sandbox Code Playgroud)