Spring的国际化资源messageSource
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的更多相关文章
- spring boot 国际化MessageSource
转自:https://blog.csdn.net/flowingflying/article/details/76358970 spring中ResourceBundleMessageSource的配 ...
- Spring boot 国际化自动加载资源文件问题
Spring boot 国际化自动加载资源文件问题 最近在做基于Spring boot配置的项目.中间遇到一个国际化资源加载的问题,正常来说只要在application.properties文件中定义 ...
- spring mvc 国际化
spring mvc 国际化 使用CookieLocaleResolver实现国际化的步骤:1.注册 messageSource,localeResolver 两个bean2.调用localeReso ...
- Spring Boot 国际化及点击链接跳转国家语言
一.国际化 在SpringBoot中已经自动帮我们配置管理国际化资源的组件,所以我们只需要编写代码就可. @Bean @ConfigurationProperties(prefix = "s ...
- springmvc国际化资源文件
spring配置文件中添加 <!-- 配置国际化资源文件 --> <bean id="messageSource" class="org.springf ...
- 58. Spring Boot国际化(i18n)【从零开始学Spring Boot】
国际化(internationalization)是设计和制造容易适应不同区域要求的产品的一种方式.它要求从产品中抽离所有地域语言,国家/地区和文化相关的元素.换言之,应用程序的功能和代码设计考虑在不 ...
- Spring MVC国际化配置
Spring MVC国际化配置 前言 项目开发中要考虑支持国际化,框架选用的是Spring MVC框架,那么问题来了Spring MVC如何配置并实现国际化. 实现过程(Maven项目) 对于Spri ...
- spring boot-10.国际化
1.在原来spring MVC 中国际化实现步骤 (1)编写国际化配置文件 (2)使用ResourceBundleMessageSource管理国际化资源文件 (3)在页面中取国际化信息 2.spri ...
- Spring MVC(十六)--Spring MVC国际化实例
上一篇文章总结了一下Spring MVC中实现国际化所需的配置,本文继上一文举一个完整的例子,我选择用XML的方式.我的场景是这样的: 访问一个页面时,这个页面有个表格,对表头中的列名实现国际化. 第 ...
随机推荐
- iOS上传应用过程中出现的错误"images contain alpha channels or transparencies"以及解决方案
如何取消图片透明度 本文永久地址为 http://www.cnblogs.com/ChenYilong/p/3989954.html,转载请注明出处. 当你试图通过<预览>进行" ...
- 一个1年前的T-SQL问题
还记得年前的一个SQL问题,当时对SQL刚接触,因此绕开了它.用了别的办法.昨天看SQL突然想起了这个问题.百思不得其解,然后去SQL Server技术交流群,也请教了,大神高文佳,何志勇提示我因为先 ...
- java笔记--关于int和byte[]的转换
关于int和byte[]数组的转换 --如果朋友您想转载本文章请注明转载地址"http://www.cnblogs.com/XHJT/p/3891747.html "谢谢-- 众所 ...
- 第18章 使用MariaDB数据库管理系统
章节概述: MYSQL数据库管理系统被Oracle公司收购后从开源换向到了封闭,导致包括红帽在内的许多Linux发行版选择了MariaDB. 本章节将教会您使用mariaDB数据库管理工具来管理数据库 ...
- Linux MySQL差异备份技巧
MSSQL差异备份使用技巧 15 Apr 2013 所谓的差异备份,就是只备份最近一次备份之后到此次备份之前所增加的那一部分数据.打个比方我第N次备份后数据库存放的内容是ABCD,然后我第N+1次 备 ...
- BZOJ 2818
2818:GCD Description 给定整数$N$,求$1\le x,y\le N$且$\gcd{x,y}$为素数的数对$(x,y)$有多少对. Input $N$ Output RT Samp ...
- PHPNG (next generation)
PHPNG (next generation) This page gives short information about development state of a new PHP branc ...
- JQuery $.ajax()方法详解
jQuery Ajax 参考手册 实例 通过 AJAX 加载一段文本: jQuery 代码: $(document).ready(function(){ $("#b01").cli ...
- 全面解释StringBuilder、StringBuffer和String的关系
1. String 类 String的值是不可变的,这就导致每次对String的操作都会生成新的String对象,不仅效率低下,而且大量浪费有限的内存空间. String a = " ...
- 面向侧面的程序设计AOP-------《三》.Net平台AOP技术概览
本文转载自张逸:晴窗笔记 .Net平台与Java平台相比,由于它至今在服务端仍不具备与unix系统的兼容性,也不具备类似于Java平台下J2EE这样的企业级容器,使得.Net平台在大型的企业级应用上, ...