用法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>
<propertyname="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数据源有这一项配置
<propertyname="defaultAutoCommit">
<value>false</value>
</property>
原来dbcp数据源事务的自动提交功能被关闭了
马上把事务自动提交改成true进行测试,一切ok,(^ _ ^)
相关推荐
Spring属性占位符PropertyPlaceholderConfigurer的使用,自己写的一个demo
Spring中PropertyPlaceholderConfigurer的使用
NULL 博文链接:https://wokeke.iteye.com/blog/2214113
主要介绍了Spring如何使用PropertyPlaceholderConfigurer读取文件,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
NULL 博文链接:https://chyy001.iteye.com/blog/1338488
JDBCTemp+MySQL示例。Spring,PropertyPlaceholderConfigurer,BeanPropertyRowMapper
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <value>classpath:/deploy.properties <!-- 配置sessionFactory <value>classpath:hibernate...
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <value>classpath:proxool.properties ...
#Spring 属性注释扩展的 PropertyPlaceHolderConfigurer 使用注解将配置属性注入到 Spring 组件中。 注意:Spring 3 现在支持使用 @Value 注释的容器的。 该项目仅用于 Spring 2.5.x 支持。 ##入门Spring房产注解...
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="location" value="classpath:jdbc.properties" /> <!-- ${jdbc.driver}" /> ${jdbc.url}" /> $...
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <value>classpath:jdbc.properties ${driver}"/> ${url}"/> ${username}"/> ${password}"/> <!-- 配置...
4.3.2.2 使用静态工厂方法来实例化.. 36 4.3.2.3 使用实例工厂方法来实例化.. 37 4.4 依赖 38 4.4.1 依赖注入. 38 4.4.1.1 基于构造方法的依赖注入 39 4.4.1.2 基于setter 方法的依赖注入...
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <value>classpath*:jdbc.properties <!-- 数据源配置,主要用于开发测试...
<bean id="placeholderConfig" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <!-- ROOT web context --> ${webapp.contextPath}" ...
6 <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 7 8 9 <value>/WEB-INF/jdbc.properties 10 11 12 13 14 它配置了以下功能: 读取...
什么 与通过从属性文件读取其配置来进行自我配置相比,对于应用程序而言,有什么可能更简单? 好吧,对于业务应用程序而言... 首先,我们将使用PropertyPlaceholderConfigurer通过读取存储在.properties文件中的属性来
使用方法的返回值进行注入 4.3.6.IFactoryObject接口的其它实现 4.3.6.1.Log4Net 4.3.7.使用depends-on 4.3.8.自动装配协作对象 4.3.9.检查依赖项 4.4.类型转换 4.4.1.枚举类型的转换 4.4.2.内置的类型转换器 4.4.3....
PropertyPlaceholderConfigurer示例 3.7.2.2. PropertyOverrideConfigurer示例 3.7.3. 使用FactoryBean定制实例化逻辑 3.8. ApplicationContext 3.8.1. 利用MessageSource实现国际化 3.8.2. 事件 3.8.3. 底层资源的...
不使用XML定义档进行 Bean设置 Aware 相关介面 BeanPostProcessor BeanFactoryPostProcessor PropertyPlaceholderConfigurer PropertyOverrideConfigurer CustomEditorConfigurer ...