代码之家  ›  专栏  ›  技术社区  ›  dweeb

如何为springboot指定外部YAML配置文件

  •  1
  • dweeb  · 技术社区  · 6 年前

    我正在尝试传入一个值,以便在springboot中从自定义配置文件自动连接。以下是代码段:

    春季班

    @Configuration
    public class MyConfig {
    
        @Value("${BOOTSTRAP_SERVERS}")
        private String bootstrapServers;
    

    我的文件.yaml

    BOOTSTRAP_SERVERS: 
      10.0.0.12:9092
    

    执行命令

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

    但是,当我键入上述命令时,出现以下错误:

    java.lang.IllegalArgumentException异常:无法解析值“${BOOTSTRAP\u SERVERS}”中的占位符“BOOTSTRAP\u SERVERS”

    1 回复  |  直到 6 年前
        1
  •  3
  •   ch271828n    4 年前

    显然,这是由于路径声明无效,要配置外部属性/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