Spring中可以使用两个类加载资源文件:ReloadableResourceBundleMessageSource和ResourceBundleMessageSource。

可配置如下messageSource这个bean id不能变:

    @Bean
public MessageSource messageSource() {
ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
messageSource.setBasename("classpath:messages");
messageSource.setCacheSeconds(10); //reload messages every 10 seconds
messageSource.setDefaultEncoding("UTF-8");
messageSource.setUseCodeAsDefaultMessage(true);
return messageSource;
}

或者xml

<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
  <property name="basenames">
    <list>
      <value>resources/messages</value>
    </list>
  </property>
</bean>

Spring提供了一个接口MessageSource用于获取国际化信息,ReloadableResourceBundleMessageSource和ResourceBundleMessageSource都是继承了该接口的一个抽象实现类AbstractMessageSource,继承该抽象类的有四个类,分别是:

StaticMessageSource  主要用于测试环境,并不用于生产环境
SpringSecurityMessageSource  用于Spring security的国际化信息
ReloadableResourceBundleMessageSource  可以在不用重新启动服务器的情况下,读取更改后的资源文件
ResourceBundleMessageSource  用于生产环境

用法示例:

public class MessageSourceTest extends TestCase {
public void testResourceBundleMessageSource(){
MessageSource messageSource=new ClassPathXmlApplicationContext("applicationcontext.xml");
String username_us=messageSource.getMessage("userName_lable",new Object[1],Locale.US);
String username_chinese=messageSource.getMessage("userName_lable",new Object[0],Locale.CHINESE);
System.out.println("chinese:"+username_chinese);
System.out.println("english:"+username_us);
}
}

chinese:用户名
english:userName

Spring的国际化资源messageSource的更多相关文章

  1. spring boot 国际化MessageSource

    转自:https://blog.csdn.net/flowingflying/article/details/76358970 spring中ResourceBundleMessageSource的配 ...

  2. Spring boot 国际化自动加载资源文件问题

    Spring boot 国际化自动加载资源文件问题 最近在做基于Spring boot配置的项目.中间遇到一个国际化资源加载的问题,正常来说只要在application.properties文件中定义 ...

  3. spring mvc 国际化

    spring mvc 国际化 使用CookieLocaleResolver实现国际化的步骤:1.注册 messageSource,localeResolver 两个bean2.调用localeReso ...

  4. Spring Boot 国际化及点击链接跳转国家语言

    一.国际化 在SpringBoot中已经自动帮我们配置管理国际化资源的组件,所以我们只需要编写代码就可. @Bean @ConfigurationProperties(prefix = "s ...

  5. springmvc国际化资源文件

    spring配置文件中添加 <!-- 配置国际化资源文件 --> <bean id="messageSource" class="org.springf ...

  6. 58. Spring Boot国际化(i18n)【从零开始学Spring Boot】

    国际化(internationalization)是设计和制造容易适应不同区域要求的产品的一种方式.它要求从产品中抽离所有地域语言,国家/地区和文化相关的元素.换言之,应用程序的功能和代码设计考虑在不 ...

  7. Spring MVC国际化配置

    Spring MVC国际化配置 前言 项目开发中要考虑支持国际化,框架选用的是Spring MVC框架,那么问题来了Spring MVC如何配置并实现国际化. 实现过程(Maven项目) 对于Spri ...

  8. spring boot-10.国际化

    1.在原来spring MVC 中国际化实现步骤 (1)编写国际化配置文件 (2)使用ResourceBundleMessageSource管理国际化资源文件 (3)在页面中取国际化信息 2.spri ...

  9. Spring MVC(十六)--Spring MVC国际化实例

    上一篇文章总结了一下Spring MVC中实现国际化所需的配置,本文继上一文举一个完整的例子,我选择用XML的方式.我的场景是这样的: 访问一个页面时,这个页面有个表格,对表头中的列名实现国际化. 第 ...

随机推荐

  1. javascript trigger触发事件

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  2. PHP error_log() 函数

    定义和用法 error_log() 函数向服务器错误记录.文件或远程目标发送一个错误. 若成功,返回 true,否则返回 false. 语法 error_log(error,type,destinat ...

  3. C语言内存对齐详解(3)

    接上一篇:C语言内存对齐详解(2) 在minix的stdarg.h文件中,定义了如下一个宏: /* Amount of space required in an argument list for a ...

  4. HDU 1018 Big Number (数学题)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1018 解题报告:输入一个n,求n!有多少位. 首先任意一个数 x 的位数 = (int)log10(x ...

  5. NSArray和NSMutableArray

    //1. NSArray EOItems *eOItems = [[EOItems alloc] init]; eOItems.ID = [NSNumber numberWithInt:]; NSAr ...

  6. windows 程序的本体与操作系统之间的关系

    WinMain(hInst,hPrev,……) { MSG msg; RegisterClass(……); CreateWindow(……); ShowWindow(……); UpdateWindow ...

  7. Mysql函数集合

    Mysql提供了很多函数 提供的常用函数集合 一.数学函数 ABS(x) 返回x的绝对值 BIN(x) 返回x的二进制(OCT返回八进制,HEX返回十六进制) CEILING(x) 返回大于x的最小整 ...

  8. 【GoLang】GO语言系列--001.GO开发环境搭建

  9. putty mtputty 设置utf8编码

    2013年10月30日 10:02:36 先load你指定的ip 然后选择左侧目录中的windows->translation 再在右侧选择utf-8编码 选中后,点击左侧目录中的session ...

  10. 用RPM包安装MySQL的默认安装路径问题

    在安装PHP时候要对一些配置选项进行设置,其中就有:--with-mysql[=DIR]:包含MySQL扩展,[=DIR]指定mysql安装目录,省略[=DIR]则为默认位置/usr--with-my ...