Spring中applicationContext.xml配置文件中数据库数据源配置
来源:互联网 发布:网络禁书40百度云 编辑:程序博客网 时间:2024/05/21 18:44
a.MessageSource,提供了语言信息的国际化支持
b.提供资源(如URL和文件系统)的访问支持
c.为实现了ApplicationListener接口的bean提供了事件传播支持
d.为不同的应用环境提供不同的context,例如支持web应用的XmlWebApplicationContext类
<!-- 头文件,主要注意一下编码 -->
- <?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">
<!-- 数据库驱动,我这里使用的是Mysql数据库 -->
- <propertyname propertyname="driverClassName">
- <value>com.mysql.jdbc.Driver</value>
- </property>
<!-- 数据库地址,这里也要注意一下编码,不然乱码可是很郁闷的哦! -->
- <property name="url">
- <value>
- jdbc:mysql://localhost:3306/tie?useUnicode=true&characterEncoding=utf-8
- </value>
- </property>
<!-- 数据库的用户名 -->
- <property name="username">
- <value>root</value>
- </property>
<!-- 数据库的密码 -->
- <property name="password">
- <value>123</value>
- </property>
- </bean>
<!-- 把数据源注入给Session工厂 -->
- <bean id="sessionFactory"
- class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
- <property name="dataSource">
- <ref bean="dataSource" />
- </property>
<!-- 配置映射文件 -->
- <property name="mappingResources">
- <list>
- <value>com/alonely/vo/User.hbm.xml</value>
- </list>
- </property>
- </bean>
(1) Oracle
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
<property name="url" value="jdbc:oracle:thin:@(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=192.168.1.101)(PORT=1521)))(CONNECT_DATA=(SERVICE_NAME=orcl)(SERVER=DEDICATED)))" />
<property name="username" value="rootl" />
<property name="password" value="1234" />
</bean>
(2)DB2
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="com.ibm.db2.jdbc.app.DB2Driver" />
<property name="url" value="jdbc:db2:thin:@localhost:5000/testDB" />
<property name="username" value="rootl" />
<property name="password" value="1234" />
</bean>
(3)SQL Server
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="com.microsoft.jdbc.sqlserver.SQLServerDriver" />
<property name="url" value="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName = testDB" />
<property name="username" value="rootl" />
<property name="password" value="1234" />
</bean>
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. <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不支持动态修改读取到内存里面的数据。
【4】Spring与Hibernate通过applicationContext.xml关联
- Spring中applicationContext.xml配置文件中数据库数据源配置
- 在Spring的 applicationContext.xml文件中配置数据源
- applicationContext.xml中数据源的配置
- Spring中,applicationContext.xml 配置文件在web.xml中的配置详解
- Spring中,applicationContext.xml 配置文件在web.xml中的配置详解
- Spring中,applicationContext.xml 配置文件在web.xml中的配置详解
- Spring中,applicationContext.xml 配置文件在web.xml中的配置详解
- spring定时任务applicationContext.xml中配置
- Spring配置文件中配置数据源(dataSource)
- applicationContext.xml文件中怎么配置struts2和spring配置文件的加载路径
- SSH2整合项目开发中spring配置文件applicationContext.xml分析
- 我将Spring配置文件放置在applicationContext-common.xml中
- SSH2整合项目开发中spring配置文件applicationContext.xml分析
- Spring配置文件(applicationContext.xml如何配置)
- Spring配置文件中数据库配置
- SpringMVC配置文件applicationContext.xml操作数据源的配置
- spring配置文件applicationContext.xml
- spring配置文件 applicationContext.xml
- POJ 1185 炮兵阵地
- Android SQLite小结
- 双向冒泡
- mysql书籍
- 已阅读C/C++书籍清单
- Spring中applicationContext.xml配置文件中数据库数据源配置
- Miller-Rabin素数测试
- Extremal Region(极值区域)文本定位与识别法-学习笔记(三)
- ms sql server 2005 select 用法大全
- Oracle分段查询
- ms sql复杂点的查询
- Android Handler Message总结
- Java SSL Socket通讯示例
- conversion to dalvik format failed with ...