博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
spring中的ResourceBundleMessageSource使用和测试示例
阅读量:4316 次
发布时间:2019-06-06

本文共 1368 字,大约阅读时间需要 4 分钟。

1 首先创建两个资源文件

   messages_en_US.properties
customer.name=Yong Mook Kim, age : {0}, URL : {1}
messages_zh_CN.properties
  customer.name=\ufeff\u6768\u6728\u91d1, age : {0}, URL : {1}
2 bean文件的设置

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
 
    <bean id="messageSource"
        class="org.springframework.context.support.ResourceBundleMessageSource">
        <property name="basename">
            <value>locale\customer\messages</value>
        </property>
    </bean>
 
</beans>
其中basename指出了资源文件的位置
3 测试

public static void main(String[] args) {

 
        ApplicationContext context
            = new ClassPathXmlApplicationContext("locale.xml");
 
        String name = context.getMessage("customer.name",
                new Object[] { 28,"http://www.eeee.com" }, Locale.US);
 
        System.out.println("Customer name (English) : " + name);
 
        String namechinese = context.getMessage("customer.name",
                new Object[] {28, "http://www.eeee.com" },
                                        Locale.SIMPLIFIED_CHINESE);
 
        System.out.println("Customer name (Chinese) : " + namechinese);
 
    }
4 另外一个ReloadableResourceBundleMessageSource,主要是用来定时资源文件,可以不重启动
    增加的属性为:  <property name="cacheSeconds" value="3000" />
   以秒为单位,如果-1表示用不刷新

转载于:https://www.cnblogs.com/sky7034/archive/2012/02/20/2359494.html

你可能感兴趣的文章
.net MVC 404错误解决方法
查看>>
linux系统目录结构
查看>>
git
查看>>
btn按钮之间事件相互调用
查看>>
Entity Framework 4.3.1 级联删除
查看>>
codevs 1163:访问艺术馆
查看>>
冲刺Noip2017模拟赛3 解题报告——五十岚芒果酱
查看>>
并查集
查看>>
sessionStorage
查看>>
代码示例_进程
查看>>
Java中关键词之this,super的使用
查看>>
学习进度
查看>>
“此人不存在”
查看>>
github.com加速节点
查看>>
解密zend-PHP凤凰源码程序
查看>>
python3 序列分片记录
查看>>
Atitit.git的存储结构and 追踪
查看>>
atitit 读书与获取知识资料的attilax的总结.docx
查看>>
B站 React教程笔记day2(3)React-Redux
查看>>
找了一个api管理工具
查看>>