實際專案中常需加載多個 .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 復用。
<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 共存必須開啟**<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 可合併多文件。
<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 傳遞給第三方庫的場景。
<context:property-placeholder location="file:///${CONFIG_PATH}/*.properties"/>
<!-- 或 classpath 掃描 -->
<context:property-placeholder location="classpath*:/*.properties"/>
配合環境變量 CONFIG_PATH 實現配置目錄外部化,極適合容器化部署。
| 場景 | 推薦方案 | 關鍵點 |
|---|---|---|
| 單應用多配置文件 | 單 PropertyPlaceholderConfigurer + list | file: 協議外部化 |
| 多模塊/多團隊獨立配置 | 多 Configurer + order + ignoreUnresolvablePlaceholders | 優先級控制、避免鍵衝突 |
| 代碼中靈活取值 | util:properties + @Value SpEL | 單值/整體注入皆可 |
| XML 時代遺留專案 | PropertiesFactoryBean + property ref | 類型安全注入 Properties |
| 容器化/雲原生部署 | 通配符 + 環境變量 | 配置與鏡像分離 |
靈活組合上述模式,即可應對從單體到微服務的各類配置管理需求。