Java学习者论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

手机号码,快捷登录

恭喜Java学习者论坛(https://www.javaxxz.com)已经为数万Java学习者服务超过8年了!积累会员资料超过10000G+
成为本站VIP会员,下载本站10000G+会员资源,购买链接:点击进入购买VIP会员
JAVA高级面试进阶视频教程Java架构师系统进阶VIP课程

分布式高可用全栈开发微服务教程

Go语言视频零基础入门到精通

Java架构师3期(课件+源码)

Java开发全终端实战租房项目视频教程

SpringBoot2.X入门到高级使用教程

大数据培训第六期全套视频教程

深度学习(CNN RNN GAN)算法原理

Java亿级流量电商系统视频教程

互联网架构师视频教程

年薪50万Spark2.0从入门到精通

年薪50万!人工智能学习路线教程

年薪50万!大数据从入门到精通学习路线年薪50万!机器学习入门到精通视频教程
仿小米商城类app和小程序视频教程深度学习数据分析基础到实战最新黑马javaEE2.1就业课程从 0到JVM实战高手教程 MySQL入门到精通教程
查看: 327|回复: 0

[Java框架学习]Spring中的数据源DataSource注入

[复制链接]
  • TA的每日心情
    开心
    2021-3-12 23:18
  • 签到天数: 2 天

    [LV.1]初来乍到

    发表于 2014-10-30 23:58:50 | 显示全部楼层 |阅读模式
    对于不同的数据库存取需求,我们使用JDBC来解决这个问题,对于不同的数据库连接来源需求,Spring则提供了DataSource注入,更换数据库连接来源只要在Bean.xml中修改配置,而不用修改任何一行程序。  

             因应用不同的系统,应用程序可能使用不同的数库据连接来源,但如纯粹的使用JDBC、透过连接池、或是透过JNDI等等,数据库连接来源的更改是底层的行为,不应影响到上层的业务逻辑,为此,您可以在需要取得连接来源的Bean上保留一个资料来源注入的接口,让依赖的资料来源由该接口注入。例如我们来写一个简单的
      
      
      
    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目录下找到。  
         注意到我&#65533;在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



      
      
       
       

         
       

         
       
      


      



                            function TempSave(ElementID)
                            {
                                    CommentsPersistDiv.setAttribute("CommentContent",document.getElementById(ElementID).value);
                                    CommentsPersistDiv.save("CommentXMLStore");
                            }
                            function Restore(ElementID)
                            {
                                    CommentsPersistDiv.load("CommentXMLStore");
                                    document.getElementById(ElementID).value=CommentsPersistDiv.getAttribute("CommentContent");
                            }
                   
                      











    源码下载:http://file.javaxxz.com/2014/10/30/235849890.zip
    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    QQ|手机版|Java学习者论坛 ( 声明:本站资料整理自互联网,用于Java学习者交流学习使用,对资料版权不负任何法律责任,若有侵权请及时联系客服屏蔽删除 )

    GMT+8, 2024-5-5 19:41 , Processed in 0.440818 second(s), 50 queries .

    Powered by Discuz! X3.4

    © 2001-2017 Comsenz Inc.

    快速回复 返回顶部 返回列表