`
furuitao
  • 浏览: 33438 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Spring中配置数据库连接池

    博客分类:
  • java
阅读更多

在Spring中配置数据库连接,可以通过

<bean id="propertyConfigurer"
	class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
	<property name="locations">
		<list>
			<value>WEB-INF/jdbc.properties</value>
		</list>
	</property>
</bean>

 

将连接参数从数据库配置文件引入到Spring配置文件中,但是在进行测试的时候,即不启动服务器,而只调用main方法进行测试,将会报

Caused by: java.io.FileNotFoundException: WEB-INF\jdbc.properties (系统找不到指定的路径。)

 

有两种处理方法: 

一、需要使用绝对路径的方式,可以在程序中将绝对路径放到系统环境变量中

System.setProperty("SYSTEM_HOME", "D:/workspace/aml2010090D/WebRoot");

 在Spring配置文件中这么写

<bean id="propertyConfigurer"
	class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
	<property name="locations">
		<list>
			<value>${SYSTEM_HOME}/WEB-INF/spring/jdbc.properties</value>
		</list>
	</property>
</bean>

 

二、将数据库连接的参数直接放入到系统环境变量中

System.setProperty("driverClassName", DBDriver);
System.setProperty("username", DBUser);
System.setProperty("password", DBPassword);
System.setProperty("url", DBUrl);

 然后将Spring配置文件改为

<!-- 这个是必须写的,要不然,不会解析${} -->
<bean id="propertyConfigurer"
	class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"></bean>

   

附上dataSource的配置

<bean id="dataSourceServer"
	class="org.apache.commons.dbcp.BasicDataSource"
	destroy-method="close">
	<property name="driverClassName" value="${driverClassName}" />
	<property name="url" value="${url}" />
	<property name="username" value="${username}" />
	<property name="password" value="${password}" />
	<property name="initialSize" value="4" />
	<property name="maxActive" value="20" />
	<property name="testWhileIdle" value="true" />
	<property name="validationQuery"
		value="select count(0) from t00_trans where 1=2" />
	<property name="testOnBorrow" value="true" />
</bean>

 

2
2
分享到:
评论
2 楼 wanmeilingdu 2012-10-29  
是啊,在dao层,怎么获得数据库的连接啊?
1 楼 wanmeilingdu 2012-08-09  
你好,按你这种写法,我在dao层,怎么获得数据库的连接啊?

相关推荐

Global site tag (gtag.js) - Google Analytics