用法1:

<?xml version="1.0" encoding="UTF-8"?>

<beans

xmlns="http://www.springframework.org/schema/beans"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns:p="http://www.springframework.org/schema/p"

xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

<!-- 使用spring提供的PropertyPlaceholderConfigurer读取数据库配置信息.properties -->

  1. 这里的classpath可以认为是项目中的src-
  2. 属性名是 locations,使用子标签<list></list>可以指定多个数据库的配置文件,这里指定了一个

<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">

<property name="locations">

<list>

<value>classpath:resource/config/jdbc.properties</value>

</list>

</property>

</bean>

此时的数据库配置文件项目路径是这样的

 

读取数据库的配置文件还可以使用下面的方式

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">

<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">

<property name="locations">

<list>

<value>/WEB-INF/config_test/jdbc.properties</value>

</list>

</property>

</bean>

此时jdbc.properties文件的位置如下图所示

.properties配置文件还可以有多个,这里在<list></list>标签中指定了2个数据的配置文件

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">

<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">

<property name="locations">

<list>

<value>classpath:jdbc.properties</value>

        <value>/WEB-INF/config_test/jdbc.properties</value>

</list>

</property>

</bean>

classpath:jdbc.properties对应的文件位置是:

文件内容是:配置的是sqlserver的连接信息

sqlserver.username=sa

sqlserver.password=sqlserver

sqlserver.url=jdbc\:jtds\:sqlserver\://localhost\:1433/J2EE

sqlserver.driver=net.sourceforge.jtds.jdbc.Driver

 /WEB-INF/config_test/jdbc.properties对应的文件位置是

文件内容是:配置的是oracle的连接信息

jdbc.driverClassName=oracle.jdbc.driver.OracleDriver

jdbc.url=jdbc:oracle:thin:@127.0.0.1:1521:orcl

jdbc.username=jxbms

jdbc.password=jxbms

用法2:这种方法比较简洁:

<context:property-placeholder location="classpath:db.properties"/>//这句配置通常放在applicationContext.xml的配置文件中

用法3:利用applicationContext.xml配置文件中配置的bean,然后自己去写PropertyPlaceholderConfigurer这个类,当然你得先继承spring自身的这个PropertyPlaceholderConfigurer,这种方法适合数据库配置有好多种方式,在PropertyPlaceholderConfigurer这个类中去选择读取哪一种配置信息的数据配置信息,这个方式实现的原理就是,在spring容器中配置bean之后,spring会初始化这些bean,所以你的java选择数据库配置信息的实现代码,要在构造器中实现,现举例:

spring的配置信息:

<bean id="propertyConfigurer"
class="cn.com.ksplatform.core.expand.spring.PropertyPlaceholderConfigurer"></bean><!-- 这个PropertyPlaceholderConfigurer类重写了-->

PropertyPlaceholderConfigurer.java如下:

