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的更多相关文章

  1. Thymeleaf+Spring整合

    前言 这个教程介绍了Thymeleaf与Spring框架的集成,特别是SpringMvc框架. 注意Thymeleaf支持同Spring框架的3.和4.版本的集成,但是这两个版本的支持是封装在thym ...

  2. Thymeleaf模板引擎+Spring整合使用方式的介绍

    尊重原创,原文地址为:https://www.cnblogs.com/jiangchao226/p/5937458.html 前言 这个教程介绍了Thymeleaf与Spring框架的集成,特别是Sp ...

  3. 【sping揭秘】7、国际化信息支持

    Spring提供messagesource接口,来进行国际化事务处理 Applicationcontext会优先找一个名为messageSouce的messageSource接口实现bean,如果找不 ...

  4. [spring源码学习]八、IOC源码-messageSource

    一.代码实例 我们在第八章可以看到,spring的context在初始化的时候,会默认调用系统中的各种约定好的bean,其中第一个bean就是id为messageSource的bean,我们了解这应该 ...

  5. [Spring MVC] - 从数据库读取MessageSource

    Spring MVC中使用MessageSource默认是写在properties文件当中,以支持国际化. 但很多时候我们需要把数据写到数据库当中,而不是在properties文件当中,以方便日常维护 ...

  6. Spring的国际化资源messageSource

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

  7. 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 ...

  8. spring中MessageSource的配置使用方法3--ResourceBundleMessageSource【转】

    本文转载仅供自己学习收录,不做任何商业用途,如有需要请访问原地址:http://blog.csdn.net/qyf_5445/article/details/8124431 ApplicationCo ...

  9. spring中MessageSource的配置使用方法2--ReloadableResourceBundleMessageSource【转】

    本文转载仅供自己学习收录,不做任何商业用途,如有需要可访问原地址:http://blog.csdn.net/qyf_5445/article/details/8124362 如何在spring mvc ...

随机推荐

  1. 基于bootstrap面板的类别多选栏

    1.html部分 <div class="panel panel-default"> <div class="panel-heading"&g ...

  2. JavaScript 应用开发 #5:为完成的任务添加样式

    判断一下任务的状态,如果是完成的任务,可以在任务项目的上面,添加一个额外的 css 类,在这个 css 类里,可以去定义完成的任务的样式.比如,把文字的颜色变成浅友色,并且在文字上面添加一条删除线.这 ...

  3. Linux下安装、配置、授权、调优Mysql

    以前在linux已经安装了很多次的Mysql,但是时间间隔长了以后,安装步骤总是会遗漏,趁这次安装,做一下安装记录. 检查系统是否已经安装Mysql rpm -qa|grep -i mysql Mys ...

  4. JDK自带方法实现RSA数字签名

    JDK 6只支持MD2withRSA, MD5withRSA, SHA1withRSA 其他的如SHA512withRSA需要第三方包支持,如BC(bouncy castle) --20151126 ...

  5. H TML5 之 (3)转动的圆球

    HTML5 练手之二,一个能够为之圆心转动的圆球,原理和时钟的非常像,只是要把握转动的时间控制,同时加入了点渐变色 HTML5 练手之二,一个能够为之圆心转动的圆球,原理和时钟的非常像,只是要把握转动 ...

  6. (转)C#中Trim()、TrimStart()、TrimEnd()的用法 .

    C#中Trim().TrimStart().TrimEnd()的用法: 这三个方法用于删除字符串头尾出现的某些字符.Trim()删除字符串头部及尾部出现的空格,删除的过程为从外到内,直到碰到一个非空格 ...

  7. 监听EditText的变化

    http://liangruijun.blog.51cto.com/3061169/729505 之前博客上的有关EditText的文章,只是介绍EditText的一些最基本的用法,这次来深入学习一下 ...

  8. MongoDB的Document操作

    简介 一.Document数据插入 二.Document数据删除 三.Document数据更新 一.Document数据插入 1.插入文档 db.[文档名].insert({BSON数据}) 2.批量 ...

  9. SQL大量数据查询的优化 及 非用like不可时的处理方案

    1.对查询进行优化,应尽量避免全表扫描,首先应考虑在 where 及 order by 涉及的列上建立索引. 2.应尽量避免在 where 子句中对字段进行 null 值判断,否则将导致引擎放弃使用索 ...

  10. (五)Struts2 标签

    所有的学习我们必须先搭建好Struts2的环境(1.导入对应的jar包,2.web.xml,3.struts.xml) 第一节:Struts2 标签简介 Struts2 自己封装了一套标签,比JSTL ...