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

    Window XP + Visual Studio 2008 获取Boost库源码 我们可以从http://www.boost.org/ 上获取boost的源代码.当前最新版本为1.45.0. 解压到 ...

  2. Android开发之ViewPager实现轮播图(轮播广告)效果的自定义View

    最近开发中需要做一个类似京东首页那样的广告轮播效果,于是采用ViewPager自己自定义了一个轮播图效果的View. 主要原理就是利用定时任务器定时切换ViewPager的页面. 效果图如下: 主页面 ...

  3. iOS 关于开发者证书:此证书的签发者无效的解决方案

    备注:第二个步骤一定要进行,否则弄到吐血,还是现实签发者无效 ---------------------- 1,按照你那个链接下载,https://developer.apple.com/certif ...

  4. CSS背景颜色、背景图片、平铺、定位、固定

    CSS背景颜色设置 background-color:red;如设置背景颜色为红色: 背景颜色设置支持3种写法: 颜色名 16进制 rgb CSS背景图片颜色设置 background-image:u ...

  5. radio的change事件

    radio的change事件 <scripttype="text/javascript"> $(document).ready(function(){ $(" ...

  6. C#入门教程(二)–C#常用快捷键、变量、类型转换-打造C#学习教程

    C#入门教程(一)–.Net平台技术介绍.C#语言及开发工具介绍-打造C#学习教程 上次教程主要介绍了.Net平台以及C#语言的相关介绍.以及经典程序案例,helloworld程序. 初来乍到,第一次 ...

  7. Entity Framework性能优化

    AsNonUnicode 执行如下语句,并用SqlProfiler监控其SQL: var list = WMFactory.ReChargeMobile.Queryable().Where(w =&g ...

  8. 解读zookeeper的配置项

    zookeeper的默认配置文件为zookeeper/conf/zoo_sample.cfg,需要将其修改为zoo.cfg.其中各配置项的含义,解释如下: 1.tickTime:CS通信心跳数 Zoo ...

  9. python基础知识八

    当你的程序中出现某些 异常的 状况的时候,异常就发生了.例如,当你想要读某个文件的时候,而那个文件不存在.或者在程序运行的时候,你不小心把它删除了.上述这些情况可以使用异常来处理. 如你的程序中有一些 ...

  10. 设置appicon和启动图

    设置appicon General – App Icon Source 设置AppIcon 图片大小 iphone 6P(@3x) 180x180 iphone 6(@2x) iphone5/5s ( ...