首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> Java 程式設計 >> Java 相關知識

Spring 多 Properties 配置文件加載全攻略:五大場景與最佳實踐

     編輯:Java 相關知識

實際專案中常需加載多個 .properties 文件(數據庫、MQ、遠程服務等),且面臨多模塊、多環境、優先級覆蓋等複雜場景。本文系統梳理 Spring 加載多配置文件的五種核心模式,附完整 XML 與註解配置示例。

場景一:單上下文加載多文件(最常用)

<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">
        <list>
            <value>classpath:/opt/demo/config/demo-db.properties</value>
            <value>file:/opt/demo/config/demo-mq.properties</value>
            <value>file:/opt/demo/config/demo-remote.properties</value>
        </list>
    </property>
</bean>

推薦用 file: 协议將配置與代碼分離,支援外部化部署。也可將 list 抽取為獨立 ArrayList Bean 復用。

場景二:多模塊獨立加載,避免鍵衝突(關鍵:order + ignoreUnresolvablePlaceholders)

<bean id="dbResources" class="java.util.ArrayList">
    <constructor-arg><list><value>file:/opt/demo/config/demo-db.properties</value></list></constructor-arg>
</bean>
<bean id="mqResources" class="java.util.ArrayList">
    <constructor-arg><list><value>file:/opt/demo/config/demo-mq.properties</value></list></constructor-arg>
</bean>

<bean id="dbPropertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="order" value="1"/>
    <property name="ignoreUnresolvablePlaceholders" value="true"/>
    <property name="locations" ref="dbResources"/>
</bean>
<bean id="mqPropertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="order" value="2"/>
    <property name="ignoreUnresolvablePlaceholders" value="true"/>
    <property name="locations" ref="mqResources"/>
</bean>

核心參數:

  • order:加載優先級,數值越小越早
  • ignoreUnresolvablePlaceholders=true:當前文件找不到鍵時不拋異常,交給下一個 Configurer 處理,**多 Configurer 共存必須開啟**

場景三:Java 代碼中 @Value 直接注入單值或整個 Properties 對象

<util:properties id="remoteSettings" location="file:/opt/demo/config/demo-remote.properties"/>
<!-- 或加載多文件 -->
<bean id="settings" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
    <property name="locations">
        <list>
            <value>file:/opt/rms/config/rms-mq.properties</value>
            <value>file:/opt/rms/config/rms-env.properties</value>
        </list>
    </property>
</bean>
public class Client {
    @Value("#{remoteSettings['remote.ip']}") private String ip;
    @Value("#{remoteSettings['remote.port']}") private String port;
    @Value("#{remoteSettings}") private Properties allProps; // 注入整個 Properties
}

SpEL 語法 #{beanName['key']} 靈活提取單值;PropertiesFactoryBean 可合併多文件。

場景四:XML 中直接注入 Properties 類型屬性

<bean id="remoteConfigs" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
    <property name="locations"><list><value>file:/opt/demo/config/demo-remote.properties</value></list></property>
</bean>
<bean id="client" class="com.demo.remote.Client">
    <property name="properties" ref="remoteConfigs"/>
</bean>
public class Client {
    private Properties remoteSettings; // + getter/setter 或 @Autowired
}

適用於 legacy XML 配置專案,或需將配置作為 Map 傳遞給第三方庫的場景。

場景五:通配符批量加載(Spring 3.1+)

<context:property-placeholder location="file:///${CONFIG_PATH}/*.properties"/>
<!-- 或 classpath 掃描 -->
<context:property-placeholder location="classpath*:/*.properties"/>

配合環境變量 CONFIG_PATH 實現配置目錄外部化,極適合容器化部署。

最佳實踐總結

場景推薦方案關鍵點
單應用多配置文件單 PropertyPlaceholderConfigurer + listfile: 協議外部化
多模塊/多團隊獨立配置多 Configurer + order + ignoreUnresolvablePlaceholders優先級控制、避免鍵衝突
代碼中靈活取值util:properties + @Value SpEL單值/整體注入皆可
XML 時代遺留專案PropertiesFactoryBean + property ref類型安全注入 Properties
容器化/雲原生部署通配符 + 環境變量配置與鏡像分離

靈活組合上述模式,即可應對從單體到微服務的各類配置管理需求。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved