spring 配置參數從配置文件中加載到PropertiesFactoryBean 和配置參數從數據庫加載到PropertiesFactoryBean 的實現,及項目中的相關應用

1.加載.properties文件中的配置參數加載到PropertiesFactoryBean容器中java

<bean id="configProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="locations">//本地資源 讀取方式,用流讀取資源進行加載。
<list>
<value>classpath:config.properties</value>
<value>classpath:redis.properties</value>
</list>
</property>
</bean>


如此,兩個properties 的屬性變被放入了Spring 的PropertiesFactoryBean中redis

在此,有一點須要注意,若是在xml中須要用到剛剛配置的屬性,咱們會去用${「key」}方式去取,此時須要配置「佔位符「,spring

<!--在xml使用properties文件中的的配置數據時,須要用到PropertyPlaceholderConfigurer作佔位符-->
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="properties">
<ref bean="configProperties"/>
</property>
</bean>

PropertyPlaceholderConfigurer會把配置的properties解析並存放爲java標準的properties,此時在xml中咱們能夠經過${"key"}去取。apache

2.ui

<bean name="DatabaseConfigrationFactoryBean" class="com.shopManager.config.DatabaseConfigrationFactoryBean">
<constructor-arg ref="DatabaseConfiguration"/>
</bean>

<bean name="DatabaseConfiguration" class="org.apache.commons.configuration.DatabaseConfiguration">
</bean>
/**
* Build a configuration from a table containing multiple configurations.
* No commits are performed by the new configuration instance.
*
* @param datasource the datasource to connect to the database
* @param table the name of the table containing the configurations
* @param nameColumn the column containing the name of the configuration
* @param keyColumn the column containing the keys of the configuration
* @param valueColumn the column containing the values of the configuration
* @param name the name of the configuration
*/
public DatabaseConfiguration(DataSource datasource, String table, String nameColumn,
String keyColumn, String valueColumn, String name)
{
this(datasource, table, nameColumn, keyColumn, valueColumn, name, false);
}

從它的這個構造中咱們能夠發現須要傳入的參數,數據源、表名、名稱配置環境的名稱、屬性名、屬性值,配置名稱。(能夠根據本身須要進行配置)

this

相關文章
相關標籤/搜索