PropertyPlaceholderConfigurer的用法:
用法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 -->
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<!—
- 这里的classpath可以认为是项目中的src-
- 属性名是 locations,使用子标签<list></list>可以指定多个数据库的配置文件,这里指定了一个
->
<value>classpath:resource/config/jdbc.properties</value>
</list>
</property>
</bean>
此时的数据库配置文件项目路径是这样的
用法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>/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
这样数据库的配置信息被读取之后,在创建datasource的时候就可以使用了
下面连接oracle 使用apache的dbcp 数据源
<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数据源令人郁闷的事,使用dbcp和spring提供的JdbcTemplate操作数据库是 查询是可以的
但是执行update、delete、insert into 操作时,数据库中的数据没有变化
从网上查询了很多的资料,都无果。最后偶然看到网上有人说,dbcp数据源的事务不会自动提交,
当改成c3p0数据源后好了
随后认为这下终于可以松口气了,谁知道天不遂人愿。当更换一张表进行测试,数据库中的数据还是没有变化,难道c3p0数据源也不好使,
当再次经过代码的折磨之后,
最终发现改动测试java文件,不在一个项目中,把其他的项目关闭就好了
当文档写到这里时,突然发现oracle使用的dbcp数据源有这一项配置
<property name="defaultAutoCommit">
<value>false</value>
</property>
原来dbcp数据源事务的自动提交功能被关闭了
马上把事务自动提交改成true 进行测试,一切ok,(^ _ ^)
PropertyPlaceholderConfigurer的用法:的更多相关文章
- spring读取数据库的配置信息(url、username、password)时的<bean>PropertyPlaceholderConfigurer的用法
用法1: <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://w ...
- PropertyPlaceholderConfigurer 基本用法
目录 一.PropertyPlaceholderConfigurer 的继承体系 二.PropertyPlaceholderConfigurer 的基本概念 三.PropertyPlaceholder ...
- PropertyPlaceholderConfigurer的用法(使用spring提供的类读取数据库配置信息.properties)
http://www.cnblogs.com/wanggd/archive/2013/07/04/3172042.html(写的很好)
- Spring注解?啥玩意?
目录 基础概念:@Bean 和 @Configuration 使用AnnotationConfigApplicationContext 实例化Spring容器 简单的构造 使用register注册IO ...
- Spring PropertyPlaceholderConfigurer占位符用法
1.PropertyPlaceholderConfigurer是一个bean工厂后置处理器的实现,也就是BeanFactoryPostProcessor接口的一个实现.PropertyPlacehol ...
- PropertiesFactoryBean PropertyPlaceholderConfigurer 区别
正如 stackoverflow上说的,PropertiesFactoryBean 是PropertiesLoaderSupport 直接的实现类, 专门用来管理properties文件的工厂bean ...
- async4j 普通用法、整合spring用法
1.普通用法 asyn4j 是一个java异步方法调用框架,基于消费者与生产者模式. async4j就是基于Executors线程池和反射机制实现的. 包括了异步方法执行,异步回调执行,异步工作缓存模 ...
- MyBatis(七):mybatis Java API编程实现增、删、改、查的用法
最近工作中用到了mybatis的Java API方式进行开发,顺便也整理下该功能的用法,接下来会针对基本部分进行学习: 1)Java API处理一对多.多对一的用法: 2)增.删.改.查的用法: 3) ...
- EditText 基本用法
title: EditText 基本用法 tags: EditText,编辑框,输入框 --- EditText介绍: EditText 在开发中也是经常用到的控件,也是一个比较必要的组件,可以说它是 ...
随机推荐
- NumberFormat usage
NumberFormat 是所有数值格式的抽象基类. 该类提供了格式化和分析数值的接口. NumberFormat 也提供了确定 哪个语言环境具有数值格式以及它们名字的方法. import java. ...
- dubbo demo实现
粗略的写了一个dubbo的demo,使用了alibaba的dubbo,还有zookeeper来做配置中心 参考资料地址: http://dubbo.io/User+Guide-zh.htm#UserG ...
- 如何卸除SDL TRADOS中的自开发插件
去年学着用SDL的例子编译了一个名为SimpleText的插件,每次打开TRADOS 2014时都要提示三次加载插件,很是烦人.但我想卸掉时,却无从下手,不知道怎么办.这个问题纠缠了我很久,今晨心性比 ...
- html 问题
1.footer处理 目标: 页面较短,footer位于页面底部,出现滚动条时,依然在原来的位置,不随滚动条移动 页面较长,位于元素底部 .container{ position: relative ...
- 【原创】-- uboot,kennel,fs,rootfs 编译制作
环境:ubuntu14.04 内核版本 linux 3.13.0 OK6410 内核编译环境 linux 3.0.1 uboot版本 1.1.6 交叉编译工具链 arm-lin ...
- .NET Socket服务编程之-高效连接接入编
在.NET上编写网络服务深入都有2,3年了,而这些时间时如何在.NET里实现网络服务积累了一些经验.在接下来的时间里会把这方面的经验通过博客的方式分享出来.而这一章主要是讲解在如果提高服务连接接入的效 ...
- C语言 线性表 顺序表结构 实现
一个能够自动扩容的顺序表 ArrList (GCC编译). #include <stdio.h> #include <stdlib.h> #include <string ...
- 深入探索AngularJS(持续更新)
数据双向绑定并不是Angular最出彩的地方.大部分对AngularJs的介绍都偏重于使用,使用的学习只是学了AngularJs的API,而那只能AngularJs的很小一部分.随着使用越来越深,系统 ...
- [WinAPI] 串口1-创建[包括: 打不开串口]
本来是用一个USB扩展把一个USB括成4个,然后把USB转串口连接上,虽然设备管理器可以找到用SSCOM也能找到,但是用API就是打不开,最后把USB转串插在电脑的一个USB上就可以啦! #inclu ...
- 用dom操作替代正则表达式
在B/S结构客户端越来越“胖”的今天,作为一名全端程序员,您很可能会在前端操作html字符串,注意,是操作html字符串,不是操作当前页面的html. 举个例子,百度推出的在线HTML富文本编辑器Ue ...