Spring Boot Spring.config.附加位置作为相对路径



我有一个Spring Boot项目,它的结构是:

- /my-config
- production-settings.yml
- local-settings.yml
- /src
- /main
- /java
- (the usual Java stuff)
- /resources
- application.yml

这是我的application.yml:

my.personal.value: ${MY_PLACEHOLDER}

其中我使用在local-settings.yml:中定义的占位符

MY_PLACEHOLDER: something

有人能告诉我如何使用带有文件local-settings.yml的相对路径的spring.config.additional-location吗,这样占位符就可以解析了。

谢谢。

您可以使用@PropertySource("file:./my-config/local-settings.yml")来测试相对路径,以查看是否加载了my.personal.value

@Configuration
// If the relative path is valid, just comment the PropertySource.
// @PropertySource("file:./my-config/local-settings.yml") 
public class MyPersonConfig {
@Value("${my.personal.value}")
private String value;
@PostConstruct
void run() {
System.out.println(value);
}
}

如果工作正常,您可以设置JVM选项并启动您的jar。更改local-settings.ymlproduction-settings.yml进行测试。

java -jar -Dspring.config.additional-location=file:./my-config/local-settings.yml build/libs/your.jar

完整的代码在这里。

最新更新