stone2083

          小談PropertyPlaceholderConfigurer

          背景:讓應用在一個環境下,以多實例的方法運行.
          Log問題,可以通過Log4j占位符實現(見前文:http://www.aygfsteel.com/stone2083/archive/2010/07/01/324935.html)
          其他Java組件代碼依賴了本地環境資源,怎么解決呢?

          對于使用Spring的組件來說,PropertyPlaceholderConfigurer能幫我們解決這一問題.

          PropertyPlaceholderConfigurer除了支持配置的properties文件外,還支持系統屬性(System.getProperties()).當然,它有三種模式:
           1/** Never check system properties. */
           2public static final int SYSTEM_PROPERTIES_MODE_NEVER = 0;
           3 
           4/**
           5 * Check system properties if not resolvable in the specified properties.
           6 * This is the default.
           7 */
           8public static final int SYSTEM_PROPERTIES_MODE_FALLBACK = 1;
           9 
          10/**
          11 * Check system properties first, before trying the specified properties.
          12 * This allows system properties to override any other property source.
          13 */
          14public static final int SYSTEM_PROPERTIES_MODE_OVERRIDE = 2;

          對于使用本地環境資源的bean來說,只要配置:
           1 <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
           2     <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
           3     <property name="locations">
           4         <list>
           5             <value>classpath*:spring/env.properties</value> <!--無需配置node-->
           6         </list>
           7     </property>
           8 </bean>
           9 
          10 <bean id="javaBean" class="com.alibaba.javalab.spring.JavaBean">
          11      <property name="lockFile" value="/home/stone/base/${node}/lock" />
          12 </bean>


          在啟動腳本中,只要加入-Dnode=instanceX即可.

          總結:
          PropertyPlaceholderConfigurer支持properties文件和系統屬性.并且存在三種覆蓋策略.

          posted on 2010-07-23 08:51 stone2083 閱讀(1281) 評論(0)  編輯  收藏 所屬分類: java

          主站蜘蛛池模板: 巴东县| 贺兰县| 洛浦县| 河南省| 桐柏县| 九寨沟县| 龙陵县| 呼和浩特市| 滦南县| 鱼台县| 景洪市| 长乐市| 永新县| 榕江县| 桃源县| 台山市| 玉山县| 沈丘县| 新建县| 凉城县| 漾濞| 柯坪县| 观塘区| 从江县| 平阴县| 皮山县| 武夷山市| 柯坪县| 玉山县| 从江县| 手游| 贵港市| 新平| 民县| 浦县| 微博| 游戏| 酉阳| 杭州市| 涞源县| 建平县|