<tfoot id='mrzN1'></tfoot>

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

      <legend id='mrzN1'><style id='mrzN1'><dir id='mrzN1'><q id='mrzN1'></q></dir></style></legend>

          <bdo id='mrzN1'></bdo><ul id='mrzN1'></ul>

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

      1. 在 SpringBoot 中读取环境变量

        Read environment variable in SpringBoot(在 SpringBoot 中读取环境变量)
        <legend id='rRRgn'><style id='rRRgn'><dir id='rRRgn'><q id='rRRgn'></q></dir></style></legend>

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

                  <tbody id='rRRgn'></tbody>
                <tfoot id='rRRgn'></tfoot>

                  本文介绍了在 SpringBoot 中读取环境变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  SpringBoot 中读取环境变量的最佳方法是什么?
                  在 Java 中,我使用:

                  What is the best way to read environment variables in SpringBoot?
                  In Java I did it using:

                  String foo = System.getenv("bar");
                  

                  是否可以使用 @Value 注释来做到这一点?

                  Is it possible to do it using @Value annotation?

                  推荐答案

                  引用 文档:

                  Spring Boot 允许您将配置外部化,以便您可以在不同的环境中使用相同的应用程序代码.您可以使用属性文件、YAML 文件、环境变量和命令行参数来外部化配置.属性值可以使用 @Value 注释 直接注入到您的 bean 中,通过 Spring 的 Environment 抽象访问或通过 绑定到结构化对象@ConfigurationProperties.

                  Spring Boot allows you to externalize your configuration so you can work with the same application code in different environments. You can use properties files, YAML files, environment variables and command-line arguments to externalize configuration. Property values can be injected directly into your beans using the @Value annotation, accessed via Spring’s Environment abstraction or bound to structured objects via @ConfigurationProperties.

                  所以,既然 Spring boot 允许你使用环境变量进行配置,而且 Spring boot 也允许你使用 @Value 从配置中读取一个属性,那么答案是肯定的.

                  So, since Spring boot allows you to use environment variables for configuration, and since Spring boot also allows you to use @Value to read a property from the configuration, the answer is yes.

                  例如,以下将给出相同的结果:

                  For example, the following will give the same result:

                  @Component
                  public class TestRunner implements CommandLineRunner {
                      @Value("${bar}")
                      private String bar;
                      private final Logger logger = LoggerFactory.getLogger(getClass());
                      @Override
                      public void run(String... strings) throws Exception {
                          logger.info("Foo from @Value: {}", bar);
                          logger.info("Foo from System.getenv(): {}", System.getenv("bar")); // Same output as line above
                      }
                  }
                  

                  这篇关于在 SpringBoot 中读取环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Maven JAVA_HOME environment variable is not defined correctly, but it is(Maven JAVA_HOME 环境变量未正确定义,但它是)
                  Java System Environment Variable(Java 系统环境变量)
                  Java -classpath option(Java -classpath 选项)
                  Tomcat 8 - context.xml use Environment Variable in Datasource(Tomcat 8 - context.xml 在数据源中使用环境变量)
                  Issue with JAVA_HOME(JAVA_HOME 的问题)
                  Access shell environment variables in Java(在 Java 中访问 shell 环境变量)
                1. <tfoot id='5Y4ni'></tfoot>

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

                        <bdo id='5Y4ni'></bdo><ul id='5Y4ni'></ul>
                          <legend id='5Y4ni'><style id='5Y4ni'><dir id='5Y4ni'><q id='5Y4ni'></q></dir></style></legend>
                          1. <small id='5Y4ni'></small><noframes id='5Y4ni'>