Spring框架学习:IOC操作Bean管理-引入外部属性文件(九)

前置:引入德鲁伊jar包-druid-1.1.9.jar

1.properties文件

prop.DriverClass=com.mysql.jdbc.Driver
prop.url=jdbc:mysql://localhsot:3307/test
prop.userName=root
prop.passWord=root

2.引入context命名空间

<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"
       xmlns:util="http://www.springframework.org/schema/util"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
                           http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd
                           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

3.引入配置文件,创建dataSource Bean对象

 <!--直接配置连接池-->
        <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
            <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
            <property name="url" value="jdbc:mysql://localhost:3307:test"></property>
            <property name="username" value="root"></property>
            <property name="password" value="root"></property>
        </bean>

        <!--引入外部属性文件-->
        <context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder>
        <!--配置连接池-->
        <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
            <property name="driverClassName" value="${prop.DriverClass}"></property>
            <property name="url" value="${prop.url}"></property>
            <property name="username" value="${prop.userName}"></property>
            <property name="password" value="${prop.passWord}"></property>
        </bean>
暂无评论

发送评论 编辑评论


				
上一篇
下一篇