一、在Spring的基础上还要另外导入c3p0包和mysql的驱动包。

二、配置文件,

jdbc.propertices:这里只做了一些简单配置

user=root
password=123
driverClass=com.mysql.jdbc.Driver
jdbcUrl=jdbc\:mysql\:///test

三、spring配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd"> <!-- 导入外部配置文件-->
<context:property-placeholder location="classpath:jdbc.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>

四、测试:

package spring.beans.properties.test;

import java.sql.SQLException;

import javax.sql.DataSource;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class Main { public static void main(String[] args) throws SQLException {
ApplicationContext ctx=new ClassPathXmlApplicationContext("beans-properties.xml"); DataSource dataSource=(DataSource) ctx.getBean("dataSource");
System.out.println(dataSource.getConnection());
} }

五、输出

  数据库中有对应数据库test,并配置成功,会输出

com.mchange.v2.c3p0.impl.NewProxyConnection@28ac3dc3

Spring初学之使用外部配置文件dataSource的更多相关文章

  1. Spring 加载项目外部配置文件

    背景 在项目的部署过程中,一般是打成 war 或者 jar 包,这样一般存在两种问题: 即使是配置文件修改,也还需要整个项目重新打包和部署. 整个项目只有一套环境,不能切换. 针对上面的问题,可以使用 ...

  2. Spring配置文件中如何使用外部配置文件配置数据库连接

    直接在spring的配置文件中applicationContext.xml文件中配置数据库连接也可以,但是有个问题,需要在url后带着使用编码集和指定编码集,出现了如下问题,&这个符号报错-- ...

  3. docker 部署spring.boot项目【一】(引用外部配置文件)

    上一篇随笔,nginx是启动运行在容器内,spring.boot的web项目是运行在宿主内,这一篇的目的,是把web项目也制作成镜像,然后在容器里启动. 文件目录结构如下: 主要文件结构说明:(1)b ...

  4. application.properties多环境配置文件、jar包外部配置文件、配置项加密、程序中配置使用

    一.简介 spring boot项目application.properties文件存放及使用介绍 二.方法一多环境配置文件 我们一般都会有多个应用环境,开发环境.测试环境.生产环境,各个环境的配置会 ...

  5. Spring Boot配置,读取配置文件

    Spring Boot配置,读取配置文件 一.配置Spring Boot 1.1 服务器配置 1.2 使用其他Web服务器 1.3 配置启动信息 1.4 配置浏览器显示ico 1.5 Yaml语法 1 ...

  6. 通过外部配置文件做mybatis的基础配置,以及Mapper代理接口的实现

    1.通过外部配置文件做mybatis的基础性的配置. 1)先编写config.properties的文件(做一些动态的配置). 配置的内容如下: jdbc.jdbcUrl=jdbc:oracle:th ...

  7. spring+hibernate+jpa+Druid的配置文件,spring整合Druid

    spring+hibernate+jpa+Druid的配置文件 spring+hibernate+jpa+Druid的完整配置 spring+hibernate+jpa+Druid的数据源配置 spr ...

  8. Spring监管下的Hibernate配置文件

    今天看了看别人的程序,用的是SSH搭建的,自己回忆了下感觉假设採用注解的话那么Hibernate的配置文件hibernate.cfg.xml是还须要的,而*.hbm.xml则能够被注解所替代的,结果确 ...

  9. Spring初学之spring的事务管理xml

    所有的java类都是用的上一篇文章:Spring初学之spring的事务管理 不同的是,这时xml配置事务,所以就要把java类中的那些关于spring的注解都删掉,然后在xml中配置,Applica ...

随机推荐

  1. c#实现一个打砖块游戏step by step---开篇

    一 引子 为了让更多的编程初学者,轻松愉快地掌握面向对象的思考方法,对象继承和多态的妙用,故推出此系列随笔,还望大家多多支持. 二 游戏截图与说明 1. 游戏截图 2. 游戏说明: 蓝色砖块砖块为普通 ...

  2. 【BZOJ3158】千钧一发 最小割

    [BZOJ3158]千钧一发 Description Input 第一行一个正整数N. 第二行共包括N个正整数,第 个正整数表示Ai. 第三行共包括N个正整数,第 个正整数表示Bi. Output 共 ...

  3. 【BZOJ3930】[CQOI2015]选数 莫比乌斯反演

    [BZOJ3930][CQOI2015]选数 Description 我们知道,从区间[L,H](L和H为整数)中选取N个整数,总共有(H-L+1)^N种方案.小z很好奇这样选出的数的最大公约数的规律 ...

  4. L - Sum It Up(DFS)

    L - Sum It Up Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u Descr ...

  5. phpwind 论坛 转移

    前段时间用phpwind 搭建了一个本地论坛系统,也写过一篇随笔,讲phpwind论坛的迁移,昨天网上又对论坛做了迁移,在本地搭建了系统. 使用的是之前没有成功的方法.这种方法挺方便的,之前每次都是重 ...

  6. Linux中的服务管理

    RPM包默认安装的服务 查看已安装的服务: chkconfig --list 默认安装位置: /etc/init.d 启动脚本 /etc/sysconfig 初始化环境配置文件 /etc  配置文件位 ...

  7. 002-java反编译工具jd-gui

    官网:https://github.com/java-decompiler 下载:https://github.com/java-decompiler/jd-gui/releases 使用: java ...

  8. 阿里云公网IP不能使用

    1.开通专用网络 2.在ECS的安全组 创建 专用网络 3.配置规则 4.快速创建规则,增加自己需要入网的端口号,授权对象写:0.0.0.0/0

  9. JAVA中遍历Map和Set方法,取出map中所有的key

    Java遍历Set集合 1.迭代器遍历: Set<String> set = new HashSet<String>(); Iterator<String> it ...

  10. python读写Excel文件--使用xlrd模块读取,xlwt模块写入

    一.安装xlrd模块和xlwt模块 1. 下载xlrd模块和xlwt模块 到python官网http://pypi.python.org/pypi/xlrd下载模块.下载的文件例如:xlrd-0.9. ...