XML裡讀入properties檔

xml

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

    <!-- read properties file -->
    <context:property-placeholder location="classpath:db.properties" />    
    <!-- dataSource -->    
    <bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
        <property name="driverClass" value="${db.driverClass}" />        
        <property name="jdbcUrl" value="${db.jdbcUrl}" />        
        <property name="user" value="${db.user}" />        
        <property name="password" value="${db.password}" />    
    </bean>
</beans>

說明: 本例讀入src/db.properties,記得加上context定義:

xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.2.xsd"

db.properties

db.jdbcUrl=jdbc:mysql://127.0.0.1:3306/testHibernate
db.driverClass=com.mysql.cj.jdbc.Driver
db.user=webuser
db.password=

results matching ""

    No results matching ""