- <?xml version="1.0"encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http://www.springframework.org/schema/context"
- xmlns:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx"
- xmlns:jee="http://www.springframework.org/schema/jee"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context-2.5.xsd
- http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
- http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd
- http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
- <context:property-placeholder location="classpath:jdbc_config.properties"/>
- <!--
- DriverManagerDataSource:在每个连接请求时新建一个连接。
- SingleConnectionDataSource:在每个连接请求时都返回同一连接。
- -->
- <bean id="dataSource"class="org.springframework.jdbc.datasource.DriverManagerDataSource">
- <property name="driverClassName"value="${driverClassName}"/>
- <property name="url"value="${url}"/>
- <property name="username"value="${username}"/>
- <property name="password"value="${password}"/>
- </bean>
- </beans>
。。。。。
- <context:property-placeholder location="classpath:dbcp_config.properties"/>
- <bean id="dataSource"class="org.apache.commons.dbcp.BasicDataSource"
- destroy-method="close">
- <property name="driverClassName"value="${driverClassName}"/>
- <property name="url"value="${url}"/>
- <property name="username"value="${username}"/>
- <property name="password"value="${password}"/>
- <property name="initialSize"value="${initialSize}"/>
- <property name="maxActive"value="${maxActive}"/>
- <property name="maxIdle"value="${maxIdle}"/>
- <property name="minIdle"value="${minIdle}"/>
- <property name="maxWait"value="${maxWait}"/>
- <property name="defaultAutoCommit"value="${defaultAutoCommit}"/>
- </bean>
。。。。。
- <context:property-placeholder location="classpath:c3p0_config.properties"/>
- <bean id="dataSource"
- class="com.mchange.v2.c3p0.ComboPooledDataSource">
- <property name="user"value="${user}"/>
- <property name="password"value="${password}"/>
- <property name="jdbcUrl"value="${jdbcUrl}"/>
- <property name="driverClass"value="${driverClass}"/>
- <property name="checkoutTimeout"value="${checkoutTimeout}"/>
- <property name="idleConnectionTestPeriod"value="${idleConnectionTestPeriod}"/>
- <property name="initialPoolSize"value="${initialPoolSize}"/>
- <property name="maxIdleTime"value="${maxIdleTime}"/>
- <property name="maxPoolSize"value="${maxPoolSize}"/>
- <property name="minPoolSize"value="${minPoolSize}"/>
- <property name="maxStatements"value="${maxStatements}"/>
- </bean>
。。。。。
- <!--
- <bean id="dataSource"class="org.springframework.jndi.JndiObjectFactoryBean"
- scope="singleton">
- <property name="jndiName"value="/jdbc/spring-jndi"/>
- < !--
- 当resourceRef 属性为true时,jndiName会被添加java:comp/env/,从应用服务器的JNDI目录获取数据源
- -- >
- <property name="resourceRef"ref="true"/>
- </bean>
- -->
- <!-- jee 命名空间里的jee:jndi-lookup 元素可以从jndi获取对象。下面的XML等效于前面对JndiObjectFactoryBean的明确声明 -->
- <jee:jndi-lookup id="dataSource"jndi-name="/jdbc/spring-jndi"resource-ref="true"/>
。。。。。
测试使用一下
- <importresource="classpath:bean-dbcp.xml"/>
- <bean id="helloJdbcDao"class="cn.partner4java.jdbc.impl.HelloJdbcDaoBean">
- <property name="dataSource"ref="dataSource"/>
- </bean>
。。。。。。
- packagecn.partner4java.jdbc;
- importjava.sql.Connection;
- importjava.sql.SQLException;
- /**
- * 存jdbc练习
- * @author wangchanglong
- *
- */
- publicinterfaceHelloJdbcDao {
- publicConnection getConnection()throwsSQLException;
- }
。。。。。。
- packagecn.partner4java.jdbc.impl;
- importjava.sql.Connection;
- importjava.sql.SQLException;
- importjavax.sql.DataSource;
- importcn.partner4java.jdbc.HelloJdbcDao;
- /**
- * 最简单的练习
- * @author wangchanglong
- *
- */
- publicclassHelloJdbcDaoBeanimplementsHelloJdbcDao{
- publicDataSource dataSource;
- publicvoidsetDataSource(DataSource dataSource) {
- this.dataSource = dataSource;
- }
- publicConnection getConnection()throwsSQLException {
- returndataSource.getConnection();
- }
- }
。。。。。
- packagecn.partner4java.jdbc.junit;
- importjava.sql.SQLException;
- importorg.springframework.context.ApplicationContext;
- importorg.springframework.context.support.ClassPathXmlApplicationContext;
- importcn.partner4java.jdbc.HelloJdbcDao;
- importjunit.framework.TestCase;
- publicclassHelloJdbcDaoBeanTestextendsTestCase {
- publicHelloJdbcDao helloJdbcDao;
- @Override
- protectedvoidsetUp()throwsException {
- ApplicationContext ac =newClassPathXmlApplicationContext("beans-jdbc.xml");
- helloJdbcDao = (HelloJdbcDao) ac.getBean("helloJdbcDao");
- }
- publicvoidtestGetConn()throwsSQLException{
- System.out.println(helloJdbcDao.getConnection());
- }
- }