shenang博客技術(shù)文檔


          理論不懂就實(shí)踐,實(shí)踐不會就學(xué)理論!

          posts - 35,comments - 55,trackbacks - 0
             

          ApplicationContext學(xué)習(xí)

          相對BeanFactory而言,ApplicationContext提供了以下擴(kuò)展功能:

           

          1國際化支持

          我們可以在Beans.xml文件中,對程序中的語言信息(如提示信息)進(jìn)行定義,將程序中的提示

          信息抽取到配置文件中加以定義,為我們進(jìn)行應(yīng)用的各語言版本轉(zhuǎn)換提供了極大的靈活性。

          2資源訪問

          支持對文件和URL的訪問。

          3事件傳播

          事件傳播特性為系統(tǒng)中狀態(tài)改變時(shí)的檢測提供了良好支持。

          4多實(shí)例加載

          可以在同一個(gè)應(yīng)用中加載多個(gè)Context實(shí)例。

           下面我們就這些特性逐一進(jìn)行介紹。

          1)    國際化支持

           配置文件

          <beans>

               <description>Spring Quick Start</description>

           <bean id="messageSource" <!—注意,這里名字必須為messageSource -->

          class="org.springframework.context.support.ResourceB

          undleMessageSource">

             <property name="basenames">  

               <list>

                 <value>messages</value>

               </list>

             </property>

           </bean>

          </beans>

          在配置節(jié)點(diǎn)中,我們指定了一個(gè)配置名“messages”。Spring會自動在CLASSPATH根路徑中按照如下順序搜尋配置文件并進(jìn)行加載(以Localezh_CN為例):

          messages_zh_CN.properties

          messages_zh.properties

          messages.properties

          messages_zh_CN.class

          messages_zh.class

          messages.class

          再加入二個(gè)properties文件

          示例中包含了兩個(gè)配置文件,內(nèi)容如下:

          messages_zh_CN.properties

           

          userinfo=當(dāng)前登錄用戶: [{0}] 登錄時(shí)間:[{1}]

           

          messages_en_US.properties

           

          userinfo=Current Login user: [{0}] Login time:[{1}]

          測試

          import java.util.Calendar;

          import java.util.Locale;

          import org.springframework.context.ApplicationContext;

          import org.springframework.context.support.ClassPathXmlApplicationContext;

          publicclass TestAction {

              publicstaticvoid main(String[] args) {

                   ApplicationContext ctx2=new 

                  ClassPathXmlApplicationContext("applicationContext.xml");

                         Object[] arg = new Object[]{

                  "Erica",

                  Calendar.getInstance().getTime()

                  };

          //     以系統(tǒng)默認(rèn)Locale加載信息(對于中文WinXP而言,默認(rèn)為zh_CN)

                  String msg = ctx2.getMessage("userinfo", arg,Locale.US);

                         System.out.println("Message is ===> "+msg);

              }  

          }

          結(jié)果

          Message is ===> Current Login user: [Erica] Login time:[3/23/09 12:20 PM]

           

           2) 資源訪問

          ApplicationContext.getResource方法提供了對資源文件訪問支持,如:

           Resource rs = ctx.getResource("classpath:config.properties");

          File file = rs.getFile();

          上例從CLASSPATH根路徑中查找config.properties文件并獲取其文件句柄。

          getResource方法的參數(shù)為一個(gè)資源訪問地址,如:

          file:C:/config.properties

          /config.properties

          classpath:config.properties

          注意getResource返回的Resource并不一定實(shí)際存在,可以通過Resource.exists()方法對

          其進(jìn)行判斷。

          3)事件傳播

          ApplicationContext基于Observer模式(java.util包中有對應(yīng)實(shí)現(xiàn)),提供了針對Bean的事件傳

          播功能。通過Application. publishEvent方法,我們可以將事件通知系統(tǒng)內(nèi)所有的

          ApplicationListener

          posted on 2009-03-23 12:39 重慶理工小子 閱讀(437) 評論(0)  編輯  收藏 所屬分類: Spring2
          主站蜘蛛池模板: 台北市| 东台市| 兴化市| 图木舒克市| 饶阳县| 周宁县| 林西县| 水城县| 正蓝旗| 潼南县| 沙田区| 高阳县| 洮南市| 武穴市| 岑溪市| 宜昌市| 恩平市| 枣阳市| 泸定县| 崇阳县| 南昌县| 和林格尔县| 辽源市| 肇源县| 广河县| 射洪县| 沈丘县| 鹰潭市| 崇文区| 龙门县| 梅河口市| 吴旗县| 中牟县| 错那县| 黑山县| 巴中市| 昌江| 陵川县| 汾阳市| 沈阳市| 南城县|