• <bdo id='cTfBQ'></bdo><ul id='cTfBQ'></ul>
    <tfoot id='cTfBQ'></tfoot>

    <small id='cTfBQ'></small><noframes id='cTfBQ'>

      <legend id='cTfBQ'><style id='cTfBQ'><dir id='cTfBQ'><q id='cTfBQ'></q></dir></style></legend>
    1. <i id='cTfBQ'><tr id='cTfBQ'><dt id='cTfBQ'><q id='cTfBQ'><span id='cTfBQ'><b id='cTfBQ'><form id='cTfBQ'><ins id='cTfBQ'></ins><ul id='cTfBQ'></ul><sub id='cTfBQ'></sub></form><legend id='cTfBQ'></legend><bdo id='cTfBQ'><pre id='cTfBQ'><center id='cTfBQ'></center></pre></bdo></b><th id='cTfBQ'></th></span></q></dt></tr></i><div id='cTfBQ'><tfoot id='cTfBQ'></tfoot><dl id='cTfBQ'><fieldset id='cTfBQ'></fieldset></dl></div>

        Spring Boot:从 YAML 文件加载 @Value

        时间:2023-09-26
      1. <legend id='4icwR'><style id='4icwR'><dir id='4icwR'><q id='4icwR'></q></dir></style></legend>

                <tfoot id='4icwR'></tfoot>

                <small id='4icwR'></small><noframes id='4icwR'>

                • <bdo id='4icwR'></bdo><ul id='4icwR'></ul>
                • <i id='4icwR'><tr id='4icwR'><dt id='4icwR'><q id='4icwR'><span id='4icwR'><b id='4icwR'><form id='4icwR'><ins id='4icwR'></ins><ul id='4icwR'></ul><sub id='4icwR'></sub></form><legend id='4icwR'></legend><bdo id='4icwR'><pre id='4icwR'><center id='4icwR'></center></pre></bdo></b><th id='4icwR'></th></span></q></dt></tr></i><div id='4icwR'><tfoot id='4icwR'></tfoot><dl id='4icwR'><fieldset id='4icwR'></fieldset></dl></div>
                    <tbody id='4icwR'></tbody>
                  本文介绍了Spring Boot:从 YAML 文件加载 @Value的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我需要从 .yml 文件中加载一个属性,该文件包含应用程序可以从中读取文件的文件夹的路径.

                  I need to load a property from a .yml file, which contains the path to a folder where the application can read files from.

                  我正在使用以下代码注入属性:

                  I'm using the following code to inject the property:

                  @Value("${files.upload.baseDir}")
                  private String pathToFileFolder;
                  

                  用于开发的 .yml 文件位于 src/main/resources/config/application.yml 下,我在生产中使用以下命令运行应用程序,以覆盖开发设置:

                  The .yml file for development is located under src/main/resources/config/application.yml, im running the application with the following command in production, to override the development settings:

                  java -jar app.jar --spring.config.location=/path/to/application-production.yml
                  

                  Spring Boot 文档说:

                  The Spring Boot documentation says:

                  SpringApplication 将从以下位置的 application.properties 文件中加载属性并将它们添加到 Spring 环境中:

                  SpringApplication will load properties from application.properties files in the following locations and add them to the Spring Environment:

                  1. 当前目录的/config 子目录.

                  1. A /config subdirectory of the current directory.

                  当前目录

                  类路径/config 包

                  A classpath /config package

                  类路径根

                  还有:

                  您还可以使用 YAML ('.yml') 文件来替代.properties".

                  You can also use YAML ('.yml') files as an alternative to '.properties'.

                  .yml 文件包含:

                  {...}
                  files:
                        upload:
                          baseDir: /Users/Thomas/Code/IdeaProjects/project1/files
                  {...}
                  

                  我的 Application 类被注释为:

                  @SpringBootApplication
                  @EnableCaching
                  

                  当我运行应用程序时,我得到一个异常:

                  When I run the application, i get an exception:

                  Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'files.upload.baseDir' in string value "${files.upload.baseDir}"
                  

                  是否必须使用 YamlPropertySourceLoader 类或添加特殊注解才能在 Spring Boot 中启用对 .yml 的支持?

                  Do I have to use the YamlPropertySourceLoader class or add a special annotation to enable the support for .yml in Spring Boot?

                  .yml 文件包含一些其他属性,这些属性可以由 Spring Boot 成功加载,例如 dataSource.XXX 或 hibernate.XXX.

                  The .yml file contains some other properties, which get successfully loaded by Spring Boot like dataSource.XXXor hibernate.XXX.

                  推荐答案

                  M.Deinum 是对的,我提供的设置正在运行 - yml 文件缩进错误,因此无法找到该属性.

                  M. Deinum is right, the setup i've provided is working - the yml file was indented wrong, so the property couldn't be found.

                  这篇关于Spring Boot:从 YAML 文件加载 @Value的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:application.yml 是否支持环境变量? 下一篇:用于 java 的 Travis CI yml 文件示例

                  相关文章

                  最新文章

                • <i id='DcXoY'><tr id='DcXoY'><dt id='DcXoY'><q id='DcXoY'><span id='DcXoY'><b id='DcXoY'><form id='DcXoY'><ins id='DcXoY'></ins><ul id='DcXoY'></ul><sub id='DcXoY'></sub></form><legend id='DcXoY'></legend><bdo id='DcXoY'><pre id='DcXoY'><center id='DcXoY'></center></pre></bdo></b><th id='DcXoY'></th></span></q></dt></tr></i><div id='DcXoY'><tfoot id='DcXoY'></tfoot><dl id='DcXoY'><fieldset id='DcXoY'></fieldset></dl></div>

                  1. <small id='DcXoY'></small><noframes id='DcXoY'>

                      <bdo id='DcXoY'></bdo><ul id='DcXoY'></ul>
                      <legend id='DcXoY'><style id='DcXoY'><dir id='DcXoY'><q id='DcXoY'></q></dir></style></legend>
                    1. <tfoot id='DcXoY'></tfoot>