• spring中PropertyPlaceholderConfigurer的运用---使用${property-name}取值


    代码如下:

    配置文件:

    jdbc.properties的代码如下:

    1 jdbc.driverClassName=org.hsqldb.jdbcDriver
    2 jdbc.url=jdbc:hsqldb:hsql://production:9002
    3 jdbc.username=sa
    4 jdbc.password=root

    applicationContext-properties-placeholder.xml 代码如下:

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <beans xmlns="http://www.springframework.org/schema/beans"
     3        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     4        xmlns:p="http://www.springframework.org/schema/p"
     5        xmlns:c="http://www.springframework.org/schema/c"
     6        xmlns:aop="http://www.springframework.org/schema/aop"
     7        xsi:schemaLocation="http://www.springframework.org/schema/beans
     8                 http://www.springframework.org/schema/beans/spring-beans.xsd
     9 http://www.springframework.org/schema/aop
    10 http://www.springframework.org/schema/aop/spring-aop.xsd">
    11 
    12     <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    13         <property name="locations" value="classpath:jdbc.properties"/>
    14     </bean>
    15     <bean id="dataSource" destroy-method="close"
    16           class="org.apache.commons.dbcp.BasicDataSource">
    17         <property name="driverClassName" value="${jdbc.driverClassName}"/>
    18         <property name="url" value="${jdbc.url}"/>
    19         <property name="username" value="${jdbc.username}"/>
    20         <property name="password" value="${jdbc.password}"/>
    21     </bean>
    22 </beans>

    上面的红色部分均按照:${property-name}取值.

    测试类的代码如下:

    package com.timo.test;
    
    import org.apache.commons.dbcp.BasicDataSource;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    public class Test19 {
        public static void main(String[] args) {
            ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext-properties-placeholder.xml");
            BasicDataSource dataSource = applicationContext.getBean(BasicDataSource.class);
            System.out.println("driverClassName="+dataSource.getDriverClassName());
        }
    }

     上面xml配置文件中

    <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
            <property name="locations" value="classpath:jdbc.properties"/>
        </bean>
    等效于
    <context:property-placeholder location="classpath:jdbc.properties"/>
  • 相关阅读:
    MS CRM 2011插件调试工具
    MSCRM 相關 (到石頭居博客查看)
    es6 复习
    HTML阶段笔试题附答案
    CSS选择器
    jQuery 效果知识总结
    markdown基本语法
    HTML5给我们带来了什么?
    H5新增语义化标签
    c#中去掉字符串空格方法
  • 原文地址:https://www.cnblogs.com/1540340840qls/p/7910042.html
Copyright © 2020-2023  润新知