• Spring4.0学习笔记(4) —— 使用外部属性文件


    1、配置xml文件

    <?xml version="1.0" encoding="UTF-8"?>
    <beans
        xmlns="http://www.springframework.org/schema/beans"
        xmlns:context="http://www.springframework.org/schema/context"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:p="http://www.springframework.org/schema/p"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
    
        <context:property-placeholder location="classpath:db.properties"/>
    
        <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
            <property name="user" value="${user}"></property>
            <property name="password" value="${password}"></property>
            <property name="driverClass" value="${driverclass}"></property>
            <property name="jdbcUrl" value="${jdbcurl}"></property>
        </bean>
    
    </beans>

    2、外部配置文件

    user=root
    password=admin
    driverclass=com.mysql.jdbc.Driver
    jdbcUrl=jdbc:mysql:///authority

    3、main方法

    package com.spring.properties;
    
    import java.sql.SQLException;
    
    import javax.sql.DataSource;
    
    import org.springframework.context.*;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    public class Main {
        public static void main(String[] args) throws SQLException {
            ApplicationContext ctx = new ClassPathXmlApplicationContext("bean-properties.xml");
            
            DataSource dataSource = (DataSource)ctx.getBean("dataSource");
            System.out.println(dataSource.getConnection());
        }
    }
  • 相关阅读:
    mysql批量替换指定字符串
    php中英字符串截取
    比较两个JSON字符串是否完全相等
    Java FastJson 介绍
    线程池
    DBUS及常用接口介绍
    在Mac中如何正确地设置JAVA_HOME
    base64 原理
    sizeof与strlen的区别
    Kubernetes 部署失败的 10 个最普遍原因
  • 原文地址:https://www.cnblogs.com/cklovefan/p/5292548.html
Copyright © 2020-2023  润新知