spring中通过properties文件配置数据源

本文介绍了在Spring框架中如何使用jdbc.properties文件来配置数据源,详细讲解了两种不同的配置方法,帮助读者理解如何将数据库连接信息从代码中分离,实现更灵活的配置管理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

jdbc.properties文件:

#driver= oracle.jdbc.driver.OracleDriver
driver=com.mysql.jdbc.Driver
#url=jdbc:oracle:thin:@127.0.0.1:1521:test
url=jdbc:mysql://127.0.0.1:3306/test
username=root
password=12345

 

方法一:

<beans
     xmlns="http://www.springframework.org/schema/beans"
     xmlns:tx="http://www.springframework.org/schema/tx"
     xmlns:aop="http://www.springframework.org/schema/aop"
     xmlns:context="http://www.springframework.org/schema/context"
     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-3.0.xsd
                        http://www.springframework.org/schema/beans
                        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
                        http://www.springframework.org/schema/tx
                        http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
                        http://www.springframework.org/schema/aop
                        http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
                        http://www.springframework.org/schema/context
                        http://www.springframework.org/schema/context/spring-context-3.0.xsd">
     <context:property-placeholder location="classpath:jdbc.properties"/>
    
     <!--<context:component-scan base-package="com"></context:component-scan>  -->
     <bean id="dataSource_1" class="org.logicalcobwebs.proxool.ProxoolDataSource" >
         <property name="driver" value="${driver}"/>
        <property name="driverUrl" value="${url}"/>
        <property name="user" value="${username}"/>
        <property name="password" value="${password}" />
        <property name="maximumActiveTime" value="${maximumActiveTime}" />
     </bean >
    
     <bean id="dataSource" class="org.logicalcobwebs.proxool.ProxoolDataSource" >
        <property name="driver" value="${driver}"/>
        <property name="driverUrl" value="${url}"/>
        <property name="user" value="${username}"/>
        <property name="password" value="${password}"/>
        <property name="maximumConnectionCount" value="200" ></property>
     </bean>

方法二:

<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.5.xsd">
    
    <!--配置数据源属性文件  -->
    <bean id="propertyConfigurer"
         class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
          <property name="location">
              <value>/WEB-INF/configs/sqlServer.properties</value>
          </property>
    </bean>
    
    <!--配置数据源  -->
    <bean id="dataSource"
         class="org.springframework.jdbc.datasource.DriverManagerDataSource">
          <property name="driverClassName">
              <value>${jdbc.driver}</value>
          </property>
          <property name="url" >
              <value>${jdbc.url}</value>
          </property>
          <property name="username" >
              <value>${jdbc.user}</value>
          </property>
          <property name="password">
              <value>${jdbc.pwd}</value>
          </property>
    </bean>



 


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值