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

时间: 2024-08-05 05:35:24

Spring messageSource的相关文章

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

尊重原创,原文地址为:https://www.cnblogs.com/jiangchao226/p/5937458.html 前言 这个教程介绍了Thymeleaf与Spring框架的集成,特别是SpringMvc框架. 注意Thymeleaf支持同Spring框架的3.和4.版本的集成,但是这两个版本的支持是封装在thymeleaf-spring3和thymeleaf-spring4这两个独立的库中,项目中需要根据实际情况分别引用. 样例代码针对的是spring4.,但一般情况下,spring

错误代码以及错误消息提示-如何更好地管理与维护消息资源

错误代码以及错误消息是开发过程中不可避免会碰到的问题,虽然对于开发技巧并没有多高的要求,但是清晰地管理好这些资源对于整个项目或者产品的开发都是百益而无一害的.    1)明晰代码模块以及更好地梳理代码流程    2)便于PM和前方客户沟通    3)方便校验和国际化处理 从开发的角度,如何更好地管理好这些错误资源文件呢? 结合平常实际开发经验,总结出从几个方面实现明晰的错误消息管理.   一.错误消息规则 错误消息是一组由JSON格式表示的字符串,例如:{"error_code":&q

spring中MessageSource的配置使用方法1[转]

本文转载仅供自己学习收录,不做任何商业用途,如有需要请访问文章原地址:http://blog.csdn.net/qyf_5445/article/details/8124306 Spring定义了访问国际化信息的MessageSource接口,并提供了几个易用的实现类.首先来了解一下该接口的几个重要方法:?  String getMessage(String code, Object[] args, String defaultMessage, Locale locale) code表示国际化资

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

本文转载仅供自己学习收录,不做任何商业用途,如有需要请访问原地址:http://blog.csdn.net/qyf_5445/article/details/8124431 ApplicationContext接口扩展了MessageSource接口,因而提供了消息处理的功能(i18n或者国际化).与HierarchicalMessageSource一起使用,它还能够处理嵌套的消息,这些是spring提供的处理消息的基本接口.让我们快速浏览一下它所定义的方法: String getMessage

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

本文转载仅供自己学习收录,不做任何商业用途,如有需要可访问原地址:http://blog.csdn.net/qyf_5445/article/details/8124362 如何在spring mvc框架中实现MessageSource来管理国际资源文件呢 如下: 1.在applicationContext.xml文件内配置如下 [java] view plain copy <span style="font-size:14px;"><bean id="me

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

一.代码实例 我们在第八章可以看到,spring的context在初始化的时候,会默认调用系统中的各种约定好的bean,其中第一个bean就是id为messageSource的bean,我们了解这应该是一个读取properties的,并支持国际化的bean 1.首先我们定义这个bean,spring中默认提供了一些类,查了下主要是ResourceBundleMessageSource和ReloadableResourceBundleMessageSource,我们这里采用ResourceBund

spring中MessageSource的配置使用方法3--ResourceBundleMessageSource

ApplicationContext接口扩展了MessageSource接口,因而提供了消息处理的功能(i18n或者国际化).与HierarchicalMessageSource一起使用,它还能够处理嵌套的消息,这些是Spring提供的处理消息的基本接口.让我们快速浏览一下它所定义的方法: String getMessage(String code, Object[] args, String default, Locale loc):用来从MessageSource获取消息的基本方法.如果在指

spring中MessageSource的配置使用方法2--ReloadableResourceBundleMessageSource

如何在spring mvc框架中实现MessageSource来管理国际资源文件呢 如下: 1.在applicationContext.xml文件内配置如下 [java] view plaincopy <span style="font-size:14px;"><bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBund

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

Spring MVC中使用MessageSource默认是写在properties文件当中,以支持国际化. 但很多时候我们需要把数据写到数据库当中,而不是在properties文件当中,以方便日常维护. 1.先看Spring配置 <!-- 默认的注解映射的支持 --> <mvc:annotation-driven validator="validator" conversion-service="conversionService" /> &