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配置数据源与 ...
随机推荐
- JS 逗号表达式
JavaScript中逗号运算符 JavaScript中逗号运算符(,)是顺序执行两个表达式.使用方法: expression1, expression2 其中expression1是任何表达式.ex ...
- MIT scheme入门使用
在win7下可安装MIT-GUN scheme, 点开后有两个界面:一个交互式命令行界面:一个Edwin界面. 在命令行界面按Ctrl-G可以开始输入.在Edwin界面,输入完整命令后按Ctrl ...
- 【转载】ADO.NET与ORM的比较(3):Linq to SQL实现CRUD
[转载]ADO.NET与ORM的比较(3):Linq to SQL实现CRUD 说明:个人感觉在Java领域大型开发都离不了ORM的身影,所谓的SSH就是Spring+Struts+Hibernate ...
- div没有设置高度时背景颜色不显示(浮动)
在使用div+css进行网页布局时,如果外部div有背景颜色或者边框,而不设置其高度,在IE浏览器下显示正常.但是使用Firefox/opera浏览时却出现最外层Div的背景颜色和边框不起作用的问题. ...
- Comparator TreeSet
package study; import java.util.Comparator;import java.util.TreeSet; public class TreeSetTest { publ ...
- Mac OS X和iOS上基本数据类型的字节数
在Mac和IPhone上分别运行下面的程序: NSLog(@"char size = %lu", sizeof(char)); NSLog(@"short int siz ...
- mysql 使用set names 解决乱码问题的原理
解决乱码的方法,我们经常使用“set names utf8”,那么为什么加上这句代码就可以解决了呢?下面跟着我一起来深入set names utf8的内部执行原理 先说MySQL的字符集问题.Wind ...
- Bootstrap 静态分页 和 jquery_pagination插件 动态分页
第一种Bootstrap 实例 - 默认的分页 <!DOCTYPE html> <html> <head> <title>Bootstrap 实例 - ...
- 【Ecstore】为自建模块添加自定义主题模板
做好ECSOTRE模块后,需在主题中添加模板,而添加模板页面时只有产品分类页.产品详细页.首页等内置模块的模板类型. 下面介绍如何添加一个自定义的模板类型“buildings”. 一.修改(建议复制到 ...
- 浅谈window.attachEvent
以前写 JavaScript 脚本时,事件都是采用object.event = handler;的方式初始化.这种方式对于 Internet Explorer.Mozilla/Firefox 和 Op ...