spring的各种dataSource配置

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"  
  4. xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"  
  5.   xmlns:jee="http://www.springframework.org/schema/jee"   
  6. xsi:schemaLocation="http://www.springframework.org/schema/beans  
  7.            http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  
  8.            http://www.springframework.org/schema/context  
  9.            http://www.springframework.org/schema/context/spring-context-2.5.xsd  
  10.            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd   
  11.             http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd    
  12.             http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">  
  13. <context:property-placeholder location="classpath:jdbc_config.properties"/>  
  14. <!--   
  15.   DriverManagerDataSource:在每个连接请求时新建一个连接。  
  16.   SingleConnectionDataSource:在每个连接请求时都返回同一连接。  
  17.   -->  
  18. <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">  
  19.   <property name="driverClassName" value="${driverClassName}"/>  
  20.   <property name="url" value="${url}"/>  
  21.   <property name="username" value="${username}"/>  
  22.   <property name="password" value="${password}"/>  
  23. </bean>   
  24. </beans>   
  25.   


。。。。。 
  1. <context:property-placeholder location="classpath:dbcp_config.properties" />  
  2.   <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"  
  3.   destroy-method="close">  
  4.   <property name="driverClassName" value="${driverClassName}"/>  
  5.   <property name="url" value="${url}"/>  
  6.   <property name="username" value="${username}"/>  
  7.   <property name="password" value="${password}"/>  
  8.   <property name="initialSize" value="${initialSize}"/>  
  9.   <property name="maxActive" value="${maxActive}"/>  
  10.   <property name="maxIdle" value="${maxIdle}"/>  
  11.   <property name="minIdle" value="${minIdle}"/>  
  12.   <property name="maxWait" value="${maxWait}"/>  
  13.   <property name="defaultAutoCommit" value="${defaultAutoCommit}"/>  
  14. </bean>  
  15.   

。。。。。 
  1. <context:property-placeholder location="classpath:c3p0_config.properties"/>  
  2. <bean id="dataSource"  
  3.   class="com.mchange.v2.c3p0.ComboPooledDataSource">  
  4.   <property name="user" value="${user}"/>   
  5.   <property name="password" value="${password}"/>   
  6.   <property name="jdbcUrl" value="${jdbcUrl}"/>   
  7.   <property name="driverClass" value="${driverClass}"/>   
  8.     
  9.   <property name="checkoutTimeout" value="${checkoutTimeout}"/>   
  10.   <property name="idleConnectionTestPeriod" value="${idleConnectionTestPeriod}"/>   
  11.   <property name="initialPoolSize" value="${initialPoolSize}"/>   
  12.   <property name="maxIdleTime" value="${maxIdleTime}"/>   
  13.     
  14.   <property name="maxPoolSize" value="${maxPoolSize}"/>   
  15.   <property name="minPoolSize" value="${minPoolSize}"/>   
  16.   <property name="maxStatements" value="${maxStatements}"/>   
  17. </bean>  
  18.   

。。。。。 
  1.   
  2. <!--    
  3. <bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean"  
  4.   scope="singleton">  
  5.   <property name="jndiName" value="/jdbc/spring-jndi" />  
  6.   < !--  
  7.    当resourceRef 属性为true时,jndiName会被添加java:comp/env/,从应用服务器的JNDI目录获取数据源  
  8.   -- >  
  9.   <property name="resourceRef" ref="true" />  
  10. </bean>  
  11. -->  
  12. <!-- jee 命名空间里的jee:jndi-lookup 元素可以从jndi获取对象。下面的XML等效于前面对JndiObjectFactoryBean的明确声明 -->  
  13. <jee:jndi-lookup id="dataSource" jndi-name="/jdbc/spring-jndi" resource-ref="true"/>  
  14.   
  15.   


。。。。。 


测试使用一下 
  1. <import resource="classpath:bean-dbcp.xml" />  
  2. <bean id="helloJdbcDao" class="cn.partner4java.jdbc.impl.HelloJdbcDaoBean">  
  3.   <property name="dataSource" ref="dataSource"/>  
  4. </bean>  
  5.   

。。。。。。 
  1. package cn.partner4java.jdbc;  
  2. import java.sql.Connection;  
  3. import java.sql.SQLException;  
  4. /** 
  5. * 存jdbc练习 
  6. * @author wangchanglong 
  7. * 
  8. */  
  9. public interface HelloJdbcDao {  
  10. public Connection getConnection() throws SQLException;  
  11. }  
  12.   

。。。。。。 
  1. package cn.partner4java.jdbc.impl;  
  2. import java.sql.Connection;  
  3. import java.sql.SQLException;  
  4. import javax.sql.DataSource;  
  5. import cn.partner4java.jdbc.HelloJdbcDao;  
  6. /** 
  7. * 最简单的练习 
  8. * @author wangchanglong 
  9. * 
  10. */  
  11. public class HelloJdbcDaoBean implements HelloJdbcDao{  
  12. public DataSource dataSource;  
  13. public void setDataSource(DataSource dataSource) {  
  14.   this.dataSource = dataSource;  
  15. }  
  16. public Connection getConnection() throws SQLException {  
  17.   return dataSource.getConnection();  
  18. }  
  19.   
  20. }  
  21.   

。。。。。 
  1. package cn.partner4java.jdbc.junit;  
  2. import java.sql.SQLException;  
  3. import org.springframework.context.ApplicationContext;  
  4. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  5. import cn.partner4java.jdbc.HelloJdbcDao;  
  6. import junit.framework.TestCase;  
  7. public class HelloJdbcDaoBeanTest extends TestCase {  
  8. public HelloJdbcDao helloJdbcDao;  
  9. @Override  
  10. protected void setUp() throws Exception {  
  11.   ApplicationContext ac = new ClassPathXmlApplicationContext("beans-jdbc.xml");  
  12.   helloJdbcDao = (HelloJdbcDao) ac.getBean("helloJdbcDao");  
  13. }  
  14.   
  15. public void testGetConn() throws SQLException{  
  16.   System.out.println(helloJdbcDao.getConnection());  
  17. }  
  18.   
  19. }  
  20.   

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值