spring-properties 介紹
在項目中如果有些參數經常需要修改,或者後期可能需要修改,那我們都可以把這些.properties文件類似的配置文件中,源代碼中讀取properties裏面的值,這樣後期只需要改動properties文件即可,不需要去尋找源代碼進行修改,這樣更加方便。在Spring中有兩種加載properties文件的方式:基於xml方式和基於註解方式。
spring管理屬性配置文件properties
- org.springframework.beans.factory.config.PropertiesFactoryBean管理屬性
- org.springframework.beans.factory.config.PropertyPlaceholderConfigurer也可以管理配置文件。
使用方法
1.新建一個Java project,命名settingConfig;
2 新建一個文件夾config 存放 .properties配置文件;
3.新建一個spring配置文件applicationContext.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:annotation-config />
<context:component-scan base-package="com.*"></context:component-scan>
<!-- 使用註解注入properties中的值 -->
<bean id="setting"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:*.properties</value>
<value>/WEB-INF/config/settingConfig.properties</value>
</list>
</property>
<!-- 設置編碼格式 -->
<property name="fileEncoding" value="UTF-8"></property>
</bean>
</beans>
4.創建settinConfig對象
5.使用@註解方式 讀取.properties 值 爲settinConfig對象賦值
package com.cdgk.flxh.config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
/**
* setting.properties 的映射文件
*
/
@Component("settinConfig")
public class SettinConfig {
@Value("${namConfig.key}")
private String test_01;
@Value("${namConfig.key}")
private String test_02;
@Value("${namConfig.key}")
private String test_03;
public String getTest_01() {
return test_01;
}
public void setTest_01(String test_01) {
this.test_01= test_01;
}
public String getTest_01() {
return test_01;
}
public void setTest_02(String test_02) {
this.test_01= test_02;
}
public String getTest_02() {
return test_02;
}
public void setTest_03(String test_03) {
this.test_01= test_03;
}
public String getTest_03() {
return test_03;
}
}
5.也可以使用xml進行賦值
<bean id="settingConfig" class="${com.cdgk.flxh.config.SettingConfig}"> <!-- 這些配置Spring在啓動時會去config.properties中找 -->
<property name="test" value="${key}" />
<property name="test" value="${key}" />
<property name="test" value="${key}" />
<property name="test" value="${key}" />
</bean>
- 總結
- 使用PropertyPlaceholderConfigurer管理屬性文件獲取屬性的方式 @Value("${properties_key}")
- 使用PropertiesFactoryBean管理屬性文件獲取屬性的方式 @Value("${bean_id['properties_key']}")
拓展
一、Spring中報"Could not resolve placeholder"的解決方案
除去properites文件路徑錯誤、拼寫錯誤外,出現”Could not resolve placeholder”很有可能是使用了多個PropertyPlaceholderConfigurer或者多個<context:property-placeholder>(<context:property-placeholder>底層也是PropertyPlaceholderConfigurer)的原因。
因爲spring掃描註冊PropertyPlaceholderConfigurer的bean被設置爲單例模式,spring只會掃描一個PropertyPlaceholderConfigurer或者<context:property-placeholder>配置,其它多餘將被忽略,所以纔會出現替代屬性值的佔位符無法解析。
解決方案:
方案一:將多個PropertyPlaceholderConfigurer或者多個<context:property-placeholder>合併到一個裏面,統一加載所有的資源文件
1.如下代碼所示:
<bean id="propertyPlaceholderConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:xxx.properties</value>
<value>classpath:jyyy.properties</value>
</list>
</property>
</bean>
或者:
<context:property-placeholder location="classpath:xxx.properties,classpath:yyy.properties"/>
方案二:堅持使用多個PropertyPlaceholderConfigurer或者多個<context:property-placeholder>,那麼需要添加ignoreUnresolvablePlaceholders屬性或者ignore-unresolvable屬性,設置其值爲true
1.如下代碼所示:
<bean id="xxx" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="xxx.properties" />
<property name="ignoreUnresolvablePlaceholders" value="true" />
</bean>
<bean id="yyy" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="yyy.properties" />
<property name="ignoreUnresolvablePlaceholders" value="true" />
</bean>
或者:
<context:property-placeholder location="xxx.properties" ignore-unresolvable="true"/>
<context:property-placeholder location="yyy.properties" ignore-unresolvable="true"/>