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"/>
  • 相关阅读:
    Oracle 11g+Windows10 x64安装、配置过程记录
    json工具类
    restTemplate工具类
    VirtualBox中安装CentOS 7后无法上网问题
    VirtualBox中安装CentOS 7
    idea安装完成后要做的几件事(设置字体、编码、行号)
    MiniUI学习笔记1-新手必读
    BUU-[GKCTF2020]WannaReverse
    BUU-Dragon Quest
    BUU-EzObfus-Chapter2
  • 原文地址:https://www.cnblogs.com/1540340840qls/p/7910042.html
Copyright © 2011-2022 走看看