Spring 注入数据源
一、在项目中添加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 注入数据源的更多相关文章
- spring 多数据源一致性事务方案
spring 多数据源配置 spring 多数据源配置一般有两种方案: 1.在spring项目启动的时候直接配置两个不同的数据源,不同的sessionFactory.在dao 层根据不同业务自行选择使 ...
- Spring 管理数据源
Spring 管理数据源 不管通过何种持久化技术,都必须通过数据连接访问数据库,在Spring中,数据连接是通过数据源获得的.在以往的应用中,数据源一般是Web应用服务器提供的.在Spring中,你不 ...
- spring 动态数据源
1.动态数据源: 在一个项目中,有时候需要用到多个数据库,比如读写分离,数据库的分布式存储等等,这时我们要在项目中配置多个数据库. 2.原理: (1).spring 单数据源获取数据连接过程: ...
- 深入理解Spring Boot数据源与连接池原理
Create by yster@foxmail.com 2018-8-2 一:开始 在使用Spring Boot数据源之前,我们一般会导入相关依赖.其中数据源核心依赖就是spring‐boot‐s ...
- 谈谈Spring 注入properties文件总结
本篇谈谈Spring 注入properties文件总结,小编觉得挺不错的,现在分享给大家,也给大家做个参考.一起跟随小编过来看看吧 spring提供了多种方式来注入properties文件,本文做一个 ...
- 基于注解的Spring多数据源配置和使用(非事务)
原文:基于注解的Spring多数据源配置和使用 1.创建DynamicDataSource类,继承AbstractRoutingDataSource package com.rps.dataSourc ...
- spring+hibernate 配置多个数据源过程 以及 spring中数据源的配置方式
spring+hibernate 配置多个数据源过程 以及 spring中数据源的配置方式[部分内容转载] 2018年03月27日 18:58:41 守望dfdfdf 阅读数:62更多 个人分类: 工 ...
- Spring boot 数据源未配置异常
问题 在使Springboot自动生成的项目框架时如果选择了数据源,比如选择了mysql,生成项目之后,启动会报一下异常: Description: Cannot determine embedded ...
- spring(16)------spring的数据源配置
在spring中,通过XML的形式实现数据源的注入有三种形式. 一.使用spring自带的DriverManagerDataSource 使用DriverManagerDataSource配置数据源与 ...
随机推荐
- Android中获取apk基本信息
一 PackageManager可以获得的所有包节点信息: 1,所有节点的基类:PackageItemInfo: 2,PackageInfo:package的全面信息,与AndroidManifest ...
- 关于Emit中动态类型TypeBuilder创建类标记的一点思考
利用TypeBuilder是可以动态创建一个类型,现在有个需求,动态生成一个dll,创建类型EmployeeEx,需要继承原dll里面的Employee类,并包含Employee类上的所有类标记. ...
- SQL Server事务的存储过程
在酒店管理系统开发中,我们会创建房间表和房间类型表(房型表)这两个表,如下图所示: 房型表:RoomType 房间表:Room 首先这两个表的关系:Room是从表,RoomType是主表,两表有主外键 ...
- MJRefresh(上拉加载下拉刷新)
整理自:https://github.com/CoderMJLee/MJRefresh#%E6%94%AF%E6%8C%81%E5%93%AA%E4%BA%9B%E6%8E%A7%E4%BB%B6%E ...
- 拥有iframe页面的子父类窗口调用JS的方法,并且注意的事项
一.前言 我页面用的是EasyUI的弹出窗口里面嵌入一个iframe.第一:父窗口打开子窗口是一个新增用户信息的iframe子页面,点击保存后,子窗口iframe则去调用父窗口的function cl ...
- activiti笔记三 Activiti问题重现
测试的时候出现一个异常: ContextLoader.initWebApplicationContext(308) | Context initialization failed org.spring ...
- php对数组中指定键值排序
function array_sort($arr,$keys,$type='asc'){ $keysvalue = $new_array = array(); foreach ($arr as $k= ...
- 网关协议学习:CGI、FastCGI、WSGI、uWSGI
一直对这四者的概念和区别很模糊,现在就特意梳理一下它们的关系与区别. CGI CGI即通用网关接口(Common Gateway Interface),是外部应用程序(CGI程序)与Web服务器之间的 ...
- 异常处理与调试2 - 零基础入门学习Delphi51
异常处理与调试2 让编程改变世界 Change the world by program Delphi 异常类 利用异常机制,就是允许写代码时可以假设:如果用户可以得到子程序调用或计算的结果,这些结果 ...
- 凸包(hd1392)
Surround the Trees Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...