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

          主站蜘蛛池模板: 徐水县| 渑池县| 宁陕县| 枞阳县| 武陟县| 东光县| 略阳县| 龙游县| 牟定县| 宝坻区| 稷山县| 沂南县| 嘉鱼县| 马龙县| 奉节县| 上思县| 措美县| 江油市| 九龙坡区| 大埔县| 淮南市| 乌拉特前旗| 全南县| 峨边| 宝鸡市| 肥西县| 沧州市| 鹤山市| 栾城县| 修武县| 木里| 通州市| 旺苍县| 科尔| 湖口县| 罗田县| 白银市| 贵溪市| 光泽县| 连城县| 尤溪县|