框架中mysql的数据源配置文件_Spring中applicationContext.xml配置文件中数据库数据源配置...

(1) Oracle    (2)DB2   (3)SQL Server org.apache.commons.dbcp.BasicDataSource 需要commons-pool.jar,commons-dbcp-1.2.2.jar,commons-collections-3.2.jar三个JAR包,但是这种模式不能动态修改数据库,下面将介绍动态修改数据库链接。

【3】动态修改数据库链接(此没经过具体测试,转自点击打开链接)

需要访问多个数据库,而且需要在服务器运行不重新启动的情况下,动态的修改spring中配置的数据源datasource,spring的配置文件是在容器启动的时候就加载到内存中的,如果手动改了application.xml,我们必须要重新启动服务器配置文件才会生效。而在spring中提供了一个类WebApplicationContext,这个类可以让你获得一些bean,可以修改内存中的信息,通过这个类来实现的。下面是具体的代码。

package com.southdigital.hospital;

import java.io.IOException;

import javax.servlet.ServletContext;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import org.springframework.web.context.WebApplicationContext;

import org.springframework.web.context.support.WebApplicationContextUtils;

import com.mchange.v2.c3p0.ComboPooledDataSource;

public class ChangeSpringConfig extends HttpServlet

{

private String ipAddress = "127.0.0.1";

/**

* The doGet method of the servlet.

*

* This method is called when a form has its tag value method equals to get.

*

* @param request the request send by the client to the server

* @param response the response send by the server to the client

* @throws ServletException if an error occurred

* @throws IOException if an error occurred

*/

public void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException

{

doPost(request, response);

}

/**

* The doPost method of the servlet.

*

* This method is called when a form has its tag value method equals to post.

*

* @param request the request send by the client to the server

* @param response the response send by the server to the client

* @throws ServletException if an error occurred

* @throws IOException if an error occurred

*/

public void doPost(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException

{

//先取得servleContext对象,提供给spring的WebApplicationUtils来动态修改applicationContext.xml

ipAddress = request.getParameter("ipAddress");

System.out.println(ipAddress);

ServletContext servletContext = this.getServletContext();

WebApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(servletContext);

ComboPooledDataSource cpds = (ComboPooledDataSource) applicationContext.getBean("dataSource");

cpds.setJdbcUrl("jdbc:mysql://"+ipAddress+":3306/ssh");

}

}

注意:通过这种方法修改applicationContext.xml文件的时候用c3p0,而不可以用dbcp,dbcp不支持动态修改读取到内存里面的数据。

【4】Spring与Hibernate通过applicationContext.xml关联

配置Spring,使它能管理Hibernate。其实,只要在Spring的配置文件(我们这里是applicationContext.xml)中配置一个叫做sessionFactory的bean,Spring就可以和Hibernate联系起来了。

com/ascent/bean/hibernate.cfg.xml

这样,Spring和Hibernate的第一步整合就完成了,现在到了关键的地方——如何让Spring和Hibernate双剑合璧来实现业务逻辑?

还是在applicationContext.xml文件中先做一个配置。

在上面你大概可以感觉到Spring给我们带来的好处了,Spring的IoC模式可以统一管理各层,而又使各层松散耦合在一起,使各层之间实现最大的解耦性,这也是Web架构一向的追求。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值