Spring Boot-外部化配置属性 [英] Spring boot - Externalize config properties
问题描述
我的应用程序是带有嵌入式tomcat的Spring Boot应用程序.它使用名为"config.properties"的属性文件来存储各种应用程序级别的属性.我正在将应用程序中的属性文件加载为:
My Application is a Spring Boot Application with embedded tomcat. It uses a property file named "config.properties" for storing various application level 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>
当将属性文件嵌入jar文件中时,应用程序运行良好,但是我想外部化该属性文件-从系统中的文件夹而不是jar中提供它.
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.
我尝试将文件夹添加到类路径,然后使用 -cp
vm参数提供文件夹的位置,但这不起作用.
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.
所以我的问题是如何实现这种情况,即属性文件是从外部源提供的,而不是从jar中提供的.
So my question is how to achieve this scenario where property file is supplied from external source rather than supplied from within the 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>
,然后使用-
java -jar -Dconfig.file.location=D:\folder\ myjar.jar
这篇关于Spring Boot-外部化配置属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!