linux - 如何在 Windows 和 linux 的 @PropertySource 注释中保留通用文件路径
问题描述
我的 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
推荐阅读
- r - 使用条件和子类别过滤(分层)数据
- elasticsearch - 在 ruby fiter 下使用 Time.parse 时,Logstash 解析失败
- java - Kubernetes 外部连接到 Mongodb 集群
- javascript - 一个函数中有多个事件处理程序
- elixir - JSON 响应的单元测试,匹配 JSON 响应以断言键和值
- r - 在库函数中分配变量
- angular - Angular 6.x 元标记 OG:描述不适用于 FB
- unit-testing - Rails 升级后的神秘 rspec 错误
- javascript - 如何使用 JS 动态创建 SVG 文本元素
- python - 处理的 python 是固定在一个 CPU 上,还是可以使用多个 CPU 超时?