当前位置:天才代写 > tutorial > JAVA 教程 > Spring如何操作propertyConfigurer类 读取.property数据库设置文件

Spring如何操作propertyConfigurer类 读取.property数据库设置文件

2017-11-02 08:00 星期四 所属: JAVA 教程 浏览:516

副标题#e#

1.Spring的框架中,org.springframework.beans.factory.config.PropertyPlaceholderConfigurer类可以将.properties(key/value形式)文件中

一些动态设定的值(value),在XML中替换为占位该键($key$)的值,

.properties文件可以按照客户需求,自界说一些相关的参数,这样的设计可提供措施的机动性。

2.在Spring中,利用PropertyPlaceholderConfigurer可以在XML设置文件中插手外下属性文件,虽然也可以指定外部文件的编码,如:

<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
    <property name="location">  
      <value>conf/sqlmap/jdbc.properties</value>  
    </property>  
     <property name="fileEncoding">  
       <value>UTF-8</value>  
     </property>  
</bean>

虽然也可以引入多个属性文件,如:

<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
   <property name="locations">  
    <list>  
     <value>/WEB-INF/mail.properties</value>     
     <value>classpath: conf/sqlmap/jdbc.properties</value>//留意这两种value值的写法  
    </list>  
   </property>  
</bean>

根基的利用要领是:

Xml代码

<bean id="propertyConfigurerForAnalysis" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
    <property name="location">  
        <value>classpath:/spring/include/dbQuery.properties</value>  
    </property>  
    <property name="fileEncoding">  
       <value>UTF-8</value>  
     </property>  
</bean>

个中classpath是引用src目次下的文件写法。

当存在多个Properties文件时,设置就需利用locations了:

Xml代码

<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
    <property name="locations">  
       <list>  
          <value>classpath:/spring/include/jdbc-parms.properties</value>  
          <value>classpath:/spring/include/base-config.properties</value>  
          <value>classpath*:config/jdbc.properties</value>  
        </list>  
    </property>  
</bean>

接下来我们要利用多个PropertyPlaceholderConfigurer来分手设置,到达整合多工程下的多个分手的Properties文件,其设置如下

Xml代码

<bean id="propertyConfigurerForProject1" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
    <property name="order" value="1"  />  
    <property name="ignoreUnresolvablePlaceholders" value="true"  />  
    <property name="location">  
       <value>classpath:/spring/include/dbQuery.properties</value>  
    </property>  
</bean>

Xml代码

<bean id="propertyConfigurerForProject2" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
    <property name="order" value="2"  />  
    <property name="ignoreUnresolvablePlaceholders" value="true"  />  
    <property name="locations">  
      <list>  
        <value>classpath:/spring/include/jdbc-parms.properties</value>  
        <value>classpath:/spring/include/base-config.properties</value>  
      </list>  
    </property>  
</bean>


#p#副标题#e#

个中order属性代表其加载顺序,

而ignoreUnresolvablePlaceholders为是否忽略不行理会的Placeholder,如设置了多个PropertyPlaceholderConfigurer,则需配置为true

3.譬如,jdbc.properties的内容为:

jdbc.driverClassName=com.mysql.jdbc.Driver

jdbc.url=jdbc:mysql://localhost/mysqldb?useUnicode=true&amp;characterEncoding=UTF-8&amp;zeroDateTimeBehavior=round;

jdbc.username=root

jdbc.password=123456

4.那么在spring设置文件中,我们就可以这样写:

<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
   <property name="locations">  
    <list>  
     <value>classpath: conf/sqlmap/jdbc.properties </value>  
    </list>  
   </property>  
</bean>  
      
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">  
   <property name="driverClassName" value="${jdbc.driverClassName}"  />  
   <property name="url" value="${jdbc.url}"  />  
   <property name="username" value="${jdbc.username}"  />  
   <property name="password" value="${jdbc.password}"  />  
</bean>

5.这样,一个简朴的数据源就配置完毕了。可以看出:PropertyPlaceholderConfigurer起的浸染就是将占位符指向的数据库设置信息放在bean中界说的东西。

From:csdn博客 跬步小流

 

    关键字:

天才代写-代写联系方式