Spring messageSource
Spring中可以使用两个类加载资源文件:
org.springframework.context.support.ReloadableResourceBundleMessageSource
和
org.springframework.context.support.ResourceBundleMessageSource
可配置如下:
ReloadableResourceBundleMessageSource:

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

ResourceBundleMessageSource:

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

Spring提供了一个接口MessageSource用于获取国际化信息,ReloadableResourceBundleMessageSource和ResourceBundleMessageSource都是继承了该接口的一个抽象实现类AbstractMessageSource,继承该抽象类的有四个类,分别是:
1、
public class StaticMessageSource extends AbstractMessageSource {
...
}
2、
public class SpringSecurityMessageSource extends ResourceBundleMessageSource {
...
}
3、
public class ReloadableResourceBundleMessageSource extends AbstractMessageSource
implements ResourceLoaderAware
{
...
}
4、
public class ResourceBundleMessageSource extends AbstractMessageSource
implements BeanClassLoaderAware
{
...
}
每个类的用处不同,StaticMessageSource主要用于测试环境,并不用于生产环境,SpringSecurityMessageSource用于Spring security的国际化信息
ApplicationContext实现了MessageSource接口,所以,ApplicationContext自身提供了国际化信息功能
例子如下:

在这里,我们测试Spring提供的国际化信息功能,文件名称为testInfo的Resource bundle中有两个文件,分别为英语,中文,国际化资源文件有一定的命名规范,只有符合命名规范的国际化资源文件才能正确的被Spring读取,国际化资源问及爱你命名规范遵循:${filename}_${languagename}_${countryname},其中${}是需要替代的内容,下划线是必需的分隔符,所以如果你想要定义一个中文国际化文件应该是这样的 testInfo_zh_CN,至于language和countryname的取名请参见java.util.Locale类,里面有详细说明。
Spring配置文件如下(app-context.xml):
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd"> <bean id="messageSource"
class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basenames">
<list>
<value>andy/resources/testInfo</value>
</list>
</property>
</bean> </beans>
这里定义了一个MessageSource的实现类ResourceBundleMessageSource用户提供国际化功能,为什么这里的id以messageSource命名呢?如果不明白可以查看AbstractApplicationContext的源代码,你会发现里面定义了一个messageSource的属性,并提供了set方法,也就是Spring在初始化时将Spring配置文件(app-context.xml)中id为messageSource的bean注入到ApplicationContext中,这样我们就可以使用ApplicationContext提供的国际化功能了,一下是测试类:
package test.andy; import java.util.Locale; import junit.framework.TestCase; import org.springframework.context.MessageSource;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class MessageSourceTest extends TestCase {
public void testResourceBundleMessageSource(){
MessageSource messageSource=new ClassPathXmlApplicationContext("app-context.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
ReloadableResourceBundleMessageSource和ResourceBundleMessageSource有着微小区别,从字面就可以看出,ReloadableResourceBundleMessageSource可以在不用重新启动服务器的情况下,读取更改后的资源文件
http://www.cnblogs.com/shanshouchen/archive/2012/08/08/2628394.html
Spring messageSource的更多相关文章
- Thymeleaf+Spring整合
前言 这个教程介绍了Thymeleaf与Spring框架的集成,特别是SpringMvc框架. 注意Thymeleaf支持同Spring框架的3.和4.版本的集成,但是这两个版本的支持是封装在thym ...
- Thymeleaf模板引擎+Spring整合使用方式的介绍
尊重原创,原文地址为:https://www.cnblogs.com/jiangchao226/p/5937458.html 前言 这个教程介绍了Thymeleaf与Spring框架的集成,特别是Sp ...
- 【sping揭秘】7、国际化信息支持
Spring提供messagesource接口,来进行国际化事务处理 Applicationcontext会优先找一个名为messageSouce的messageSource接口实现bean,如果找不 ...
- [spring源码学习]八、IOC源码-messageSource
一.代码实例 我们在第八章可以看到,spring的context在初始化的时候,会默认调用系统中的各种约定好的bean,其中第一个bean就是id为messageSource的bean,我们了解这应该 ...
- [Spring MVC] - 从数据库读取MessageSource
Spring MVC中使用MessageSource默认是写在properties文件当中,以支持国际化. 但很多时候我们需要把数据写到数据库当中,而不是在properties文件当中,以方便日常维护 ...
- Spring的国际化资源messageSource
Spring中可以使用两个类加载资源文件:ReloadableResourceBundleMessageSource和ResourceBundleMessageSource. 可配置如下message ...
- spring ----> ResourceBundle [message] not found for MessageSource: Can't find bundle for base name message, local_zh
环境: idea 2018.1.3社区版,jdk8,spring4.2.0,maven3.5.2 主题: spring国际化 出现的问题: ResourceBundle [message] not f ...
- spring中MessageSource的配置使用方法3--ResourceBundleMessageSource【转】
本文转载仅供自己学习收录,不做任何商业用途,如有需要请访问原地址:http://blog.csdn.net/qyf_5445/article/details/8124431 ApplicationCo ...
- spring中MessageSource的配置使用方法2--ReloadableResourceBundleMessageSource【转】
本文转载仅供自己学习收录,不做任何商业用途,如有需要可访问原地址:http://blog.csdn.net/qyf_5445/article/details/8124362 如何在spring mvc ...
随机推荐
- 【Android】数据库的简单应用——升级数据库
假如我们已经创建好了一个数据库,随着功能需求的增加,想在数据库中再添加一个表,如果直接在之前的代码中插入一个表,会发现创建表失败,这是因为该数据库已经存在.该如何解决呢? 1.卸载程序,重新编译安装. ...
- iOS UIKit:TableView之表格创建(1)
Table View是UITableView类的实例对象,其是使用节(section)来描述信息的一种滚动列表.但与普通的表格不同,tableView只有一行,且只能在垂直方向进行滚动.tableVi ...
- linux bash下 快捷键
c + a # 光标跳转到最左 c + e # 光标跳转到最后 c + w # 删除最后输入的单词 c + u # 删除整行 c + k # 删除光标到末尾 c + l # 清屏 c + z # 挂起 ...
- ajax 基础教程
这是一本什么书?这是一本技术类的书籍,主要从历史.XMLHttpRequest对象.怎么样于服务器交互.构建完备的Ajax开发工具箱.使用jsUnit测试javascript 代码,总之就是让我们从这 ...
- jQuery 取值、赋值的基本方法【转藏】
/*获得TEXT.AREATEXT的值*/ var textval = $("#text_id").attr("value"); //或者 var textva ...
- C#设置与获取目录权限(.net控制ACL)
找到两种方式可以修改文件夹的权限 第一种: 想用c#来设置和读取ntfs分区上的目录权限,找了很多资料,未果.终于发现了一段vb.net的代码,做了修改,以C#展示给大家. using System; ...
- (六)Struts2 国际化
所有的学习我们必须先搭建好Struts2的环境(1.导入对应的jar包,2.web.xml,3.struts.xml) 第一节:国际化简介 国际化(Internationlization),通俗地讲, ...
- CABasicAnimation添加动画离开屏幕就动画停止的问题
解决方法: animation.removedOnCompletion = NO;
- C#动态二维数组输出
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Cons ...
- Ubuntu系列Crontab日记记录
修改rsyslog文件,将/etc/rsyslog.d/50-default.conf 文件中的#cron.*前的#删掉 重启rsyslog服务service rsyslog restart 重启cr ...