public class PropertyPlaceholderConfigurer extends
org.springframework.beans.factory.config.PropertyPlaceholderConfigurer {
/**
* 初始化任务 1.完成系统配置的动态载入,读取数据库的配置信息是在构造函数中完成的
*/
public PropertyPlaceholderConfigurer() {
InstallInfo info = new InstallInfo();
if (PlatformContext.SYSTEMSTATU.equals(SystemStatu.INSTALLED)) {
Log.info("加载系统安装信息。");
try {
Log.info("读取内部配置文件。");
info = SystemUtil.getSysConfigModel();
if(info == null){
Log.info("无法读取内部文件。。 读取容器配置");
info = SystemUtil
.getSysConfigModel(PlatformContext.CONTEXT_PATH);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
} else {
Log.info("未检测到系统按照信息。 加载系统文件数据库");
//启动本地数据库
DbUtil.runLocalDb(9002);
info.setDataBaseType(DBType.HSQLDB.getValue());
info.setDataBaseHost("127.0.0.1");
info.setDataBaseName("ksPlatform");
info.setDataBaseUserName("sa");
info.setDataBasePassWord("");
info.setDataBasePort(9002);
DbHelp.setUrl(info);
PlatformContext.SYSTEMSTATU = SystemStatu.INSTALLED;
} Properties p = new Properties();
p.setProperty("dataBaseUrl", info.getDataBaseUrl());
p.setProperty("dataBaseUserName", info.getDataBaseUserName());
p.setProperty("dataBasePassWord", info.getDataBasePassWord());
p.setProperty("hibernateProperties",info.hibernateProperties);
super.setProperties(p);
}
}

  

 这样数据库的配置信息被读取之后,在创建datasource的时候就可以使用了

 下面连接oracle 使用apachedbcp 数据源

<bean id="dataSource"

class="org.apache.commons.dbcp.BasicDataSource"

destroy-method="close">

<property name="driverClassName">

<value>${jdbc.driverClassName}</value>

</property>

<property name="url">

<value>${jdbc.url}</value>

</property>

<property name="username">

<value>${jdbc.username}</value>

</property>

<property name="password">

<value>${jdbc.password}</value>

</property>

<property name="maxActive">

<value>100</value>

</property>

<property name="maxIdle">

<value>3</value>

</property>

<property name="maxWait">

<value>-1</value>

</property>

<property name="defaultAutoCommit">

        <value>false</value>

       </property>

</bean>

 下面连接sqlserver数据库使用的是c3p0数据源

<bean id="dataSource_oracle" class="com.mchange.v2.c3p0.ComboPooledDataSource"

destroy-method="close" >

<property name="driverClass" value="${jdbc.driverClassName}" />

<property name="jdbcUrl" value="${jdbc.url}" />

<property name="user" value="${jdbc.username}" />

<property name="password"  value="${jdbc.password}" />

</bean>

使用dbcp数据源令人郁闷的事,使用dbcpspring提供的JdbcTemplate操作数据库是  查询是可以的

但是执行updatedeleteinsert into 操作时,数据库中的数据没有变化

 从网上查询了很多的资料,都无果。最后偶然看到网上有人说,dbcp数据源的事务不会自动提交,

当改成c3p0数据源后好了

 随后认为这下终于可以松口气了,谁知道天不遂人愿。当更换一张表进行测试,数据库中的数据还是没有变化,难道c3p0数据源也不好使,

当再次经过代码的折磨之后,

最终发现改动测试java文件,不在一个项目中,把其他的项目关闭就好了

 当文档写到这里时,突然发现oracle使用的dbcp数据源有这一项配置

        <property name="defaultAutoCommit">

        <value>false</value>

       </property>

原来dbcp数据源事务的自动提交功能被关闭了

 马上把事务自动提交改成true  进行测试,一切ok

spring读取数据库的配置信息(url、username、password)时的<bean>PropertyPlaceholderConfigurer的用法的更多相关文章

  1. Eclipse中利用JSP把mysql-connector-java-8.0.13.jar放到WebContent\WEB-INF\lib中连接MySQL数据库时Connection conn = DriverManager.getConnection(url,username,password)报错的解决办法

    开发环境: 1.系统:windows 7/8/10均可 2.jdk:1.8.0_144 3.服务器:apache-tomcat-9.0.8 4.IDE:eclipse+jsp 0.网页代码如下: &l ...

  2. [Xcode 实际操作]九、实用进阶-(8)实现App的Setting设置:添加和读取程序的配置信息

    目录:[Swift]Xcode实际操作 本文将演示如何实现添加和读取程序的配置信息. 在项目文件夹[DemoApp]上点击鼠标右键->[New File]创建一个设置束文件 ->[Sett ...

  3. Spring主从数据库的配置和动态数据源切换原理

    原文:https://www.liaoxuefeng.com/article/00151054582348974482c20f7d8431ead5bc32b30354705000 在大型应用程序中,配 ...

  4. Spring引用数据库文件配置数据源

    例子:引用 druid.properties 在Spring配置文件(applicationContext.xml)引入外部配置文件,需要指定特定的 key才能被正确识别并使用 在原本的 url.us ...

  5. php随笔2-php+ajax 实现输入读取数据库显示匹配信息

    dropbox_index.php <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" " ...

  6. 从Config文件中读取节点的配置信息

    下面是web.config中与本内容有关的细节 <appSettings> <add key="servername" value="www" ...

  7. 教你如何利用分布式的思想处理集群的参数配置信息——spring的configurer妙用

    引言 最近LZ的技术博文数量直线下降,实在是非常抱歉,之前LZ曾信誓旦旦的说一定要把<深入理解计算机系统>写完,现在看来,LZ似乎是在打自己脸了.尽管LZ内心一直没放弃,但从现状来看,需要 ...

  8. 第九篇:Spring的applicationContext.xml配置总结

    在前面的一篇日志中,记录了web.xml配置启动的顺序,web启动到监听器ContextLoaderListener时,开始加载spring的配置文件applicationContext.xml(通常 ...

  9. JDBC通过配置文件(properites)读取数据库配置信息

    扫盲: Classloader 类加载器,用来加载 Java 类到 Java 虚拟机中.与普通程序不同的是.Java程序(class文件)并不是本地的可执行程序.当运行Java程序时,首先运行JVM( ...

随机推荐

  1. 疯狂JAVA——第六章 面向对象(下)

    6.1包装类 java为了照顾程序员的传统习惯,所以提供了八种基本数据类型.但也带来不方便,例如所有引用类型都继承自Object类,都可当做Object类型变量使用.但基本数据类型的变量就不可以.如果 ...

  2. Python 遍历文件夹 listdir walk 的区别

    一.一级目录import os path = 'd:\file'; for filename in os.listdir(path): print(os.path.join(path,filename ...

  3. Excel日期处理

    short format = cell.getCellStyle().getDataFormat(); //其值为22 输入值类型为2018/6/28 17:25:48 if (format!=22) ...

  4. Android中WebView使用全解

    开始 在Android系统中内嵌的WebKit,这是一个浏览器内核,它帮助着我们可以浏览网页.在实际开发中,如果你想让你的App能够访问网页,那就需要用到WebView这个控件. 如何使用? 其实使用 ...

  5. #define宏常量和const常量的区别

    C++ 语言可以用const 来定义常量,也可以用#define 来定义常量.但是前者比后者有更多的优点:(1) const 常量有数据类型,而宏常量没有数据类型.编译器可以对前者进行类型安全检查.而 ...

  6. 使用Sql分页方法给Repeater控件分页的方法

    页面代码 <div class="bookList"> <asp:Repeater ID="rpBooks" runat="serv ...

  7. tcp连接需要注意的问题

    当有子进程时,子进程终止时会返回SIGCHLD信号,默认忽略,此时会有僵尸进程. 处理方法: 捕获信号,并waitpid. 当慢系统调用被中断时(如信号中断),有些系统不会自动重启调用,此时系统调用可 ...

  8. JAVA 框架 Spring Cache For Redis.

    一.概述 缓存(Caching)可以存储经常会用到的信息,这样每次需要的时候,这些信息都是立即可用的. 常用的缓存数据库: Redis   使用内存存储(in-memory)的非关系数据库,字符串.列 ...

  9. [leetcode]211. Add and Search Word - Data structure design添加查找单词 - 数据结构设计

    Design a data structure that supports the following two operations: void addWord(word) bool search(w ...

  10. linux 下 php 安装 ZeroMQ 扩展

    一.下载安装源码包 ZeroMQ源码包下载地址: http://zeromq.org/area:download 如:zeromq-4.1.4.tar.gz   php的zmq扩展源码包 https: ...