在spring裏面用${}是可以獲得配置文件裏面的值。比如你的<bean id="xxx" class="xxx.xxx.xxx.${xmlTool}">這個裏面xmlTool是在另一個配置文件裏面配了的,在spring裏面就可以用${xmlTool}引用進來。以後如果你要對xmlTool做修改的話,也只用修改配置文件,不用修改代碼。好處就是如果你的程序要修改的時候,就只用改${}裏配置文件的內容就行了,不用改spring裏的配置文件的內容。
那麼問題來了,spring這麼知道我的配置文件是哪個呢?這個當然是需要配置啦!
方法一:使用bean的注入來引入配置文件:
<!-- 引入配置文件 -->
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath*:jdbc.properties" />
</bean>
方法二:spring3提供更簡單的方式,使用自動掃描的方式:
<!-- 配置文件 -->
<context:property-placeholder location="classpath*:jdbc.properties" />
以上兩種方式都可以將指定的配置文件加載進來,然後通過${}符號的引用,即可通過外部對變量的修改,來進行切換,不需要每次改動內部的值!
jdbc.properties文件的內容格式也很簡單,是鍵值對的方式,#表示註釋,比如:
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://127.0.0.1:3306/test?autoReconnect=true
username=root
password=root
#定義初始連接數
initialSize=5
#定義最大連接數
maxActive=20
#定義最大空閒
maxIdle=20
#定義最小空閒
minIdle=1
#定義最長等待時間
maxWait=60000
參考出處:
https://zhidao.baidu.com/question/2055440848342908507.html
http://www.cnblogs.com/handsomeye/p/6432203.html