使用Spring中Src下 -context.xml : 用资源文件夹保存数据库的链接信息 并在配置文件里读取

db.user=root

db.password=fulee2014
db.url=jdbc:mysql://127.0.0.1:3309/haofangjia?characterEncoding=GBK
db.driverClass=com.mysql.jdbc.Driver



 


src下的项目名-context

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" 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.xsd
http://www.springframework.org/schema/context 
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop 
http://www.springframework.org/schema/aop/spring-aop.xsd">


<!-- 自动装配的业务类必须指定要装配的类所处的包 -->
<context:component-scan
base-package="com.cn.fuleehaofangjia.biz.impl,com.cn.fuleehaofangjia.dao.impl" />


<!-- 配置外部资源文件 --><!-- <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 
<property name="locations" value="classpath:db.properties"></property> </bean> -->


<!-- 配置外部资源文件 多个文件使用"," -->
<context:property-placeholder
location="classpath:db.properties" />


<!-- c3p0连接池 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<!-- ${外部资源文件中key} -->
<property name="user" value="${db.user}" />
<property name="password" value="${db.password}" />
<property name="driverClass" value="${db.driverClass}" />
<property name="jdbcUrl" value="${db.url}" />
</bean>


<!-- Spring中默认的连接池 <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 
<property name="username" value="root"/> <property name="password" value="admin"/> 
<property name="driverClassName" value="com.mysql.jdbc.Driver"/> <property 
name="url" value="jdbc:mysql:///springJdbc"/> </bean> -->


<!-- apache连接池 dbcp -->
<!--<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> 
<property name="username" value="root"/> <property name="password" value="admin"/> 
<property name="driverClassName" value="com.mysql.jdbc.Driver"/> <property 
name="url" value="jdbc:mysql:///springJdbc"/> </bean> -->






<!-- Spring JdbcTemplate -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"
scope="prototype">
<constructor-arg ref="dataSource" />
<property name="dataSource" ref="dataSource" />
</bean>


</beans>



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值