您的位置:首页 > 编程语言 > Java开发

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

2015-05-27 17:27 861 查看
ApplicationContext
接口扩展了
MessageSource
接口,因而提供了消息处理的功能(i18n或者国际化)。与
HierarchicalMessageSource
一起使用,它还能够处理嵌套的消息,这些是Spring提供的处理消息的基本接口。让我们快速浏览一下它所定义的方法:

String getMessage(String code, Object[] args, String default, Locale loc):用来从
MessageSource
获取消息的基本方法。如果在指定的locale中没有找到消息,则使用默认的消息。args中的参数将使用标准类库中的[code]MessageFormat
来作消息中替换值。[/code]

String getMessage(String code, Object[] args, Locale loc):本质上和上一个方法相同,其区别在:没有指定默认值,如果没找到消息,会抛出一个
NoSuchMessageException
异常。


String getMessage(MessageSourceResolvable resolvable, Locale locale)
:上面方法中所使用的属性都封装到一个
MessageSourceResolvable
实现中,而本方法可以指定
MessageSourceResolvable
实现。

当一个
ApplicationContext
被加载时,它会自动在context中查找已定义为
MessageSource
类型的bean。此bean的名称须为
messageSource
。如果找到,那么所有对上述方法的调用将被委托给该bean。否则
ApplicationContext
会在其父类中查找是否含有同名的bean。如果有,就把它作为
MessageSource
。如果它最终没有找到任何的消息源,一个空的
StaticMessageSource
将会被实例化,使它能够接受上述方法的调用。

Spring目前提供了两个
MessageSource
的实现:
ResourceBundleMessageSource
StaticMessageSource
。它们都继承
NestingMessageSource
以便能够处理嵌套的消息。
StaticMessageSource
很少被使用,但能以编程的方式向消息源添加消息。
ResourceBundleMessageSource
会用得更多一些,为此提供了一下示例:

<beans>
<bean id="messageSource"
class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basenames">
<list>
<value>format</value>
<value>exceptions</value>
<value>windows</value>
</list>
</property>
</bean>
</beans>

这段配置假定在你的classpath中有三个资源文件(resource bundle),它们是
format
exceptions
windows
。通过ResourceBundle,使用JDK中解析消息的标准方式,来处理任何解析消息的请求。出于示例的目的,假定上面的两个资源文件的内容为…

# in 'format.properties'
message=Alligators rock!

# in 'exceptions.properties'
argument.required=The '{0}' argument is required.

下面是测试代码。因为
ApplicationContext
实现也都实现了
MessageSource
接口,所以能被转型为
MessageSource
接口

public static void main(String[] args) {
MessageSource resources = new ClassPathXmlApplicationContext("beans.xml");
String message = resources.getMessage("message", null, "Default", null);
System.out.println(message);
}

上述程序的输出结果将会是...

Alligators rock!

总而言之,我们在
'beans.xml'
的文件中(在classpath根目录下)定义了一个
messageSource
bean,通过它的
basenames
属性引用多个资源文件;而
basenames
属性值由list元素所指定的三个值传入,它们以文件的形式存在并被放置在classpath的根目录下(分别为
format.properties
exceptions.properties
windows.properties
)。

再分析个例子,这次我们将着眼于传递参数给查找的消息,这些参数将被转换为字符串并插入到已查找到的消息中的占位符(译注:资源文件中花括号里的数字即为占位符)。

<beans>

<!-- this 
MessageSource
is being used in a web application -->
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="baseName" value="WEB-INF/test-messages"/>
</bean>

<!-- let's inject the above [code]MessageSource
into this POJO -->
<bean id="example" class="com.foo.Example">
<property name="messages" ref="messageSource"/>
</bean>

</beans>[/code]
public class Example {

private MessageSource messages;

public void setMessages(MessageSource messages) {
this.messages = messages;
}

public void execute() {
String message = this.messages.getMessage("argument.required",
new Object [] {"userDao"}, "Required", null);
System.out.println(message);
}

}

调用
execute()
方法的输出结果是...

The 'userDao' argument is required.

对于国际化(i18n),Spring中不同的
MessageResource
实现与JDK标准ResourceBundle中的locale解析规则一样。比如在上面例子中定义的
messageSource
bean,如果你想解析British (en-GB) locale的消息,那么需要创建
format_en_GB.properties
exceptions_en_GB.properties
windows_en_GB.properties
三个资源文件。

Locale解析通常由应用程序根据运行环境来指定。出于示例的目的,我们对将要处理的(British)消息手工指定locale参数值。

# in 'exceptions_en_GB.properties'
argument.required=Ebagum lad, the '{0}' argument is required, I say, required.

public static void main(final String[] args) {
MessageSource resources = new ClassPathXmlApplicationContext("beans.xml");
String message = resources.getMessage("argument.required",
new Object [] {"userDao"}, "Required", Locale.UK);
System.out.println(message);
}

上述程序运行时的输出结果是...

Ebagum lad, the 'userDao' argument is required, I say, required.

MessageSourceAware
接口还能用于获取任何已定义的
MessageSource
引用。任何实现了
MessageSourceAware
接口的bean将在创建和配置的时候与
MessageSource
一同被注入。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