• 导读
  • 概述
  • MessageSource接口方法
  • MessageSource类结构
  • ResourceBundleMessageSource
    • 实例
  • ReloadableResourceBundleMessageSource
    • 实例

导读

Spring-国际化信息01-基础知识

Spring-国际化信息02-MessageSource接口

Spring-国际化信息03-容器级的国际化信息资源


概述

spring定义了访问国际化信息的MessageSource接口,并提供了几个易用的实现类.


MessageSource接口方法

我们先看下源码,先来了解一下该接口的几个重要方法

  • String getMessage(String code, Object[] args, String defaultMessage, Locale locale)
    code表示国际化资源中的属性名;args用于传递格式化串占位符所用的运行期参数;当在资源找不到对应属性名时,返回defaultMessage参数所指定的默认信息;locale表示本地化对象;

  • String getMessage(String code, Object[] args, Locale locale) throws NoSuchMessageException;

    与上面的方法类似,只不过在找不到资源中对应的属性名时,直接抛出NoSuchMessageException异常;

  • String getMessage(MessageSourceResolvable resolvable, Locale locale) throws NoSuchMessageException;

    MessageSourceResolvable 将属性名、参数数组以及默认信息封装起来,它的功能和第一个接口方法相同。


MessageSource类结构

其中:

HierarchicalMessageSource接口添加了两个方法,建立父子层级的MessageSource结构 ,setParentMessageSource (MessageSource parent)方法用于设置父MessageSource,而getParentMessageSource()方法用于返回父MessageSource。

HierarchicalMessageSource接口最重要的两个实现类是
ResourceBundleMessageSource 和ReloadableResourceBundleMessageSource。

  • 它们基于Java的ResourceBundle基础类实现,允许仅通过资源名加载国际化资源。
  • ReloadableResourceBundleMessageSource提供了定时刷新功能,允许在不重启系统的情况下,更新资源的信息。
  • StaticMessageSource主要用于程序测试,它允许通过编程的方式提供国际化信息。
  • DelegatingMessageSource是为方便操作父MessageSource而提供的代理类。

ResourceBundleMessageSource

该实现类允许用户通过beanName指定一个资源名(包括类路径的全限定资源名),或通过beanNames指定一组资源名.

实例

代码已托管到Github—> https://github.com/yangshangwei/SpringMaster

资源文件:

greeting.common=How are you {0}?,today is {1}
greeting.morning=Good Morning {0}! now is {1,time,short}
greeting.afternoon=Good Afternoon {0}! now is {1,date,long}

通过ResourceBundleMessageSource配置Bean

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:util="http://www.springframework.org/schema/util" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd"><bean id="resource"class="org.springframework.context.support.ResourceBundleMessageSource"><property name="basenames" ref="resourceList"/></bean><util:list id="resourceList"><value>i18n/fmt_resource</value></util:list></beans>

测试类

启动Spring容器,并通过MessageSource访问配置的国际化资源

