日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

Spring xml 配置使用外部config 文件

發布時間:2025/3/20 javascript 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Spring xml 配置使用外部config 文件 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Spring xml 配置使用外部config 文件

當使用Spring framework后, 我們一般會把db connection的信息寫在spring的bean config xml里面。

例如:

<?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"><bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"><property name="user" value="testUser1"></property><property name="password" value="12345678"></property><property name="driverClass" value="com.mysql.jdbc.Driver"></property><property name="jdbcUrl" value="jdbc:mysql://homeServer:3306"></property></bean> </beans>

但是在項目中, 我們的程序一般在不同的環境中運行,如果每次都去修改xml就會出現如下的問題。

1.容易出錯

2.需要重新打包(xml在包里的話)

3.程序猿會見到密碼等敏感信息。

所以我們一般會把項目deployment的信息例如db Conection寫在 外部的1個 config file中。

例子

首先在系統中隨便1個位置建立1個config 文件:

/home/gateman/Studies/java_start/configfiles/mysql.config

user=testUser1 password=12345678 driver=com.mysql.jdbc.Driver url=jdbc:mysql://homeServer:3306

在spring xml中引入 PropertyPlaceholderConfigurer 這個bean

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"><property name="location" value="file:/home/gateman/Studies/java_start/configfiles/mysql.config"></property></bean>

注意 上面的location value就是配置文件的位置
file:xxx 代表絕對路徑
classpath: xxx 代表類路徑

最后, 在dataSoruce 這個bean就可以引用config file的值了

<?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"><bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"><property name="location" value="file:/home/gateman/Studies/java_start/configfiles/mysql.config"></property></bean><bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"><property name="user" value="${user}"></property><property name="password" value="${password}"></property><property name="driverClass" value="${driver}"></property><property name="jdbcUrl" value="${url}"></property></bean> </beans>

總結

以上是生活随笔為你收集整理的Spring xml 配置使用外部config 文件的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。