spring中MessageSource的配置使用方法3--ResourceBundleMessageSource
2014-12-03 11:20
507 查看
ApplicationContext接口扩展了
MessageSource接口,因而提供了消息处理的功能(i18n或者国际化)。与
HierarchicalMessageSource一起使用,它还能够处理嵌套的消息,这些是Spring提供的处理消息的基本接口。让我们快速浏览一下它所定义的方法:
String getMessage(String code, Object[] args, String default, Locale loc):用来从来作消息中替换值。[/code]MessageSource获取消息的基本方法。如果在指定的locale中没有找到消息,则使用默认的消息。args中的参数将使用标准类库中的[code]MessageFormat
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会用得更多一些,为此提供了一下示例:
<span style="font-size: 14px;"><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></span>
这段配置假定在你的classpath中有三个资源文件(resource bundle),它们是
format,
exceptions和
windows。通过ResourceBundle,使用JDK中解析消息的标准方式,来处理任何解析消息的请求。出于示例的目的,假定上面的两个资源文件的内容为…
<span style="font-size: 14px;"><span class="lineannotation" style="font-style: italic;"># in 'format.properties'</span> message=Alligators rock!</span>
<span style="font-size: 14px;"><span class="lineannotation" style="font-style: italic;"># in 'exceptions.properties'</span> argument.required=The '{0}' argument is required.</span>
下面是测试代码。因为
ApplicationContext实现也都实现了
MessageSource接口,所以能被转型为
MessageSource接口
<span style="font-size: 14px;">public static void main(String[] args) { MessageSource resources = new ClassPathXmlApplicationContext("beans.xml"); String message = resources.getMessage("message", null, "Default", null); System.out.println(message); }</span>
上述程序的输出结果将会是...
<span style="font-size: 14px;">Alligators rock!</span>
总而言之,我们在
'beans.xml'的文件中(在classpath根目录下)定义了一个
messageSourcebean,通过它的
basenames属性引用多个资源文件;而
basenames属性值由list元素所指定的三个值传入,它们以文件的形式存在并被放置在classpath的根目录下(分别为
format.properties,
exceptions.properties和
windows.properties)。
再分析个例子,这次我们将着眼于传递参数给查找的消息,这些参数将被转换为字符串并插入到已查找到的消息中的占位符(译注:资源文件中花括号里的数字即为占位符)。
<span style="font-size: 14px;"><beans> <span class="lineannotation" style="font-style: italic;"><!-- this <code class="interfacename prettyprint"><span class="typ">MessageSource</span></code> is being used in a web application --></span> <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource"> <property name="baseName" value="WEB-INF/test-messages"/> </bean> <span class="lineannotation" style="font-style: italic;"><!-- let's inject the above <code class="interfacename prettyprint"><span class="typ">MessageSource</span></code> into this POJO --></span> <bean id="example" class="com.foo.Example"> <property name="messages" ref="messageSource"/> </bean> </beans></span>
<span style="font-size: 14px;">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); } }</span>
调用
execute()方法的输出结果是...
<span style="font-size: 14px;">The 'userDao' argument is required.</span>
对于国际化(i18n),Spring中不同的
MessageResource实现与JDK标准ResourceBundle中的locale解析规则一样。比如在上面例子中定义的
messageSourcebean,如果你想解析British (en-GB) locale的消息,那么需要创建
format_en_GB.properties,
exceptions_en_GB.properties和
windows_en_GB.properties三个资源文件。
Locale解析通常由应用程序根据运行环境来指定。出于示例的目的,我们对将要处理的(British)消息手工指定locale参数值。
<span style="font-size: 14px;"><span class="lineannotation" style="font-style: italic;"># in 'exceptions_en_GB.properties'</span> argument.required=Ebagum lad, the '{0}' argument is required, I say, required.</span>
<span style="font-size: 14px;">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); }</span>
上述程序运行时的输出结果是...
<span style="font-size: 14px;">Ebagum lad, the 'userDao' argument is required, I say, required.</span>
MessageSourceAware接口还能用于获取任何已定义的
MessageSource引用。任何实现了
MessageSourceAware接口的bean将在创建和配置的时候与
MessageSource一同被注入。
相关文章推荐
- spring中MessageSource的配置使用方法3--ResourceBundleMessageSource
- spring中MessageSource的配置使用方法2--ReloadableResourceBundleMessageSource
- spring中MessageSource的配置使用方法3--ResourceBundleMessageSource【转】
- spring中MessageSource的配置使用方法3--ResourceBundleMessageSource
- spring中MessageSource的配置使用方法2--ReloadableResourceBundleMessageSource
- spring中MessageSource的配置使用方法--ResourceBundleMessageSource
- spring中MessageSource的配置使用方法3--ResourceBundleMessageSource
- spring中MessageSource的配置使用方法3--ResourceBundleMessageSource
- spring中MessageSource的配置使用方法2--ReloadableResourceBundleMessageSource
- spring中MessageSource的配置使用方法2--ReloadableResourceBundleMessageSource【转】
- spring中MessageSource的配置使用方法2--ReloadableResourceBundleMessageSource
- spring中MessageSource的配置使用方法3--ResourceBundleMessageSource
- spring中MessageSource的配置使用方法3--ResourceBundleMessageSource
- spring中MessageSource的配置使用方法3--ResourceBundleMessa
- spring中MessageSource的配置使用方法2--ReloadableResourceBundleMessageSource
- spring中MessageSource的配置使用方法2--ReloadableResourceBundleMessageSource
- spring中MessageSource的配置使用方法2--ReloadableResourceBundleMessageSource
- spring中MessageSource的配置使用方法2--ReloadableResourceBundleMessageSource
- spring中MessageSource的配置使用方法1
- spring中的ResourceBundleMessageSource使用和测试示例