Spring-数据源对象管理-加载properties文件

管理DruidDataSource对象

1、引入坐标

 <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.16</version>
        </dependency>

2、创建bean

<?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.xsd">

<!--    管理DruidDataSource对象-->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value=""/>
        <property name="url" value=""/>
        <property name="username" value="root"/>
        <property name="password" value="123456"/>
    </bean>

</beans>

管理c3p0 DataSource对象

1、引入坐标

        <dependency>
            <groupId>c3p0</groupId>
            <artifactId>c3p0</artifactId>
            <version>0.9.1.2</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.16</version>
        </dependency>

2、创建bean

<!--        管理c3p0DataSource对象-->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value=""/>
        <property name="jdbcUrl" value=""/>
        <property name="user" value="root"/>
        <property name="password" value="123456"/>
    </bean>

加载properties文件

1、开启context命名空间

2、使用context空间加载properties文件

3、使用属性占位符${}读取properties文件中的属性

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

<!--    1、开启context命名空间-->
<!--    2、使用context空间加载properties文件-->
    <context:property-placeholder location="jdbc.properties"/>
<!--    3、使用属性占位符${}读取properties文件中的属性-->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value="${jdbc.driver}"/>
        <property name="url" value="${jdbc.url}"/>
        <property name="username" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
    </bean>


</beans>
使用context空间加载properties文件时,可以添加system-properties-mode="NEVER",这样便不会加载系统变量属性,防止properties中命名跟系统变量冲突
    <context:property-placeholder location="jdbc.properties" system-properties-mode="NEVER"/>

加载多个properties可使用逗号分隔

    <context:property-placeholder location="jdbc.properties,jdbc2.properties" system-properties-mode="NEVER"/>

或者加载全部的properties:::location="classpath:*.properties"

    <context:property-placeholder location="classpath:*.properties" system-properties-mode="NEVER"/>

或者加载引用的jar包中的的properties:::location="classpath*:*.properties"

    <context:property-placeholder location="classpath*:*.properties" system-properties-mode="NEVER"/>

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值