本文內容
-
BeanFactory對比ApplicationContext
-
ApplicationContext的擴展能力
-
國際化
BeanFactory對比ApplicationContext
簡單點對比下兩者的功能定位:
- BeanFactory 提供了管理和操作 bean 的基本功能,為 Spring的IoC 功能提供了底層基礎,用于與 Spring 的其他部分以及相關的第三方框架的集成
- ApplicationContext 在 BeanFactory 基礎上還擴展了其他介面以提供更多面向應用程式框架和企業開發的附加功能,
下表列出了 BeanFactory 和 ApplicationContext 介面和實作提供的功能,
| 功能特性 | BeanFactory |
ApplicationContext |
|---|---|---|
| Bean實體化和屬性注入 | Yes | Yes |
| 生命周期管理 | No | Yes |
BeanPostProcessor 自動注冊 |
No | Yes |
BeanFactoryPotProcessor 自動注冊 |
No | Yes |
國際化 MessageSource |
No | Yes |
內置的 ApplicationEvent 發布機制 |
No | Yes |
因為ApplicationContext包含BeanFactory的所有功能,所以通常建議使用它而不是普通的BeanFactory,除非需要完全控制bean處理的場景,
ApplicationContext的擴展能力
上面討論了org.springframework.beans.factory包提供了管理和操作bean的基本功能,包括以編程的方式,org.springframework.context包添加了ApplicationContext介面,它擴展了BeanFactory介面,此外還擴展了其他介面,以更面向應用程式框架的方式提供額外的功能,
- 國際化: 通過 MessageSource 介面以 i18n 方式訪問訊息
- 通過 ResourceLoader 介面訪問資源,例如 URL 和檔案
- 事件發布,即通過使用 ApplicationEventPublisher 介面發布到實作 ApplicationListener 介面的 bean
- 通過 HierarchicalBeanFactory 介面加載多個(分層)背景關系,讓每個背景關系都專注于一個特定的層,例如應用程式的 Web 層
國際化 MessageSource
ApplicationContext 介面擴展了一個名為 MessageSource 的介面,因此提供了國際化(“i18n”)功能,
MessageSource 介面定義和主要方法如下
public interface MessageSource {
// 獲取訊息: code訊息key args替換內支持{} default默認值 loc語言
String getMessage(String code, Object[] args, String default, Locale loc);
String getMessage(String code, Object[] args, Locale loc)
String getMessage(String code, Object[] args, Locale loc)
}
java.util.Locale物件表示特定的地理、政治或文化區域,如中國是zh
Spring中查找加載MessageSource程序
- 加載 ApplicationContext 時,它會自動搜索背景關系中定義的 MessageSource bean, bean 必須具有名稱 messageSource,
- 如果找到了這樣的bean,那么對上述方法的所有呼叫都被委托給訊息源,
- 如果沒有找到訊息源,ApplicationContext將嘗試查找包含具有相同名稱的bean的父類,如果有,則使用該bean作為MessageSource,
- 如果 ApplicationContext 找不到任何訊息源,則實體化一個空的 DelegatingMessageSource 以便能夠接受對上面定義的方法的呼叫,
MessageSource 實作
Spring 提供了三個 MessageSource 實作:
-
ResourceBundleMessageSource
使用指定的基本名稱訪問資源包,這個類依賴于底層 JDK 的 ResourceBundle 實作,結合 MessageFormat 提供的 JDK 標準訊息決議,
-
ReloadableResourceBundleMessageSource
Spring 特定的實作,使用指定的基本名稱訪問資源包,參與 Spring .ApplicationContext 的資源加載,與基于 JDK 的 ResourceBundleMessageSource 相比,該類使用
java.util.Properties實體作為其自定義的訊息資料結構,通過 Spring Resource 句柄的org.springframework.util.PropertiesPersister策略加載它們,該策略不僅能夠基于時間戳更改重新加載檔案,還能夠加載具有特定字符編碼的屬性檔案,它還將檢測 XML 屬性檔案, -
StaticMessageSource,
MessageSource 的簡單實作,它允許以編程方式注冊訊息,此 MessageSource 支持基本的國際化,用于測驗而不是用于生產系統,
它們都實作了 HierarchicalMessageSource 以進行嵌套訊息傳遞,
Resource 相關的知識點后續文章已經安排上
綜合案例
提供一個ResourceBundleMessageSource 的案例,方便理解,
-
類路徑定義國際化資源檔案,定義默認和中文的
demo16/exceptions_en.properties 檔案,支持{..}占位符內容替換
argument.required=The {0} argument is required.demo16/exceptions_zh.properties 檔案
argument.required={0} 引數是必須的.demo16/format_zh.properties 檔案
message=中文訊息demo16/format_en.properties 檔案
message=en -
注入 ResourceBundleMessageSource 實體,名稱為 messageSource
@Configuration @ComponentScan public class AppConfig { @Bean("messageSource") public ResourceBundleMessageSource messageSource() { ResourceBundleMessageSource source = new ResourceBundleMessageSource(); // 設定類路徑下的資源檔案 source.setBasenames("demo16/format","demo16/exceptions","demo16/windows"); return source; } } -
bean中呼叫 MessageSource 介面方法使用
@Component public class MyBean { @Autowired private MessageSource messageSource; // 獲取訊息內容 public void execute() { System.out.println("獲取訊息內容:"); String message = this.messageSource.getMessage("message", null, "Default", Locale.ENGLISH); System.out.println(message); } // 替換占位符內容 字串形式 public void execute1() { System.out.println(" 替換占位符內容 字串形式: "); String message = this.messageSource.getMessage("argument.required", new Object[]{"messageSource"}, "Required", Locale.ENGLISH); System.out.println(message); } // 獲取中文訊息 public void getChinesMessage() { System.out.println("替換占位符內容 字串形式,語言是中文: "); String message = this.messageSource.getMessage("argument.required", new Object[]{"messageSource"}, "Required", Locale.CHINESE); System.out.println(message); } } -
測驗并觀察結果
@Test public void test1() { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class); MyBean myBean = context.getBean(MyBean.class); myBean.execute(); myBean.execute1(); myBean.getChinesMessage(); }獲取訊息內容: en 替換占位符內容 字串形式: The messageSource argument is required. 替換占位符內容 字串形式,語言是中文: messageSource 引數是必須的.已經可以根據語言進行國際化切換了
總結
本文介紹了Spring中BeanFactory和AppplicationContext的對比,重點介紹了AppplicationContext中擴展實作國際化,
本篇原始碼地址: https://github.com/kongxubihai/pdf-spring-series/tree/main/spring-series-ioc/src/main/java/com/crab/spring/ioc/demo16
知識分享,轉載請注明出處,學無先后,達者為先!
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/445287.html
標籤:其他
上一篇:Python爬蟲案例:采集Tripadvisor資料
下一篇:請求合并與拆分在并發場景中應用
