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. Java基础知识强化之网络编程笔记02:Socket通信原理图解

    1. Socket (1)Socket套接字  网络上具有唯一标识的IP地址和端口号组合在一起才能构成唯一能识别的标识符套接字 (2)Socket原理机制:  • 通信两端都有Socket.  • 网 ...

  2. minicom移植到ARM开发平台

    minicom需要ncurses库的支持.arm-linux-gcc中并没有此库故需要交叉编译ncurses,否则出现很多头文件.库函数找不到. 软件环境: ncurses-6.0 下载网址:http ...

  3. 用bootstrap的tab插件做一个图层切换效果(感觉会误导淫们,大家当乐子看吧)

    小伙伴们啊,我JS真的是个渣渣,所以总想要偷懒,于是为了实现效果就把tab插件给改了(各位大神轻拍啊,我是小白,很纯洁滴,小心脏也很脆弱)…… 最近做的项目为了考虑以后的移动设备兼容性,所以用了Boo ...

  4. 【转】[转]order by 1是什么意思?

    [转][转]order by 1是什么意思? ORDER BY 1 表示 所select 的字段按第一个字段排序 ORDER BY ASC应该没有这样写法,ORDER BY 后面不是字段就是数字, 可 ...

  5. 黑马程序员-集合(二)contains()方法的内部探索

    ------Java培训.Android培训.iOS培训..Net培训.期待与您交流! ------- 我们知道集合是用来存储对象的.在他们实现了众多的接口我们以Arraylist为列子 所有已实现的 ...

  6. jquery ajax跨域取数据

    jsonp.js/html 主要是利用jquery ajax和jsonp的datatype 跨站点请求数据,记录~ 同源策略:同端口,同协议,同域:所以ajax不能支持跨域取得数据,解决方案一般是js ...

  7. Java 去除utf-8类型的空格的方法

    问题产生 最近遇到一个这样的问题,在生成的报文中,某个字段信息后面有一个空格,在代码中trim()下,它仍然存在.到底什么原因呢? 问题的根源 经过多番查证,是由于utf-8中的特俗字符造成的. 问题 ...

  8. 4.SQL语言基础

    4.1语言分类和用户模式 4.1.1语言分类 1)数据查询语言 用语检索数据库中的数据,主要是select语句,是操作数据库时最为频繁使用. 2)数据操纵语言 用语改变数据库中的数据,主要包括inse ...

  9. oraclesql日志

    select * from v$logfile;  select * from v$sql select sql_text,module,action,parsing_schema_name,firs ...

  10. 检查.gitignore语法

    每次配置git的时候,要写gitignore文件,你知道你的.gitignore匹配那些文件吗? 看看.gitignore放过了哪些文件: git ls-files -ocm --exclude-st ...