Spring 中 用 ${xxx} 讀取properties文件的說明


properties 如果在 spring 中通過 PropertyPlaceholderConfigurer 加載,當spring 中需要 用到 properties 中的一些 key 和value 值時可以 利用 PorpertyPlaceholderConfiger 提供的$ 直接 取得。

PorpertyPlaceholderConfiger 有一些常用的屬性,在一些高級應用中,可能會用到

  • locations 
  • fileEncoding 屬性文件的編碼格式
  • order 文件中定義了多個PropertyPlaceholderConfigurer,則通過該屬性指定優先順序。
  • placeholderPrefix 默認前綴占位符是"${"。可以根據需要改為其它的前綴符。  --- 這個特點估計就是 spring 中能用${xxx} 取得 properties 文件中的內容的理由吧,即估計是spring只要看到 是 ${  占位符 就會 到 PropertyPlaceholderConfigurer 中去找其加載的 properties文件,從而 spring能用${}取得 properties 中屬性值。
  • placeholdersuffix 默認后綴占位符是“}”。
  • 由於 PropertyPlaceholderConfigurer內置的功能非常豐富,如果它未找到${xxx}中定義的xxx鍵,它還會去JVM系統屬性(System.getProperty())和環境變量(System.getenv())中尋找。通過啟用systemPropertiesMode和searchSystemEnvironment屬性,開發者能夠控制這一行為。
  • ${} 還可以直接用在 java bean 上即用在類上,詳見:http://www.cnblogs.com/wzhanke/p/4838890.html

舉例如下:

<bean

class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath*:*.properties</value>
</list>
</property>
</bean>
 
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
  <property name="driverClass" value="${driverClass}"></property>   --->表明 properties文件中有 driverClass = aaa,通過${driverClass} 就能拿到 aaa值了
  <property name="jdbcUrl" value="${url}"></property>
</bean>
 
 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM