一.spring獲取資源屬性值${key名稱}與#{key名稱}區別 1)用戶獲取外部文件中指定key的值; 2)可以出現在xml配置文件中,也可以出現在註解@Value中; 3)一般用戶獲取數據庫配置文件的內容信息; #{key名稱}: 1)SpEL表達式的格式:
SpEL:Spring Expression Language,spring的一套表達式,主要應用在IOC容器進行對象屬性的注入。格式爲:#{表達式} 在使用的時候也允許#{‘${key}’}這樣使用。2)可以出現在xml配置文件中,也可以出現在註解@Value中 3)可以任意表達式,支持運算符。 二.資源文件獲取方式 <!-- 根據系統環境label.properties配置的id,獲取環境配置值,語法:#{label['system.label']}--> 【前提條件:】 1.工程resources目錄結果如下: -----resources -----resources/label/label.properties -----resources/conf_dev/report-jdbc.properties -----resources/conf_dev/business-jdbc.properties -----resources/conf_test/report-jdbc.properties -----resources/conf_test/business-jdbc.properties -----resources/conf_pre/report-jdbc.properties -----resources/conf_pre/business-jdbc.properties -----resources/conf_prod/report-jdbc.properties -----resources/conf_prod/business-jdbc.properties -----resources/spring -----resources/spring/spring.xml -----resources/spring/spring-dao.xml 2.文件label.properties內容如下: system.label=dev 3.公共系統環境配置 <!-- 讀取系統環境label.properties配置 --> <util:properties id="label" location="classpath:label/label.properties" /> 【加載配置方法一:】 <!-- 根據系統環境label.properties配置的id,獲取環境配置值,語法:#{label['system.label']},下面指定資源文件可以絕對路徑,也可以相對路徑,並支持正則匹配 --> <util:properties id="conf" location="classpath:conf_#{label['system.label']}/*jdbc.properties" /> 注意:多個資源屬性文件,使用逗號分隔(,) 【加載配置方法二:】 <!-- 根據系統環境label.properties配置的id,獲取環境配置值,語法:#{label['system.label']} --> <bean id="conf" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <value> <!-- 此可以絕對路徑,也可以相對路徑,並支持正則匹配 --> classpath:conf_#{label['system.label']}/*jdbc.properties </value> </list> </property> </bean> 注意:PropertyPlaceholderConfigurer類Bean的id 與<util:properties>標籤的id是等同的, 【加載配置方法三:】 <context:property-placeholder id="conf" ocation="classpath:conf_#{label['system.label']}/*jdbc.properties" />
注意:多個資源屬性文件,使用逗號分隔(,)
【加載配置方法四:】 在java代碼中,使用 @PropertySource 註解實現配置文件加載: import org.springframework.beans.factory.annotation.Value; @Service @PropertySource(value={"classpath:conf_#{label['system.label']}/*jdbc.properties"} public class ComboPooledDataSource { @Value("#{conf['dataSource.master.driverClassName']}") private String driverClass; } 【加載配置方法五:】 使用PropertiesFactoryBean方式 <bean id="configProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean"> <property name="locations">//本地資源 讀取方式,用流讀取資源進行加載。 <list> <value>classpath:conf_#{label['system.label']}/*jdbc.properties</value> </list> </property> </bean>
三.案例: 根據該id獲取資源屬性值語法:#{id名['資源屬性名']}, 比如:#{conf['dataSource.master.driverClassName']} 1)xml文件中獲取資源屬性值: <bean id="master_ccdai_data_source_collector" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"> <property name="driverClass" value="#{conf['dataSource.master.driverClassName']}"></property> ...... </bean> 2)java類中註解獲取資源屬性值: import org.springframework.beans.factory.annotation.Value; @Service public class ComboPooledDataSource { @Value("#{conf['dataSource.master.driverClassName']}") private String driverClass; } 注意: 1)@Value方式支持Service、DAO、JavaBean類;但類成員屬性不能爲final、static。 2)如在引入資源文件中,不設置id="conf"時,在使用時不需要使用conf,例如:
@Value("#{dataSource.master.driverClassName}")
3)資源文件加載配置需放值spring.xml文件最頂端,否則部分代碼導不入屬性值。