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

Spring PropertyPlaceholderConfigurer 讀取 properties 配置完全指南

     編輯:Java 相關知識

核心作用

PropertyPlaceholderConfigurer 將外部 .properties 文件中的鍵值對解析後,替換 Spring XML 中的 ${key} 佔位符,實現配置外部化與環境隔離。

基本配置:單文件

<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="location" value="classpath:conf/sqlmap/jdbc.properties"/>
    <property name="fileEncoding" value="UTF-8"/>
</bean>

進階配置:多文件與路徑模式

多文件列表

<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">
        <list>
            <value>/WEB-INF/mail.properties</value>
            <value>classpath:conf/sqlmap/jdbc.properties</value>
        </list>
    </property>
</bean>

通配符批量載入

<property name="locations">
    <list>
        <value>classpath*:config/**/*.properties</value> <!-- 所有 config 目錄下 properties -->
        <value>classpath*:META-INF/spring/*.properties</value>
    </list>
</property>
  • classpath: 僅第一個匹配
  • classpath*: 所有匹配(跨 JAR)

多環境隔離:多 Configurer 協作

場景:多模塊專案,各模塊有自己的 properties,需合併且優先級可控。

<!-- 模塊 1:優先級 1 -->
<bean id="configurerModule1" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="order" value="1"/>
    <property name="ignoreUnresolvablePlaceholders" value="true"/>
    <property name="location" value="classpath:module1/db.properties"/>
</bean>

<!-- 模塊 2:優先級 2 -->
<bean id="configurerModule2" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="order" value="2"/>
    <property name="ignoreUnresolvablePlaceholders" value="true"/>
    <property name="locations">
        <list>
            <value>classpath:module2/jdbc.properties</value>
            <value>classpath:module2/cache.properties</value>
        </list>
    </property>
</bean>

關鍵屬性解析

屬性作用建議值
order加載順序,數值越小越優先按模塊依賴關係設定
ignoreUnresolvablePlaceholders遇到無法解析的 ${key} 是否報錯true(多 Configurer 必須)
placeholderPrefix/suffix自定義佔位符格式默認 ${ / },可改 %{ }
systemPropertiesMode系統屬性覆蓋策略OVERRIDE(系統屬性優先)

實戰:DataSource 配置範例

jdbc.properties

jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mydb?useUnicode=true&characterEncoding=UTF-8
jdbc.username=root
jdbc.password=123456

Spring XML 引用

<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="location" value="classpath:jdbc.properties"/>
</bean>

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
    <property name="driverClassName" value="${jdbc.driverClassName}"/>
    <property name="url" value="${jdbc.url}"/>
    <property name="username" value="${jdbc.username}"/>
    <property name="password" value="${jdbc.password}"/>
</bean>

Spring 3.1+ 現代替代方案

Environment & @PropertySource (JavaConfig)

@Configuration
@PropertySource("classpath:jdbc.properties")
public class DataSourceConfig {
    @Autowired Environment env;

    @Bean
    public DataSource dataSource() {
        BasicDataSource ds = new BasicDataSource();
        ds.setDriverClassName(env.getProperty("jdbc.driverClassName"));
        ds.setUrl(env.getProperty("jdbc.url"));
        ds.setUsername(env.getProperty("jdbc.username"));
        ds.setPassword(env.getProperty("jdbc.password"));
        return ds;
    }
}

Spring Boot 自動配置

# application.properties
spring.datasource.url=jdbc:mysql://localhost:3306/mydb
spring.datasource.username=root
spring.datasource.password=123456
# 零代碼,DataSource 自動創建

PropertyPlaceholderConfigurer 是 Spring 早期配置外部化的基石。現代專案建議遷移至 @PropertySource + Environment 或 Spring Boot application.yml,獲得類型安全、鬆綁重啟、配置中心整合等能力。

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