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>


  • 相关阅读:
    DBCA创建数据库ORA-01034 ORACLE not available
    Linux shell 内部变量
    ext4文件系统制作
    curses-键盘编码-openssl加解密【转】
    Linux 中的键盘映射【转】
    C 语言 字符串命令 strstr()的用法 实现将原字符串以分割串分割输出【转】
    Linux下使用popen()执行shell命令【转】
    linux下获取按键响应事件【转】
    linux select函数:Linux下select函数的使用详解【转】
    OTA升级中关于update.zip包的一些总结【转】
  • 原文地址:https://www.cnblogs.com/moxiaotao/p/9247042.html
Copyright © 2011-2022 走看看