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 閱讀(1283) 評論(0)  編輯  收藏 所屬分類: java

          主站蜘蛛池模板: 正蓝旗| 册亨县| 平原县| 徐水县| 永昌县| 绥芬河市| 呼图壁县| 磐安县| 平顺县| 桐柏县| 育儿| 阳泉市| 黎城县| 奉化市| 梅河口市| 远安县| 平南县| 双牌县| 潜江市| 榆树市| 宿松县| 溧阳市| 台中县| 鲁甸县| 大邑县| 石泉县| 安远县| 玉山县| 霍城县| 石渠县| 汤原县| 靖边县| 安达市| 蒙自县| 左权县| 黄山市| 汨罗市| 广汉市| 洛扎县| 东乌| 石家庄市|