package com.xgj.ioc.i18n.messageSource;import java.util.GregorianCalendar;
import java.util.Locale;import org.springframework.context.ApplicationContext;
import org.springframework.context.MessageSource;
import org.springframework.context.support.ClassPathXmlApplicationContext;public class MessageSourceTest {public static void main(String[] args) {ApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:com/xgj/ioc/i18n/messageSource/beans.xml");// 获取MessageSource的BeanMessageSource messageSource = ctx.getBean("resource",MessageSource.class);// 动态参数Object[] objs = { "Xiaogongjiang", new GregorianCalendar().getTime() };// 获取格式化的国际化信息String msg1 = messageSource.getMessage("greeting.common", objs,Locale.CHINESE);String msg2 = messageSource.getMessage("greeting.morning", objs,Locale.US);String msg3 = messageSource.getMessage("greeting.afternoon", objs,Locale.CHINESE);System.out.println(msg1);System.out.println(msg2);System.out.println(msg3);}
}

运行结果:

通过Spring我们无须再分别加载不同语言、不同国家/地区的本地化资源文件,仅仅通过资源名就可以加载整套的国际化资源文件。此外,我们无须显式使用MessageFormat操作国际化信息,仅通过MessageSource# getMessage()方法就可以完成操作了.

比较下 和 http://blog.csdn.net/yangshangwei/article/details/76946002#resourceboundle 这里的区别。


ReloadableResourceBundleMessageSource

该实现类比之于ResourceBundleMessageSource的唯一区别在于它可以定时刷新资源文件,以便在应用程序不重启的情况下感知资源文件的变化。很多生产系统都需要长时间持续运行,系统重启会给运行带来很大的负面影响。这时,通过该实现类就可以解决国际化信息更新的问题

实例

资源文件同上

通过ReloadableResourceBundleMessageSource配置资源

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:util="http://www.springframework.org/schema/util" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd"><bean id="resource"class="org.springframework.context.support.ReloadableResourceBundleMessageSource"><property name="basenames" ref="resourceList"/><!-- 刷新资源文件的周期,以秒为单位 --><property name="cacheSeconds" value="5"/></bean><util:list id="resourceList"><value>i18n/fmt_resource</value></util:list></beans>

我们通过cacheSeconds属性让ReloadableResourceBundleMessageSource每5秒钟刷新一次资源文件(在真实的应用中,刷新周期不能太短,否则频繁的刷新将带来性能上的负面影响,一般不建议小于30分钟)。cacheSeconds默认值为-1表示永不刷新,此时,该实现类的功能就蜕化为ResourceBundleMessageSource的功能。

测试类:

package com.xgj.ioc.i18n.reloadableResourceBundleMessageSource;import java.util.GregorianCalendar;
import java.util.Locale;import org.springframework.context.ApplicationContext;
import org.springframework.context.MessageSource;
import org.springframework.context.support.ClassPathXmlApplicationContext;public class ReloadableResourceBundleMessageSourceTest {public static void main(String[] args) throws Exception {ApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:com/xgj/ioc/i18n/reloadableResourceBundleMessageSource/beans.xml");MessageSource messageSource = ctx.getBean("resource",MessageSource.class);Object[] objects = { "XiaoGongJiang", new GregorianCalendar().getTime() };for (int i = 0; i < 2; i++) {String msg = messageSource.getMessage("greeting.common", objects,Locale.US);System.out.println(msg + "\nsleep 20S");Thread.sleep(20000);}}
}

让程序睡眠20秒钟,在这期间,我们将fmt_resource_en_US.properties 中的 greeting.common改为

greeting.common=***How are you {0}?,today is {1}

运行结果:

两次输出的格式化信息分别对应更改前后的内容,也即本地化资源文件的调整被自动生效了

Spring-国际化信息02-MessageSource接口相关推荐

  1. 从零开始学习springmvc(5)——Spring国际化和全局异常处理

    [项目地址] 从零开始学习springmvc 如果觉得有用可以关注一下公众号:码字不易,求赞求关注 五.Spring国际化和全局异常处理 五.Spring国际化和全局异常处理 5.1 国际化介绍 5. ...

  2. Spring 国际化 MessageSource

    假设我们正在开发一个支持多国语言的Web应用程序,要求系统能够根据客户端的系统的语言类型返回对应的界面:英文的操作系统返回英文界面,而中文的操作系统则返回中文界面--这便是典型的i18n国际化问题.对 ...

  3. Springboot国际化信息(i18n)解析

    国际化信息理解 国际化信息也称为本地化信息 . Java 通过 java.util.Locale 类来表示本地化对象,它通过 "语言类型" 和 "国家/地区" ...

  4. Spring入门到放弃篇(1)- Spring国际化

    Java原生国际化 文档地址 java官方文档 参考官方文档 自定义国际化案例 public class LocaleDemo {public static void main(String[] ar ...

  5. springboot+jsp中文乱码_【spring 国际化】springMVC、springboot国际化处理详解

    在web开发中我们常常会遇到国际化语言处理问题,那么如何来做到国际化呢? 你能get的知识点? 使用springgmvc与thymeleaf进行国际化处理. 使用springgmvc与jsp进行国际化 ...

  6. springmvc二十四:自定义国际化信息

    springmvc中区域信息是由区域信息解析器得到的. private LocaleResolver localeResolver 默认会用一个AcceptHeaderLocaleResolver 自 ...

  7. Struts2的国际化(一)-国际化资源文件的配置及国际化信息的访问

    一.概述: 1)国际化是一种技术:在程序设计领域,把在无需改写源代码即可让开发出来的应用程序能够支持多种语言和数据格式的技术称为国际化. 2)本地化是一个动作:与国际化对应的是本地化,指让一个具备国际 ...

  8. 【spring学习】02

    [spring学习]02 spring快速入门 实例 Spring配置文件 Bean标签的基本配置 Bean标签的范围配置 Spring 依赖注入 Spring引入其他配置文件 spring配置文件总 ...

  9. STS创建Spring Boot项目实战(Rest接口、数据库、用户认证、分布式Token JWT、Redis操作、日志和统一异常处理)

    STS创建Spring Boot项目实战(Rest接口.数据库.用户认证.分布式Token JWT.Redis操作.日志和统一异常处理) 1.项目创建 1.新建工程 2.选择打包方式,这边可以选择为打 ...

  10. Spring源码解析 - BeanFactory接口体系解读

    不知道为什么看着Spring的源码,感触最深的是Spring对概念的抽象,所以我就先学接口了. BeanFactory是Spring IOC实现的基础,这边定义了一系列的接口,我们通过这些接口的学习, ...

最新文章

  1. 【面向对象设计模式】 适配器模式 (二)
  2. 事务,视图 ,函数,存储过程,触发器
  3. HTTPS配置全记录
  4. php获取linux是几核的,linux下怎么查看机器cpu是几核的
  5. 如何判断链表有环、如何判断两个链表相交
  6. python生物数据分析师职业技能_数据分析师需要什么技能,数据分析行业都有什么职业?...
  7. 人眼定位python代码_使用dlib,OpenCV和Python进行人脸识别—人眼眨眼检测
  8. 一、tkinter简介
  9. SQL Unicode
  10. sqoop1.99.6 mysql_Alex的Hadoop菜鸟教程:第6课Sqoop2安装教程
  11. python matplotlib阶段性总结——word转txt、绘图、文件操作
  12. java常用框架集合
  13. RGB565的计算颜色表
  14. java jbutton 禁用_Java 如何禁用JButton在禁用时变灰?
  15. JZOJ【NOI2017模拟3.30】原谅
  16. 【中科大软院】还香不香?20软院考研四千字复盘
  17. java使用redis incr,JFinal Redis plugin 有关数值类型incr操作的bug
  18. Dockerfile文件解释
  19. 根据词云寻找对应文章的Web开发
  20. 一周技术思考(第21期)-人们说脏话的频率是衡量代码质量的唯一标准

热门文章

  1. linux ttyusb读写_linux下非root用户获得devttyUSB0的读写权限
  2. Leetcode 42.接雨水 (每日一题 20210629)
  3. 文巾解题 177. 第N高的薪水
  4. Scrapy实战篇(二)之爬取链家网成交房源数据(下)
  5. Tableau系列之与R语言结合
  6. redis中的事务、lua脚本和管道的使用场景
  7. 重新理解微服务--转
  8. Spring Security Architecture--官方
  9. c之指针与数组(1)
  10. cp: omitting directory”错误