好記性不如爛筆頭84-spring3學習(5)-spring的ApplicationContext(應用上下文)

來源:互聯網
上載者:User

標籤:beanfactory   applicationcontext   好記性不如爛筆頭   spring   

我們一般把BeanFactory叫做IoC容器,叫ApplicationContext 為應用上下文(或者Spring容器);
如果說BeanFactory是Spring的心臟,那麼ApplicationContext就是Spring的完整的身軀了;
ApplicationContext由BeanFactory派生而來,提供了更加多的面向實際應用的功能;
在BeanFactory中,很多功能需要以編程的方式實現,而在ApplicationContext中則可以通過配置的方式實現。

ApplicationContext還提供了以下的功能:
1) MessageSource,提供國際化的訊息訪問
2)資源訪問,如URL和檔案
3)事件傳播,實現了ApplicationListener介面的bean
4)應用多個上文,使得每一個上下文都專註於一個特定的層次,比如應用的web層.

1.MessageSource

ApplicationContext介面擴充了MessageSource介面,因而提供了訊息處理的功能(i18n或者國際化)。與HierarchicalMessageSource一起使用,它還能夠處理嵌套的訊息,這些是Spring提供的處理訊息的基本介面。讓我們快速探索一下它所定義的方法:
? String getMessage(String code, Object[] args, String default, Locale loc):用來從MessageSource擷取訊息的基本方法。如果在指定的locale中沒有找到訊息,則使用預設的訊息。args中的參數將使用標準類庫中的MessageFormat來作訊息中替換值。
? String getMessage(String code, Object[] args, Locale loc):本質上和上一個方法相同,其區別在:沒有指定預設值,如果沒找到訊息,會拋出一個NoSuchMessageException異常。
? String getMessage(MessageSourceResolvable resolvable, Locale locale):上面方法中所使用的屬性都封裝到一個MessageSourceResolvable實現中,而本方法可以指定MessageSourceResolvable實現。
當一個ApplicationContext被載入時,它會自動在context中尋找已定義為MessageSource類型的bean。此bean的名稱須為messageSource。如果找到,那麼所有對上述方法的調用將被委託給該bean。否則ApplicationContext會在其父類中尋找是否含有同名的bean。如果有,就把它作為MessageSource。如果它最終沒有找到任何的訊息源,一個空的StaticMessageSource將會被執行個體化,使它能夠接受上述方法的調用。

ApplicationContext載入多個設定檔,並讀取
Bean.xml的配置情況<beans>  <bean id="messageSource"        class="org.springframework.context.support.ResourceBundleMessageSource">    <property name="basenames">      <list>        <value>one</value>        <value>two</value>      </list>    </property>  </bean></beans>

這段設定檔告訴我們資源檔載入的方式,它可以從one.properties,two.properties,兩個檔案裡分別載入我們需要的資源,且它們是按設定檔夾的順序載入的。

one.properties# in ‘one.properties‘one=one format!two.properties# in ‘two.properties‘message=one format!
ApplicationContext載入多個設定檔的JAVA原始碼
package com.spring;import org.springframework.context.MessageSource;import org.springframework.context.support.ClassPathXmlApplicationContext;/**   * 簡單類比ApplicationContext載入多個設定檔,並讀取屬性 *   * @author 範芳銘 */ public class EasyApplicationContext {    public static void main(String[] args) {        MessageSource resources = new ClassPathXmlApplicationContext("bean.xml");        String message = resources.getMessage("message", null, "Default", null);        System.out.println(message);    }}    

好記性不如爛筆頭84-spring3學習(5)-spring的ApplicationContext(應用上下文)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.