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

标签 java spring-boot

我正在尝试传入一个要从 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/

相关文章:

java - 在什么情况下应该重写 onDestroy()?

java - 删除最后一个空行

java - Spring Security 5 api key 过滤器

java - RabbitMq Spring Boot 部署 docker

java MockRestServiceServer 转义 url 中的字符

java - 如何从Java应用程序读取SVN Tortoise命令

java - 多线程问题

java - SpringBoot Redis 远程主机

java - 我可以在 Spring Boot 中从服务中显式调用自定义 validator 吗?

java - 基本 Controller 配置在 Spring Boot 中不起作用