`

spring 注入datasource

阅读更多

Bean: 
代码: 
package onlyfun.caterpillar; 
                                                                                
import javax.sql.DataSource; 
import java.sql.Connection; 
                                                                                
public class DataBean { 
    private DataSource dataSource; 
                                                                                
    public void setDataSource(DataSource dataSource) { 
        this.dataSource = dataSource; 
    } 
                                                                                
    public void testDataSource() { 
        try { 
            Connection connection = dataSource.getConnection(); 
            if(connection != null) 
                System.out.println("test ok!"); 
        } 
        catch (Exception e) { 
            e.printStackTrace(); 
        } 
    } 



    这是一个简单的测试Spring DataSource注入的程式,我们通过javax.sql.DataSource接口来注入资料来源,Spring提供了org.springframework.jdbc.datasource.DriverManagerDataSource来取得DataSource,它实现了javax.sql.DataSource,您将之当作一个Bean,之后再注入DataBean中即可,Bean.xml可以这么写: 
代码: 
<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE beans PUBLIC "-//SPRING/DTD BEAN/EN" "http://www.springframework.org/dtd/spring-beans.dtd"> 
<beans> 
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 
        <property name="driverClassName"> 
            <value>com.mysql.jdbc.Driver</value> 
        </property> 
        <property name="url"> 
            <value>jdbc:mysql://localhost:3306/TestDB</value> 
        </property> 
        <property name="username"> 
            <value>caterpillar</value> 
        </property> 
        <property name="password"> 
            <value>123456</value> 
        </property> 
    </bean> 
                                                                                
    <bean id="dataBean" class="onlyfun.caterpillar.DataBean"> 
        <property name="dataSource"> 
            <ref bean="dataSource"/> 
        </property> 
    </bean> 
</beans> 


    如果您之前只使用spring-core.jar这个类库,您还必须加入spring-dao.jar,
org.springframework.jdbc.datasource.DriverManagerDataSource是包括在这个类库中,如果您使用的是spring.jar,当中已经包括了,无需加入任何的jar,当然,为了使用JDBC,您必须要有JDBC驱动程序的jar档。 

可以用下面这段程式简单的测试一下: 
代码: 
  BeanDefinitionRegistry reg = new DefaultListableBeanFactory(); 
  XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(reg); 
                                                                                
  reader.loadBeanDefinitions(new ClassPathResource("bean.xml"));; 
                                                                                
  BeanFactory bf = (BeanFactory) reg; 
  DataBean dataBean = (DataBean) bf.getBean("dataBean"); 
  dataBean.testDataSource(); 

   DriverManagerDataSource并没有提供连接池的功能,只能作简单的连接测试,现在假设连接测试没有问题了,您想要换上DBCP以获得连接池的功能,则原程序不用更动,只要改改Bean定义档就可以了: 
代码: 
<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE beans PUBLIC "-//SPRING/DTD BEAN/EN" "http://www.springframework.org/dtd/spring-beans.dtd"> 
<beans> 
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> 
        <property name="driverClassName"> 
            <value>com.mysql.jdbc.Driver</value> 
        </property> 
        <property name="url"> 
            <value>jdbc:mysql://localhost:3306/TestDB</value> 
        </property> 
        <property name="username"> 
            <value>caterpillar</value> 
        </property> 
        <property name="password"> 
            <value>123456</value> 
        </property> 
    </bean> 
    <bean id="dataBean" class="onlyfun.caterpillar.DataBean"> 
        <property name="dataSource"> 
            <ref bean="dataSource"/> 
        </property> 
    </bean> 
</beans> 


    现在我们使用的是org.apache.commons.dbcp.BasicDataSource作为注入的DataSource源,为了使用DBCP的功能,您必须要将commons-dbcp.jar加入CLASSPATH中,另外您还需要commons-pool.jar与commons-collections.jar,这些都可以在Spring的相依版本中的lib目录下找到。 
    注意到我們在dataSource Bean上宣告了destroy-method,如此可以确保BeanFactory在关闭进也一并关闭BasicDataSource。 

    如果您的Servlet容器提供了JNDI资料源,您也可以简单的换上这个资料源: 
代码: 
<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE beans PUBLIC "-//SPRING/DTD BEAN/EN" "http://www.springframework.org/dtd/spring-beans.dtd"> 
<beans> 
    <bean id="dataSource" class="org.springframework.indi.JndiObjectFactoryBean"> 
        <property name="jndiName"> 
            <value>jdbc/TestDB</value> 
        </property> 
    </bean> 
    <bean id="dataBean" class="onlyfun.caterpillar.DataBean"> 
        <property name="dataSource"> 
            <ref bean="dataSource"/> 
        </property> 
    </bean> 
</beans> 

   为了使用org.springframework.indi.JndiObjectFactoryBean,您必须加入spring-context.jar这个类库,jndiName实际上要根据您所设定的JNDI查询名称,您可以在下面这个网址找到有关于Tomcat中JNDI设定的方式: 
http://www.caterpillar.onlyfun.net/phpBB2/viewtopic.php?t=1354

分享到:
评论

相关推荐

    SpringBoot框架Datasource注入

    该项目采用标签形式对Datasource进行注入将Datasource组件交给容器进行统一管理

    从零开始学Spring Boot

    1.8 Spring Boot datasource - mysql 1.9 JPA - Hibernate 1.10 使用JPA保存数据 1.11 使用JdbcTemplate 1.12 Spring Boot修改端口号 1.13 Spring Boot配置ContextPath 1.14 Spring Boot改变JDK编译版本 1.15 处理...

    spring.doc

    DataSource注入的三种方式: 108 5.1.8声明式事务管理 116 5.1.8.1Spring的事务管理器 117 5.1.8.2Spring事务的传播属性 117 5.1.8.3Spring事务的隔离级别 117 拓展: 118 5.1.8.4以XML配置的 形式 119 拓展: 120 ...

    Spring的学习笔记

    第九课:DataSource 28 一、 Sping配置数据源: 28 二、 注入使用 29 三、 dbcp.BasicDataSource 29 第十课 Spring整合Hiberante3 30 一、 Spring配置hibernate3的SessionFactory 30 (一) xml形式的SessionFactory ...

    spring applicationContext 配置文件

    &lt;bean id="transactionManagerPdm" class="org.springframework.jdbc.datasource.DataSourceTransactionManager " p:dataSource-ref="dataSourcePdm"/&gt; &lt;!-- 配置事务的传播特性 --&gt; *" propagation=...

    Spring-Reference_zh_CN(Spring中文参考手册)

    6.8.1. 在Spring中使用AspectJ来为domain object进行依赖注入 6.8.1.1. @Configurable object的单元测试 6.8.1.2. 多application context情况下的处理 6.8.2. Spring中其他的AspectJ切面 6.8.3. 使用Spring IoC来...

    开源框架 Spring Gossip

    Spring 的 DAO 支持 DataSource 注入 DataSource 置换 JDBC 支援 Spring 在 JDBC 的使用上提供了几个类别,让您可以简化 JDBC 在使用时的流程。 使用 JdbcTemplate JdbcTemplate 执行与...

    springjdbc

    &lt;property name="dataSource" ref="dataSource" /&gt; &lt;/bean&gt; &lt;/beans&gt; &lt;!-- controller配置 --&gt; &lt;beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi=...

    Spring 2.0 开发参考手册

    6.8.1. 在Spring中使用AspectJ来为domain object进行依赖注入 6.8.2. Spring中其他的AspectJ切面 6.8.3. 使用Spring IoC来配置AspectJ的切面 6.8.4. 在Spring应用中使用AspectJ Load-time weaving(LTW) 6.9. ...

    spring-boot-mybatis-multidatasource多数据源配置(oracle)

    1, 用idea或者eclipse或者其他开发工具导入springboot项目。 2, 等待导入完毕,更改...4, 启动项目,测试接口在DataSourceController中,只用注入相关的bean就能使用。 5, 使用过程中有其他问题请留言咨询,谢谢。

    Spring中文帮助文档

    6.8.1. 在Spring中使用AspectJ进行domain object的依赖注入 6.8.2. Spring中其他的AspectJ切面 6.8.3. 使用Spring IoC来配置AspectJ的切面 6.8.4. 在Spring应用中使用AspectJ加载时织入(LTW) 6.9. 更多资源 7...

    spring chm文档

    6.8.1. 在Spring中使用AspectJ来为domain object进行依赖注入 6.8.2. Spring中其他的AspectJ切面 6.8.3. 使用Spring IoC来配置AspectJ的切面 6.8.4. 在Spring应用中使用AspectJ Load-time weaving(LTW) 6.9. ...

    Spring API

    6.8.1. 在Spring中使用AspectJ进行domain object的依赖注入 6.8.2. Spring中其他的AspectJ切面 6.8.3. 使用Spring IoC来配置AspectJ的切面 6.8.4. 在Spring应用中使用AspectJ加载时织入(LTW) 6.9. 更多资源 7...

    第24次课-1 Spring与Hibernate的整合

    如果Spring与Hibernate进行了整合,则Hibernate便处于被Spring管理的状态下,Hibernate所需的基础资源,都由Spring以注入的方式提供。 由Spring接管的内容包括: Hibernate创建SessionFactory时需要的DataSource ...

    spring in action英文版

     4.1.2 与DataSource一起工作  4.1.3 一致的DAO支持  4.2 在Spring中使用JDBC  4.2.1 JDBC代码的问题  4.2.2 使用JdbcTemplate  4.2.3 把操作创建成对象  4.2.4 自增键  4.3 介绍Spring的ORM...

    spring2.5 学习笔记

    第九课:DataSource 28 一、 Sping配置数据源: 28 二、 注入使用 29 三、 dbcp.BasicDataSource 29 第十课 Spring整合Hiberante3 30 一、 Spring配置hibernate3的SessionFactory 30 (一) xml形式的SessionFactory ...

    spring_MVC源码

    41. class="org.springframework.jdbc.datasource.DriverManagerDataSource"&gt; 42. ${dataSource.driverClassName}" /&gt; 43. ${dataSource.url}" /&gt; 44. ${dataSource.username}" /&gt; 45. ${dataSource.password}" ...

    spring-common:[已弃用]与Spring Boot应用程序一起使用的通用Spring组件库和相关类

    注释类,用于注入适当的Logger LoggerProvider 提供Logger界面 @LogField 注释类,指示要记录的字段 JdbcProperties 用于保存JDBC DataSource属性的抽象类 JdbcDataSource 使用JdbcProperties实例的DataSource...

    Spring + Hibernate + Struts 事务配置小例子(带提示框等小技巧)

    -- 事务拦截器bean需要依赖注入一个事务管理器 --&gt; &lt;!-- 下面定义事务传播属性 [ bus* 事务的方法名]--&gt; *"&gt;PROPAGATION_REQUIRED &lt;!-- 定义BeanNameAutoProxyCreator,该bean是个bean后...

    springmybatis

    mybatis实战教程mybatis in action之五与spring3集成附源码 mybatis实战教程mybatis in action之六与Spring MVC 的集成 mybatis实战教程mybatis in action之七实现mybatis分页源码下载 mybatis实战教程mybatis in ...

Global site tag (gtag.js) - Google Analytics