简体   繁体   English

Spring Boot-外部化配置属性

[英]Spring boot - Externalize config properties

My Application is a Spring Boot Application with embedded tomcat. 我的应用程序是带有嵌入式tomcat的Spring Boot应用程序。 It uses a property file named "config.properties" for storing various application level properties. 它使用名为“ config.properties”的属性文件来存储各种应用程序级别的属性。 I am loading property file in my application as : 我在应用程序中将属性文件加载为:

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

Application works fine when the property file is embedded in the jar file, but I want to externalize the property file - provide it from a folder in the system not from the jar. 当将属性文件嵌入jar文件中时,应用程序运行良好,但我想外部化该属性文件-从系统中的文件夹而不是jar中提供它。

I tried adding the folder to the classpath and then supplying the location of the folder using -cp vm argument but that does not work. 我尝试将文件夹添加到类路径中,然后使用-cp vm参数提供文件夹的位置,但这不起作用。

So my question is how to achieve this scenario where property file is supplied from external source rather than supplied from within the jar. 所以我的问题是如何实现这种情况,即属性文件是从外部源提供的,而不是从jar中提供的。

I have been able load file using the following code : 我已经可以使用以下代码加载文件:

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="location">
        <value>file:{config.file.location}/config.properties</value>
    </property>
</bean>

and starting the jar by using - 并通过使用-启动jar

java -jar -Dconfig.file.location=D:\folder\ myjar.jar

Use this code: 使用此代码:

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="location">
        <value>file:/full/path/to/your/file</value>
    </property>
</bean>

Using "file" you canspecify a full pathe where your config file is located. 使用“文件”,您可以指定配置文件所在的完整路径。

EDIT: If you whant to use in-line arguments, remove the bean PropertyPlaceholderConfigurer and use this instead: 编辑:如果您想使用嵌入式参数,请删除bean PropertyPlaceholderConfigurer并使用它:

-Dspring.config.location=file:/path/to/file/config.properties

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM