簡體   English   中英

Spring Boot-外部化配置屬性

[英]Spring boot - Externalize config properties

我的應用程序是帶有嵌入式tomcat的Spring Boot應用程序。 它使用名為“ 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:{config.file.location}/config.properties</value>
    </property>
</bean>

並通過使用-啟動jar

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

使用此代碼:

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

使用“文件”,您可以指定配置文件所在的完整路徑。

編輯:如果您想使用嵌入式參數,請刪除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