Spring messageSource

简介: Spring中可以使用两个类加载资源文件: org.springframework.context.support.ReloadableResourceBundleMessageSource 和 org.

 

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

 

相关文章
|
XML 存储 缓存
从MessageSource源码出发实战spring·i18n国际化的三种改造方案
从源码去看MessageSource的几个实现类的源码出发,基于spring的国际化支持,实现国际化的开箱即用,静态文件配置刷新生效以及全局异常国际化处理。
546 0
从MessageSource源码出发实战spring·i18n国际化的三种改造方案
|
XML Java 数据格式
|
Java Spring 容器
Spring 国际化支持之 MessageSource
背景 为了友好的支持各个国家的语言,Java 本身已经提供了对国际化的支持,上篇文章《Java 国际化与文本格式化》已经介绍了 Java 对国际化的支持。
274 0
|
自然语言处理 Java 开发者
Spring 源码阅读 15:初始化 MessageSource
本文分析 Spring 上下文初始化过程中, MessageSource 初始化的过程。MessageSource 是 Spring 框架中,处理 i18n 的组件。
247 0
Spring 源码阅读 15:初始化 MessageSource
|
缓存 Java 测试技术
Spring(21)——国际化MessageSource
21 国际化MessageSource Spring中定义了一个MessageSource接口,以用于支持信息的国际化和包含参数的信息的替换。MessageSource接口的定义如下,对应的方法说明已经在方法上注释了。
1134 0
|
Java Spring 安全
Spring的国际化资源messageSource
Spring中可以使用两个类加载资源文件:ReloadableResourceBundleMessageSource和ResourceBundleMessageSource。 可配置如下messageSource这个bean id不能变: @Bean public MessageS...
1322 0
|
3天前
|
Java 应用服务中间件 Maven
SpringBoot 项目瘦身指南
SpringBoot 项目瘦身指南
59 0
|
3天前
|
缓存 安全 Java
Spring Boot 面试题及答案整理,最新面试题
Spring Boot 面试题及答案整理,最新面试题
142 0
|
3天前
|
存储 JSON Java
SpringBoot集成AOP实现每个接口请求参数和返回参数并记录每个接口请求时间
SpringBoot集成AOP实现每个接口请求参数和返回参数并记录每个接口请求时间
49 2
|
3天前
|
前端开发 搜索推荐 Java
【Spring底层原理高级进阶】基于Spring Boot和Spring WebFlux的实时推荐系统的核心:响应式编程与 WebFlux 的颠覆性变革
【Spring底层原理高级进阶】基于Spring Boot和Spring WebFlux的实时推荐系统的核心:响应式编程与 WebFlux 的颠覆性变革