gpt4 book ai didi

java - 如何为Spring Boot指定外部YAML配置文件

转载 作者:行者123 更新时间:2023-11-30 02:02:38 24 4
gpt4 key购买 nike

我正在尝试传入一个要从 Spring boot 中的自定义配置文件 Autowiring 的值。以下是代码片段:

Spring 类

@Configuration
public class MyConfig {

@Value("${BOOTSTRAP_SERVERS}")
private String bootstrapServers;

myfile.yaml

BOOTSTRAP_SERVERS: 
10.0.0.12:9092

执行命令

java  -jar app.jar --spring.config.location=/file/path/myfile.yaml

但是,当我输入上述命令时,我收到此错误:

java.lang.IllegalArgumentException: Could not resolve placeholder 'BOOTSTRAP_SERVERS' in value "${BOOTSTRAP_SERVERS}"

为了让它发挥作用,我在这里缺少什么?我打算将应用程序安装在 kubernetes 中,因此我需要能够外部化我的配置。提前致谢。

最佳答案

显然是由于路径声明无效,要配置外部属性/yml文件,您必须使用file:前缀作为--spring.config.location

所以试试这个,

--spring.config.location="file:/path/to/myfile.yaml"

另一种选择是,

-Dspring.config.location="file:/path/to/myfile.yaml"

确保 myfile.yaml 位于该目录中。

<小时/>

官方文档:https://docs.spring.io/spring-boot/docs/2.1.9.RELEASE/reference/html/boot-features-external-config.html#boot-features-external-config-application-property-files

关于java - 如何为Spring Boot指定外部YAML配置文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52336358/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com