---
title: "Spring 的国际化"
author: "wangyun"
date: "2008-06-26T11:31:21Z"
tags: []
slug: "spring"
draft: false
lastmod: "2008-06-26T11:31:21Z"
id: 208169
url: "https://wangyun.iteye.com/blog/208169"
---

Spring 通过ApplicationContext 容器的MessageSource 接口实现Sping 对国际化的支持。

      Spring 已经已经提供提供了一个现成的MessageSource 的实现类ResourceBundleMessageSource。因此只需要在Spring 的配置文件中进行简单的配置即可，如下：

```
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
        <property name="basename" value="application" />
    </bean>
```

 

注意，这里的Bean id 必须是“messageSource”，因为Spring 在装配系统Bean 时会根据这个名字进行查找。

       这样，我们便可以在程序中通过ApplicationContext 对象调用getMessage（）方法获取资源文件的信息。

 

 

ResourceBundleMessageSource有两个getMessage方法：

1）getMessage（String code，Object\[] args，Locale locale）

2）getMessage（String code，Object\[] args，String defaultMessage，Locale locale）