zoukankan      html  css  js  c++  java
  • spring 配置参数从配置文件中加载到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);
    }

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

  • 相关阅读:
    设计模式之责任链模式(Chain of Responsibility )
    Cubieboard2裸机开发之(二)板载LED交替闪烁
    Cubieboard2裸机开发之(一)点亮板载LED
    A20(Cubieboard2)启动过程浅析
    入手Cubieboard2之制作最小Linux系统
    ARM Linux启动代码分析
    Linux设备驱动剖析之Input(四)
    Linux设备驱动剖析之Input(三)
    Linux设备驱动剖析之Input(二)
    Linux设备驱动剖析之Input(一)
  • 原文地址:https://www.cnblogs.com/weixupeng/p/11453416.html
Copyright © 2011-2022 走看看