linux - 如何在 Windows 和 Linux 的@PropertySource 注释中保留通用文件路径

标签 linux spring-boot window filepath

我的 spring boot 应用程序中有以下配置代码:-

@Configuration
@PropertySources({
@PropertySource(value = "file://${CONFIG_PATH}/folder/application.properties"),
@PropertySource(value = "file://${CONFIG_PATH}/folder/application-log.properties"),
@PropertySource(value = "file://${CONFIG_PATH}/folder/application-persistence.properties"),
@PropertySource(value = "file://${CONFIG_PATH}/folder/application-environment.properties")
})
public class PopulateFixturesConfig {

@Bean
public static PropertySourcesPlaceholderConfigurer
propertySourcesPlaceholderConfigurer() {
    return new PropertySourcesPlaceholderConfigurer();
}
}

以上代码在 Linux 服务器上执行,我在本地机器上安装了 Windows。因此,当我必须进行开发时,我必须进行更改,我必须像这样在文件后添加额外的“/”:

 @PropertySource(value = "file:///${CONFIG_PATH}/folder/application-environment.properties")

再次,当我必须提交代码时,我必须删除多余的“/”。

有什么方法可以使路径在 windows 和 linux 上通用。

谢谢,

最佳答案

如果您使用的 Java 大于 Java 7,那么您可以这样做

String configPath= System.getProperty("CONFIG_PATH");

java.nio.file.Path path = java.nio.file.Paths.get(configPath, "some", "dir", "path1", "path2")

java.nio.file.Path 有很多方法可以给你字符串中的绝对路径

如果您使用的 Java 低于 Java 7,则使用 File.separator

关于linux - 如何在 Windows 和 Linux 的@PropertySource 注释中保留通用文件路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51872731/

相关文章:

java - 为什么我无法使用虚拟主机名来获取我的微服务?

java - Spring Boot RequestMapping 删除不起作用

emacs - 如何让编译日志在 Emacs 中创建一个新窗口?

java - 在同一个端口上连续运行一个程序

linux - 将 awk 脚本的输出重定向到文件

linux - 在 awk 的 BASH shell 中使用 bc 作为守护进程

javascript - 无法使用 Javascript 打开新窗口

linux - i.mx 287 EVK (FreeScale) 板未检测到 USB

spring-boot - Spring Boot 2.1.5、WebFlux、Reactor : How to deal properly with MDC

javascript - jQuery在计算窗口高度后将div放置在特定位置