Spring 启动 - 外部化配置属性
Spring boot - Externalize config properties
我的应用程序是一个 Spring 带有嵌入式 tomcat 的启动应用程序。它使用名为 "config.properties" 的 属性 文件来存储各种应用程序级别的属性。我在我的应用程序中加载 属性 文件:
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>classpath:config.properties</value>
</property>
</bean>
当 属性 文件嵌入到 jar 文件中时,应用程序工作正常,但我想外部化 属性 文件 - 从系统中的文件夹而不是从 jar 提供它。
我尝试将文件夹添加到类路径,然后使用 -cp
vm 参数提供文件夹的位置,但这不起作用。
所以我的问题是如何实现这种 属性 文件从外部源提供而不是从 jar 内部提供的场景。
使用此代码:
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>file:/full/path/to/your/file</value>
</property>
</bean>
使用 "file" 您可以指定配置文件所在的完整路径。
编辑:
如果您想使用内联参数,请删除 bean PropertyPlaceholderConfigurer 并改用它:
-Dspring.config.location=file:/path/to/file/config.properties
我已经能够使用以下代码加载文件:
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>file:{config.file.location}/config.properties</value>
</property>
</bean>
并使用 -
启动 jar
java -jar -Dconfig.file.location=D:\folder\ myjar.jar
我的应用程序是一个 Spring 带有嵌入式 tomcat 的启动应用程序。它使用名为 "config.properties" 的 属性 文件来存储各种应用程序级别的属性。我在我的应用程序中加载 属性 文件:
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>classpath:config.properties</value>
</property>
</bean>
当 属性 文件嵌入到 jar 文件中时,应用程序工作正常,但我想外部化 属性 文件 - 从系统中的文件夹而不是从 jar 提供它。
我尝试将文件夹添加到类路径,然后使用 -cp
vm 参数提供文件夹的位置,但这不起作用。
所以我的问题是如何实现这种 属性 文件从外部源提供而不是从 jar 内部提供的场景。
使用此代码:
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>file:/full/path/to/your/file</value>
</property>
</bean>
使用 "file" 您可以指定配置文件所在的完整路径。
编辑: 如果您想使用内联参数,请删除 bean PropertyPlaceholderConfigurer 并改用它:
-Dspring.config.location=file:/path/to/file/config.properties
我已经能够使用以下代码加载文件:
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>file:{config.file.location}/config.properties</value>
</property>
</bean>
并使用 -
启动 jarjava -jar -Dconfig.file.location=D:\folder\ myjar.jar