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的方式.我的场景是这样的: 访问一个页面时,这个页面有个表格,对表头中的列名实现国际化. 第 ...
随机推荐
- (JS高手不用看了!我只是在碎碎念,因为我也不知道面什么)JavaScript的算术运算
Math.pow(2,53) //2的51次幂 Math.round(0.6) //四舍五入 Math.cell(0.6) //向上求整 Math.floor(0.6) / ...
- Windows主机里利用VMware安装Linux(CentOS)虚拟机,Host-only连接上网方式详解
关于Host-only指的是主机与虚拟机之间的互联,因此虚拟机是不能连网的,若需要连网则需要使用NAT模式: Host-only模式实现联网得考虑如下配置过程: 附:VMware虚拟机三种网络模式(B ...
- 在iOS7中修改状态栏字体的颜色
http://www.2cto.com/kf/201408/324442.html 默认状态栏的字体为黑色:UIStatusBarStyleDefault 状态栏的字体为白色:UIStatusBarS ...
- 关于seajs
(这些文章都是从我的个人主页上粘贴过来的,大家也可以访问我的主页 www.iwangzheng.com) 最近经常听到各种JS前缀的名称,瞬间感觉自己弱爆了,啥都没用过呢,这么下去将来怎么嫁人呢. ...
- Linux查看和结束进程命令详解
在ubuntu中,终止一个进程或终止一个正在运行的程序,一般是通过 kill .killall.pkill.xkill 等进行. ----------------------------------- ...
- python 最佳入门实践
勿在浮沙筑高台,无论什么技术,掌握核心精神和api,是很重要的. 但是入门过程也可能不是一帆风顺的,这里有八个入门任务,看看你完成了没有: http://code.tutsplus.com/artic ...
- Nginx和PHP-FPM的启动/重启脚本 [转发]
Nginx和PHP-FPM的启动/重启脚本 [转发] (2012-07-27 16:07:52) 标签: it 分类: 学习 转载自:http://blog.sina.com.cn/s/blog_53 ...
- 【云计算】开源的Docker Registry WebUI
kwk/docker-registry-frontend Code Issues 9 Pull requests 6 Wiki ...
- 【转】js onclick用法:跳转到指定URL
使用onclick跳转到其他页面/跳转到指定url ☆如果是本页显示可以直接用location,方法如下: ①onclick="javascript:window.location.hr ...
- Android 中SimpleDateFormat的使用注意
以前在程序中总这样使用 SimpleDateFormat sdf=new SimpleDateFormat("hh:mma"); date1=sdf.parse(alertT ...