Spring抽取jdbc配置文件

引入context命名空间和约束路径

命名空间:

xmlns:context="http://www.springframework.org/schema/context"

约束路径


http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd

引入jdbc.properties文件

<context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder>

然后就可以动态引用了

<bean id="druidDataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value="${jdbcdriver}"></property>
        <property name="url" value="${url}"></property>
        <property name="username" value="${username}"></property>
        <property name="password" value="${password}"></property>
    </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"
       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/context  http://www.springframework.org/schema/context/spring-context.xsd">

    <context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder>

    <bean id="druidDataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value="${jdbcdriver}"></property>
        <property name="url" value="${url}"></property>
        <property name="username" value="${username}"></property>
        <property name="password" value="${password}"></property>
    </bean>


</beans>
原文地址:https://www.cnblogs.com/lyd447113735/p/14249146.html