Spring 国际化支持之 MessageSource

简介: 背景为了友好的支持各个国家的语言,Java 本身已经提供了对国际化的支持,上篇文章《Java 国际化与文本格式化》已经介绍了 Java 对国际化的支持。

背景


为了友好的支持各个国家的语言,Java 本身已经提供了对国际化的支持,上篇文章《Java 国际化与文本格式化》已经介绍了 Java 对国际化的支持。Java 对国际化的支持主要是使用 ResourceBundle 根据区域信息选取不同的 properties 文件获取 key 对应的 value,并且 Java 还提供了 MessageFormat 来支持对消息的格式化,Spring 将这两者进行结合,抽象出了 MessageSource 接口,使我们更方便的在 Spring 中获取国际化消息。


Spring 中的 MessageSource 接口


MessageSource 接口定义


认识 Spring 国际化支持,先从 MessageSource 接口看起,这是 Spring 国际化的核心接口,其定义如下。


public interface MessageSource {
  @Nullable
  String getMessage(String code, @Nullable Object[] args, @Nullable String defaultMessage, Locale locale);
  String getMessage(String code, @Nullable Object[] args, Locale locale) throws NoSuchMessageException;
  String getMessage(MessageSourceResolvable resolvable, Locale locale) throws NoSuchMessageException;
}


MessageSource 接口提供了三个获取国际化消息的方法,其主要是根据 Locale 信息获取对应的国际化消息的集合,然后根据 code 获取对应的消息,并且通过提供的参数 args 还可以对获取后的消息进行格式化。具体参数含义如下。


code:要查找的消息编号。

args:为消息中的参数填充的值。

defaultMessage:默认的消息,如果没有找到将返回默认消息。

resolvable:消息参数,封装了 code、args、defaultMessage。


MessageSource 主要实现


MessageSource 在 Spring 中主要有两个实现,具体如下。


ResourceBundleMessageSource:底层使用 ResourceBundle 和 MessageFormat 实现的 MessageSource。

ReloadableResourceBundleMessageSource:支持国际化文件动态刷新的 MessageSource。

DelegatingMessageSource:代理父 MessageSource 的 MessageSource,获取消息时将委托父 MessageSource 获取,这是 Spring 应用上下文默认创建的 MessageSource。


如何在 Spring 中获取 MessageSource


我们经常说 Spring 的基础容器是 BeanFactory,ApplicationContext 是对 BeanFactory,提供了企业级的特性,其中就包括了对国际化的支持,这是因为 ApplicationContext 接口就继承了 MessageSource 接口,因此就可以认为 ApplicationContext 就是 MessageSource ,在 Spring 应用上下文刷新时将调用 AbstractApplicationContext#initMessageSource 初始化 MessageSource,这将保证 Spring 中一定存在一个类型为 MessageSource 的 bean,ApplicationContext 底层正是依托这个 bean 来实现 MessageSource 接口的方法。因此具有以下的方法获取 MessageSource。


依赖查找 MessageSource,其中 bean 的名称为 messageSource,即AbstractApplicationContext#MESSAGE_SOURCE_BEAN_NAME。

依赖注入 MessageSource。

直接使用 ApplicationContext。

通过 MessageSourceAware 回调获取。


目录
相关文章
|
8天前
|
Java 数据安全/隐私保护 网络架构
一个简单的示例在spring boot中实现国际化
一个简单的示例在spring boot中实现国际化
|
1月前
|
存储 Java Spring
Spring之国际化:i18n
【1月更文挑战第17天】 一、i18n概述 二、Java国际化 三、Spring6国际化 1、MessageSource接口 2、使用Spring6国际化
66 1
|
1月前
|
缓存 Java Spring
Spring Boot做国际化
Spring Boot做国际化
|
10月前
|
Java Spring 容器
Spring Boot入门(十五) 之 国际化操作(页面的中英文相互切换)
Spring Boot入门(十五) 之 国际化操作(页面的中英文相互切换)
490 0
|
1月前
|
存储 人工智能 运维
spring国际化 - i18n
spring国际化 - i18n
53 0
|
1月前
|
存储 开发框架 自然语言处理
Spring6 i18n国际化
Spring6 i18n国际化
|
10月前
|
自然语言处理 前端开发 Java
【第十二篇】Spring Boot中的页面国际化
【第十二篇】Spring Boot中的页面国际化
117 0
|
自然语言处理 Java Spring
Spring Context 国际化支持
Spring Context 国际化支持
|
Java Spring 安全
Spring的国际化资源messageSource
Spring中可以使用两个类加载资源文件:ReloadableResourceBundleMessageSource和ResourceBundleMessageSource。 可配置如下messageSource这个bean id不能变: @Bean public MessageS...
1337 0
|
3天前
|
Java
springboot自定义拦截器,校验token
springboot自定义拦截器,校验token
20 6