配置参数从配置文件中加载到PropertiesFactoryBean 和配置参数从数据库加载到PropertiesFactoryBean 的实现,及项目中的相关应用

1.加载.properties文件中的配置参数加载到PropertiesFactoryBean容器中

<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中

在此,有一点需要注意,如果在xml中需要用到刚刚配置的属性,我们会去用${“key”}方式去取,此时需要配置“占位符“,

<!--在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"}去取。

2.

<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);
}

从它的这个构造中我们可以发现需要传入的参数,数据源、表名、名称配置环境的名称、属性名、属性值,配置名称。(可以根据自己需要进行配置)

猜你喜欢

转载自blog.csdn.net/m0_37899388/article/details/81102362
今日推荐