一、在项目中添加dataSource所用到的包

dbcp数据源所需包:
    commons-dbcp.jar
    commons-pool.jar

C3P0数据源所需包:
    c3p0-0.9.1.2.jar

二、在需要用到数据源的类中添加DataSource属性和相应的set方法。

public class StudentDaoImpl implements IStudentDao {
//数据库类DataSource在javax.sql包下
private DataSource dataSource; public void save(Student student) {
Connection conn=null;
try {
//通过getConnection()方法得到数据库连接
conn = dataSource.getConnection();
//后面的代码和JDBC一样
PreparedStatement pStat = conn.prepareStatement("insert into student values (?,?)");
pStat.setInt(1, student.getId());
pStat.setString(2, student.getName());
pStat.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}finally{
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
System.out.println("save:"+student.getName());
} //为dataSource属性添加set方法,这样Spring才能把具体的数据源对象注入进来
public void setDataSource(DataSource dataSource) {
this.dataSource = dataSource;
} }

三、在Spring配置文件中添加DataSource的bean,并注入到用到DataSource的类中

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <!-- 数据源:C3P0 -->
<bean id="dataSource" destroy-method="close"
class="com.mchange.v2.c3p0.ComboPooledDataSource">
<!-- 基本属性:数据库驱动类、连接字符串、用户名、密码 -->
<property name="driverClass" value="oracle.jdbc.OracleDriver" />
<property name="jdbcUrl" value="jdbc:oracle:thin:@localhost:1521:ORCL" />
<property name="user" value="hib" />
<property name="password" value="hib" />
<!-- 连接数、最小连接数、最大连接数、最大空闲时间 -->
<property name="initialPoolSize" value="200"></property>
<property name="minPoolSize" value="50"></property>
<property name="maxPoolSize" value="300"></property>
<property name="maxIdleTime" value="60"></property>
</bean>
<!-- dbcp数据源配置 -->
<!--
<bean id="dataSource" destroy-method="close" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="oracle.jdbc.OracleDriver"/>
<property name="url" value="jdbc:oracle:thin:@localhost:1521:ORCL"/>
<property name="username" value="hib"/>
<property name="password" value="hib"/>
</bean> --> <bean id="studentDao" class="com.startspring.dao.impl.StudentDaoImpl">
<!-- 把dataSource注入给studentDao -->
<property name="dataSource" ref="dataSource"></property>
</bean> <bean id="studentService" class="com.startspring.service.impl.StudentServiceImpl">
<property name="studentDao" ref="studentDao" />
</bean> <bean id="start" class="com.startspring.Start">
<property name="studentService" ref="studentService" />
</bean>
</beans>

------------------------------------------------------------------------------------------------------------------------------

补充:把数据源的属性值写到properties文件中。

一、在项目里添加properties文件,如下:

oracle_c3p0.properties:

jdbc.driverClass=oracle.jdbc.OracleDriver
jdbc.url=jdbc:oracle:thin:@localhost:1521:ORCL
jdbc.user=hib
jdbc.password=hib

key值可以随便起,value值就是数据源的属性值

二、在Spring配置文件中添加propertyConfigurer bean,并把属性值用占位符代替,如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> <!-- 添加propertyConfigurer bean 这个类也是Spring提供的 -->
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<!-- 把properties文件的路径注入给location属性,前面要加classpath: -->
<property name="location" value="classpath:oracle_c3p0.properties"/>
</bean> <!-- 数据源:C3P0 -->
<bean id="dataSource" destroy-method="close" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<!-- 现在可以使用${} 占位符了,Spring会去取相应的value值 -->
<property name="driverClass" value="${jdbc.driverClass}" />
<property name="jdbcUrl" value="${jdbc.url}" />
<property name="user" value="${jdbc.user}" />
<property name="password" value="${jdbc.password}" />
</bean> <bean id="studentDao" class="com.startspring.dao.impl.StudentDaoImpl">
<!-- 把dataSource注入给studentDao -->
<property name="dataSource" ref="dataSource"></property>
</bean> <bean id="studentService" class="com.startspring.service.impl.StudentServiceImpl">
<property name="studentDao" ref="studentDao" />
</bean> <bean id="start" class="com.startspring.Start">
<property name="studentService" ref="studentService" />
</bean>
</beans>

Spring 注入数据源的更多相关文章

  1. spring 多数据源一致性事务方案

    spring 多数据源配置 spring 多数据源配置一般有两种方案: 1.在spring项目启动的时候直接配置两个不同的数据源,不同的sessionFactory.在dao 层根据不同业务自行选择使 ...

