<bdo id='Pslm5'></bdo><ul id='Pslm5'></ul>
      <tfoot id='Pslm5'></tfoot><legend id='Pslm5'><style id='Pslm5'><dir id='Pslm5'><q id='Pslm5'></q></dir></style></legend>

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

      1. <i id='Pslm5'><tr id='Pslm5'><dt id='Pslm5'><q id='Pslm5'><span id='Pslm5'><b id='Pslm5'><form id='Pslm5'><ins id='Pslm5'></ins><ul id='Pslm5'></ul><sub id='Pslm5'></sub></form><legend id='Pslm5'></legend><bdo id='Pslm5'><pre id='Pslm5'><center id='Pslm5'></center></pre></bdo></b><th id='Pslm5'></th></span></q></dt></tr></i><div id='Pslm5'><tfoot id='Pslm5'></tfoot><dl id='Pslm5'><fieldset id='Pslm5'></fieldset></dl></div>
      2. .properties 文件中的 PropertyPlaceholderConfigurer 和环境变量

        PropertyPlaceholderConfigurer and environment variables in .properties files(.properties 文件中的 PropertyPlaceholderConfigurer 和环境变量)
          <bdo id='stzeG'></bdo><ul id='stzeG'></ul>

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

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

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

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

                  问题描述

                  我有一个带有 PropertyPlaceholderConfigurer 的 Spring application-context.xml,用于从 .properties 文件中获取属性值.主要和测试源文件夹具有单独的 .properties 文件.问题是我需要在 .properties 文件中使用环境变量.但是当我按照以下方式进行操作时:

                  I have a Spring application-context.xml with PropertyPlaceholderConfigurer to get properties' values from .properties file. Main and test source folders have separate .properties file. The issue is that I need to use environment variables in .properties file. But when I do it in the following way:

                  property.name=${env.SYSTEM_PROPERTY}
                  

                  我收到以下错误:

                  org.springframework.beans.factory.BeanDefinitionStoreException: Invalid bean definition with name 'beanName' defined in class path resource [com/example/applicationContext.xml]: Could not resolve placeholder 'env.SYSTEM_PROPERTY'
                  

                  而占位符配置器定义为

                  <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
                      <property name="location" value="classpath:com/example/application.properties"/>
                  </bean>
                  

                  任何想法如何使 property.name 被解释为环境变量(而不是占位符)?

                  Any ideas how-to make property.name be interpreted as environment variable (and not as placeholder)?

                  最好的问候,德米特里.

                  Best regards, Dmitriy.

                  推荐答案

                  我可能会彻底改变解决方案:直接注入系统属性,而不是注入引用系统属性的属性

                  I'd probably change the solution completely: inject the system property directly, as opposed to injecting the property which refers to a system property

                  例如

                  @Value("#{ systemProperties['JAVA_MY_ENV'] }") 
                  private String myVar;
                  

                  <property name ="myVar" value="#{systemProperties['JAVA_MY_ENV']}"/>
                  

                  我使用这样的属性占位符配置器

                  I use a property placeholder configurer like this

                  <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
                    <property name="locations">
                      <list>
                          <value>classpath:someprops.properties</value>
                      </list>
                    </property>
                    <property name="ignoreResourceNotFound" value="true" />
                    <property name="searchSystemEnvironment" value="true" />
                    <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
                  

                  您还必须记住使用

                   -DJAVA_MY_ENV=xyz
                  

                  这样,当您运行生产版本时,您可以传递一件事,而在运行测试时传递另一件事.

                  This way when you run the production version you can pass one thing and when you are running tests another.

                  我经常做的事情是这样的:

                  Also what I often what I do is something like this:

                    <property name="locations">
                      <list>
                        <value>classpath:someprops.properties</value>
                        <value>classpath:someprops-{environment}.properties</value>
                      </list>
                    </property>
                  

                  其中环境是 prod/stage/test/int/ci/local(每个环境 1 个 - 您现在可能只有 2 或 3 个).您可以将环境变量传递给程序.无论其在本地 pc/tests 上的生产/运行是否在 someprops.properties 属性文件中,任何应该相同的属性.任何特定于环境/运行方式的文件都将放入更具体的文件中(您应该将其放入 someprops.properties 文件以及默认值,除非被覆盖机制)

                  where environment is prod/stage/test/int/ci/local (1 per environment - you may only have 2 or 3 for now). You can pass the environment variable to the program. Any properties which should be the same regardless of if its production/running on your local pc/tests would be in the someprops.properties property file. Any ones specific to the environment/way its being run as will go in the more specific file (you should put it in the someprops.properties file as well as a default unless overridden mechanism)

                  例如在类路径中:someprops.properties

                  E.g. in classpath:someprops.properties

                  url=www.mysite.com
                  

                  在类路径中:someprops-local.properties

                  in classpath:someprops-local.properties

                  url=localhost
                  

                  通过使用这个基本思想,您可以以干净的方式分离测试和程序的正常运行属性.

                  By using this basic idea you can separate tests and the program's normal running properties in a clean manner.

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

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

                  相关文档推荐

                  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 选项)
                  Read environment variable in SpringBoot(在 SpringBoot 中读取环境变量)
                  Tomcat 8 - context.xml use Environment Variable in Datasource(Tomcat 8 - context.xml 在数据源中使用环境变量)
                  Issue with JAVA_HOME(JAVA_HOME 的问题)
                  <legend id='X6F8f'><style id='X6F8f'><dir id='X6F8f'><q id='X6F8f'></q></dir></style></legend>

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

                    <tfoot id='X6F8f'></tfoot>

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