服务器运行的状态,修改Spring中的applicationContext.xml配置文件
来源:互联网 发布:淘宝哪里取消申请退款 编辑:程序博客网 时间:2024/06/05 12:05
最近在做一个项目,需要访问多个数据库,而且需要在服务器运行不重新启动的情况下,动态的修改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. <br>
*
* 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. <br>
*
* 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");
}
}
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. <br>
*
* 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. <br>
*
* 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不支持动态修改读取到内存里面的数据。
对于proxool 的datasource 类是 org.logicalcobwebs.proxool.ProxoolDataSource
0 0
- 服务器运行的状态,修改Spring中的applicationContext.xml配置文件
- 服务器运行的状态,修改Spring中的applicationContext.xml配置文件
- Spring的配置文件applicationContext.xml
- Spring的配置文件applicationContext.xml
- spring的配置文件applicationContext.xml配置文件详解
- spring配置文件applicationContext.xml中的事务总结
- spring中的配置文件applicationContext.xml示例
- Spring的applicationContext.xml--spring 配置文件详解
- spring配置文件applicationContext.xml
- spring配置文件 applicationContext.xml
- Spring配置文件-applicationContext.xml
- Spring 配置文件applicationContext.xml的切割原则
- 详解Spring的applicationContext.xml配置文件
- spring 的配置文件applicationContext.xml详解
- java spring配置文件applicationcontext.xml的使用
- 关于spring的配置文件applicationcontext.xml
- Spring 加载xml配置文件的方式 ApplicationContext
- spring applicationContext.xml的配置文件详解
- 循环遍历HashMap
- C语言的变量的内存分配
- 一篇很全面的freemarker教程
- Linux内核中的init_task进程和idle进程
- ubuntu下搜索文件
- 服务器运行的状态,修改Spring中的applicationContext.xml配置文件
- 多线程与多进程的对比
- 网站分析与优化
- 3.17
- Codeforces #257B Jzzhu and Sequences(矩阵快速幂)
- 程序猿的日常-5
- HDU 1016 - Prime Ring Problem [简单DFS]
- Letter Combinations of a Phone Number
- 《AngularJS权威教程》