  2. Spring 管理数据源

    Spring 管理数据源 不管通过何种持久化技术,都必须通过数据连接访问数据库,在Spring中,数据连接是通过数据源获得的.在以往的应用中,数据源一般是Web应用服务器提供的.在Spring中,你不 ...

  3. spring 动态数据源

    1.动态数据源:  在一个项目中,有时候需要用到多个数据库,比如读写分离,数据库的分布式存储等等,这时我们要在项目中配置多个数据库. 2.原理:   (1).spring 单数据源获取数据连接过程: ...

  4. 深入理解Spring Boot数据源与连接池原理

    ​ Create by yster@foxmail.com 2018-8-2 一:开始 在使用Spring Boot数据源之前,我们一般会导入相关依赖.其中数据源核心依赖就是spring‐boot‐s ...

  5. 谈谈Spring 注入properties文件总结

    本篇谈谈Spring 注入properties文件总结,小编觉得挺不错的,现在分享给大家,也给大家做个参考.一起跟随小编过来看看吧 spring提供了多种方式来注入properties文件,本文做一个 ...

  6. 基于注解的Spring多数据源配置和使用(非事务)

    原文:基于注解的Spring多数据源配置和使用 1.创建DynamicDataSource类,继承AbstractRoutingDataSource package com.rps.dataSourc ...

  7. spring+hibernate 配置多个数据源过程 以及 spring中数据源的配置方式

    spring+hibernate 配置多个数据源过程 以及 spring中数据源的配置方式[部分内容转载] 2018年03月27日 18:58:41 守望dfdfdf 阅读数:62更多 个人分类: 工 ...

  8. Spring boot 数据源未配置异常

    问题 在使Springboot自动生成的项目框架时如果选择了数据源,比如选择了mysql,生成项目之后,启动会报一下异常: Description: Cannot determine embedded ...

  9. spring(16)------spring的数据源配置

    在spring中,通过XML的形式实现数据源的注入有三种形式. 一.使用spring自带的DriverManagerDataSource 使用DriverManagerDataSource配置数据源与 ...

随机推荐

  1. jquery插件datepicker

    jQuery UI很强大,其中的日期选择插件Datepicker是一个配置灵活的插件,我们可以自定义其展示方式,包括日期格式.语言.限制选择日期范围.添加相关按钮以及其它导航等. <script ...

  2. 关于android的SQLiteDatabase和Cursor的一些疑问

    android数据库操作的基础有三个类:SQLiteOpenHelper,SQLiteDatabase和Cursor.其中,SQLiteOpenHelper会建立一个数据库连接,它虽然可以调用多次ge ...

  3. Webfrom 生成流水号 组合查询 Repeater中单选与复选控件的使用 JS实战应用

                                             Default.aspx 网页界面 <%@ Page Language="C#" AutoE ...

  4. 被sjy带刷题#1

        笔记[问题描述]给定一个长度为m的序列a,下标编号为1~m.序列的每个元素都是1~n的整数.定义序列的代价为 你现在可以选择两个数x和y,并将序列a中所有的x改成y.x可以与y相等.请求出序列 ...

  5. 字符串在 UNICODE、MBCS编码下面的区别

    1:SBCS (single byte character set)单字节字符集.在这种编码格式下,所有字符都用一个字节表示.ASCII码就是单字节字符.用“0”来表示一个字节的结束.2 :Unico ...

  6. Centos安装php提示virtual memory exhausted: Cannot allocate memory

    由于内存不够,需要在php配置的时候./configure最后添加上 --disable-fileinfo >>./configure --prefix= ...........   -- ...

  7. 用Python设计第一个游戏 - 零基础入门学习Python002

    用Python设计第一个游戏 让编程改变世界 Change the world by program 有些鱼油可能会说,哇,小甲鱼你开玩笑呐!这这这这就上游戏啦?你不打算给我们讲讲变量,分支,循环,条 ...

  8. ASP.NET MVC5 生成验证码

    1 ValidateCode.cs using System; using System.Drawing; using System.Drawing.Drawing2D; using System.D ...

  9. Resharper使用

    Resharper进阶一:简要介绍 官方下载地址下载地址  注册码: 用户名: User 注册码:D9d09DSYJel9IyuDU4btAQwZcbLugUad 面对这样一个问题:为什么.net能够 ...

  10. wordpress教程之get_posts()

    get_posts 介绍 June 3rd 2012 评论(16) get_posts 函数,简单的来讲是 get_post 的复数新形势,但因为是文章多篇提取,所以使用方法上却略有不同,支持众多参数 ...