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)場景:多模塊專案,各模塊有自己的 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(系統屬性優先) |
jdbc.driverClassName=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/mydb?useUnicode=true&characterEncoding=UTF-8 jdbc.username=root jdbc.password=123456
<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>
@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;
}
}
# application.properties spring.datasource.url=jdbc:mysql://localhost:3306/mydb spring.datasource.username=root spring.datasource.password=123456 # 零代碼,DataSource 自動創建
PropertyPlaceholderConfigurer 是 Spring 早期配置外部化的基石。現代專案建議遷移至
@PropertySource+Environment或 Spring Bootapplication.yml,獲得類型安全、鬆綁重啟、配置中心整合等能力。