• 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"/>
  • 相关阅读:
    python学习之路——数字类型字符串类型列表类型 day5(18/9/17)
    python学习之路——作业 day5(18/9/17)
    python学习之路——作业 (18/9/15)
    python学习之路——作业 day4(18/9/14)
    Flask-Migrate
    Flask-Script
    Flask-SQLAlchemy
    SQLAlchemy 增删改查 一对多 多对多
    MongoDB 基础
    Flask 模板语法
  • 原文地址:https://www.cnblogs.com/1540340840qls/p/7910042.html
Copyright © 2020-2023  润新知