zoukankan      html  css  js  c++  java
  • 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"/>
  • 相关阅读:
    07noip 统计数字 解题报告
    07noip 矩阵取数游戏 解题报告
    10 noip 乌龟棋 解题报告
    10 noip 引水入城 解题报告
    让我们来看一看C++ 简短自序
    curl post请求封装
    array_merge与数组加
    composer设置autoload自己的代码
    mysql使用笔记
    android入门:zxing学习笔记(六)
  • 原文地址:https://www.cnblogs.com/1540340840qls/p/7910042.html
Copyright © 2011-2022 走看看