1. 程式人生 > 實用技巧 >Spring基於配置檔案裝配Bean(6) ------bean引用外部屬性檔案

Spring基於配置檔案裝配Bean(6) ------bean引用外部屬性檔案

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4        xmlns:context="http://www.springframework.org/schema/context"
 5        xsi:schemaLocation="http://www.springframework.org/schema/beans
 6
http://www.springframework.org/schema/beans/spring-beans.xsd 7 http://www.springframework.org/schema/context 8 http://www.springframework.org/schema/context/spring-context-4.2.xsd"> 9 10 <!--測試bean引用外部屬性--> 11 <!--<config id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">--> 12
<!--<property name="username" value="root"/>--> 13 <!--<property name="password" value="123456"/>--> 14 <!--<property name="url" value="jdbc:mysql://127.0.0.1:3306/ylws"/>--> 15 <!--<property name="driverClassName" value="com.mysql.jdbc.Driver"/>--> 16
<!--</config>--> 17 18 <!--引入外部屬性檔案--> 19 <context:property-placeholder location="classpath:config/jdbc.properties"/> 20 21 <!--c3p0--> 22 <bean id="dataSourceC3p0" class="com.mchange.v2.c3p0.ComboPooledDataSource"> 23 <!--引用properties檔案--> 24 <property name="user" value="${user}"/> 25 <property name="password" value="${password}"/> 26 <property name="jdbcUrl" value="${jdbcUrl}"/> 27 <property name="driverClass" value="${driverClass}"/> 28 </bean> 29 30 <!--通過Properties配置屬性--> 31 <!--<config id="dataSource" class="com.spring.cn.configig.collections.DataSource"> 32 <property name="properties"> 33 <props> 34 <prop key="username">root</prop> 35 <prop key="password">123456</prop> 36 <prop key="url">jdbc:mysql//localhost:3306?</prop> 37 <prop key="driver">com.mysql.cj.jdbc.driver</prop> 38 </props> 39 </property> 40 </config>--> 41 </beans>

jdbc.properties

1 user=root
2 password=123456
3 jdbcUrl=jdbc:mysql://127.0.0.1:3306/ylws
4 driverClass=com.mysql.jdbc.Driver