zoukankan      html  css  js  c++  java
  • spring配置文件中util:properties和context:property-placeholder

    util:properties和context:property-placeholder标签都可以用来获取外部配置文件中的内容 
    1、util:properties 
    它是以声明bean方式来使用,创建了一个bean,下面使用的时候通过SpEL表达式#{}获取bean的属性。

    <util:properties id="config" location="classpath:db.properties" />
    <!-- 配置连接池 -->
    <bean id="ds" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
    <property name="driverClassName" value="#{config.driver}" />
    <property name="url" value="#{config.url}" />
    <property name="username" value="#{config.username}" />
    <property name="password" value="#{config.password}" />
    </bean>

    需要注意,这种方式需要在spring配置文件头部声明

    xmlns:util="http://www.springframework.org/schema/util"
    xsi:schemaLocation="http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd"

    java中用这种方式获取 @Value(#{"
    config.driver"}) 

    2、context:property-placeholder 
    它是将配置文件加载至spring上下文中,然后通过${}取得值,常用于bean的属性上

    <context:property-placeholder location="classpath:general.properties"/>

    上面的配置和下面配置等价,是对下面配置的简化

    <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
       <property name="ignoreUnresolvablePlaceholders" value="true"/>
       <property name="locations">
          <list>
             <value>classpath:general.properties</value>
    </list>
    </property>
    </bean>
    
    <!-- 配置Druid连接池 -->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <!-- 基本属性 driverClassName、url、user、password -->
        <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>


  • 相关阅读:
    js的同步和异步
    事件三
    事件2
    JS作用域题
    游戏之乐
    NSTimer(2)
    NSTimer
    理解 Objective-C Runtime
    iOS系统安全机制概览
    Which is the best of GCD, NSThread or NSOperationQueue?
  • 原文地址:https://www.cnblogs.com/moxiaotao/p/9247042.html
Copyright © 2011-2022 走看看