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配置数据源与 ...
随机推荐
- servlet乱码以及解决
// 浏览器提交的数据是000110011(码表中对应的<编码> )等东西. // 浏览器以什么<码表>打开浏览器(而空中浏览器使用的编码是:<meta http-eq ...
- vs2013 JS代码提示
1.JS提示 在Js文件头部加 /// <reference path="ext-all-dev.js" /> 要求引用的js和本js在同一目录,否则需要全部路径
- Struts2学习笔记 国际化(Internationalization)
概述 国际化(Internationalization),通途的讲,就是让软件实现对多种语言的支持.可以通过简单的设置就可以从一种语言切换到另一种语言.用的最多的地方就是在应用程序的界面表示上.我们经 ...
- kinect
1.学习资料 http://blog.csdn.net/dustpg/article/details/37982311 https://github.com/mdkus/kinect-mssdk-op ...
- C++11新特性
c++语言的扩展和修正,不仅包含了核心语言的新机能,而且扩展了c++标准库(STL),并引入了大部分的C++ technical report 1程序库 C++11还包括大量新特性:包括lambda表 ...
- Shell glob
在Linux中,glob是用来匹配路径名的通配符,glob主要包含以下4种: Wildcard Matching * 匹配0个或者多个字符,比如d*,可以匹配d, d1, dd1 ?匹配单个字符,比如 ...
- 安卓tabhost和子Activity通信方法
发现很多同学在问这个问题 1个tabhost对应的类假设是T,假设包含2个子tab,每个是1个activity,假设叫a,b,对应的类假设是A和B 那么有这几个问题 1.a如何调用b的方法,或者说发送 ...
- 清除浮动 .clearfix
子元素的浮动 清除子元素的浮动 .clearfix{zoom:1}/*IE 6-8*/.clearfix:after{content:'\20';display:block;height:0;clea ...
- python3.4 伪装成浏览器获取页面信息失败
最近学了下网络爬虫,打算从一个网站上提取点东西,自己练练手,刚开始还从这个网站上取了正确的html,后来百般尝试还是不能取正确的html,希望能得到大家的帮助~ 我刚开始的代码是: 1 url=&qu ...
- wordpress教程之the_author_meta()显示用户的信息
描述 模板标签函数the_author_meta可以显示用户数据.如果该函数在文章主循环(Loop)中,则不必指定作者的ID值,标签所显示的就是当前文章作者的内容.如果在主循环(Loop)外,则需要指 ...