---
title: "spring-MessageSource"
author: "prodream"
date: "2010-05-05T15:41:07Z"
tags: []
categories: ["企业架构"]
slug: "spring-messagesource"
draft: false
lastmod: "2010-05-05T15:41:07Z"
id: 659594
url: "https://prodream.iteye.com/blog/659594"
---

将Spring自带的org.springframework.context.support.ResourceBundleMessageSource类注册到bean.xml中，这个类的作用是获取资源文件的内容，注册到IoC的bean.xml文件中是为了自动获得此类的对象（Spring做了一些简化编程的处理）。

&lt;?xml version="1.0" encoding="UTF-8"?&gt;

&lt;!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"&gt;

&lt;beans&gt;

       &lt;bean id="Chinese" class="cn.com.chengang.spring.Chinese"/&gt;

       &lt;bean id="American" class="cn.com.chengang.spring.American"/&gt;

       &lt;bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource"&gt;

              &lt;property name="basenames"&gt;

                     &lt;list&gt;

                            &lt;value&gt;cn.com.chengang.spring.messages&lt;/value&gt;

                     &lt;/list&gt;

              &lt;/property&gt;

       &lt;/bean&gt;

&lt;/beans&gt;

代码说明：

l           id="messageSource" 的设置是不变的、必须的。

l           ResourceBundleMessageSource是Spring的一个Message类。这里还有一个选择，用ReloadableResourceBundleMessageSource类，此类可以提供不用重启即可重新加载资源文件的特性（前者对资源文件只加载一次）。对于那种有热修改资源文件的需求，后者比较合适，只是后者在效率上有可能有损耗，因为至少要多一些检查资源文件是否改变的代码（这只是我的猜测，我没有仔佃去读这段的源码）。

l           “basenames”是不变的、必须的。它是ResourceBundleMessageSource的一个属性，在源代码中的定义是“private String\[] basenames;”，可见它是一个字符串数组。

l           “cn.com.chengang.spring.messages”是把资源文件的位置传入到basenames属性中。注意：三个资源文件只需要将共同的主名（红色字体）传入：messages.properties、messages\_zh\_CN.properties、messages\_zh\_TW.properties。

 

3、使用。修改MessageTest类，如下

package cn.com.chengang.spring;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.FileSystemXmlApplicationContext;

public class MessageTest {

    public static void main(String\[] args) {

        ApplicationContext ctx = new FileSystemXmlApplicationContext("bean.xml");

        String str = ctx.getMessage("chengang", null, null);

        System.out.println(str);

    }

}

代码说明：

（1）main方法里

l           第一句取得bean.xml文件的配置信息。

l           第二句从资源文件里得到键值chengang对应的字符串。

l           第三句将字符串打印出来，结果是打印的是“陈刚”，说明读取的是messages\_zh\_CN.properties资源文件。

（2）ctx.getMessage("chengang", null, null);有三个参数：

l           第一个是资源文件的键值；

l           第二个是资源文件字符串的参数，由于本字符串没有参数，所以用一个null（后面给出了一个用到字符串参数的实例）；

l           第三个是一个java.util. Locale类型的参数。参数为null，则表示根据使用者的语言环境来选择Locale，因为我用的是中文版的windows，所以在取字符串时它自动选择了messages\_zh\_CN.properties资源文件。  
    这其中还有一个控制点在JVM，JVM会根据当前操作系统的语言环境进行相应处理，我们可以通过在JVM启动参数中追加“-Duser.language=zh\_TW”来设定当前JVM语言类型，通过JVM级的设定，也可以实现自动切换所使用的资源文件类型。  
    所以这里面的控制语言的方式有三种：从最低层的操作系统的Locale设定，到更上一层的JVM的Locale设定，再到程序一级的Locale设定。

### 3.3  资源文件的其他使用方式：

package cn.com.chengang.spring;

import java.util.Locale;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.FileSystemXmlApplicationContext;

import org.springframework.context.support.ResourceBundleMessageSource;

public class MessageTest {

    public static void main(String\[] args) {

        ApplicationContext ctx = new FileSystemXmlApplicationContext("bean.xml");

        String str = ctx.getMessage("chengang", null, null);

        System.out.println(str); //输出“陈刚”

        /*

         * 使用了messages.properties

         \*/

        str = ctx.getMessage("chengang", null, new Locale(""));

        System.out.println(str);//输出“Giles”

        /*

         * 使用了messages\_zh\_CN.properties

         \*/

        str = ctx.getMessage("chengang", null, new Locale("zh", "CN"));

        System.out.println(str);//输出“陈刚”

        /*

         * 使用了messages\_zh\_TW.properties

         \*/

        str = ctx.getMessage("chengang", null, new Locale("zh", "TW"));

        System.out.println(str);//输出“陳剛”

        /*

         * 使用了messages\_zh\_TW.properties，从这里可见资源文件的起名可以很随意，

         * 比如我们建立一个messages\_123.properties，在传参数时候就可以这样：

         * new Locale("123")，一样也可以取出messages\_123.properties中的值

         \*/

        str = ctx.getMessage("chengang", null, new Locale("zh\_TW"));

        System.out.println(str);//输出“陳剛”

        /*

         * 当找不到相应的资源文件时，使用了messages\_zh\_CN.properties

         \*/

        str = ctx.getMessage("chengang", null, new Locale("abcd"));

        System.out.println(str);//输出“陈刚”

         /\**

         * 不通过IoC注册，直接使用ResourceBundleMessageSource类的写法。

         \*/

        ResourceBundleMessageSource s = new ResourceBundleMessageSource();

        s.setBasename("cn.com.chengang.spring.messages");

        str = s.getMessage("chengang", null, null);

        System.out.println(str);//输出“陈刚”

    }

}

代码说明：

前面说过控制语言的方式有三种：从最低层的操作系统的Locale设定，到更上一层的JVM的Locale设定，再到程序一级的Locale设定。我认为最佳的方法是在程序一级进行控制：定义一个统一的Locale静态变量，然后整个系统中只使用这一个变量，以后就可以通过界面操作设置此Locale变量的值，让用户来选择他所需的软件语言。而且我们也可以将此静态变量设成null值，来自动选择资源文件。

另外，Locale里也定义了一些常量，我们可以直接使用而不必去new一个Locale对象，如：“Locale.ENGLISH”。

---摘自[http://xp84012493.iteye.com/blog/199627](http://xp84012493.iteye.com/blog/199627)