ApplicationContext
接口扩展了MessageSource
接口,因而提供了消息处理的功能(i18n或者国际化)。与HierarchicalMessageSource
一起使用,它还能够处理嵌套的消息,这些是Spring提供的处理消息的基本接口。让我们快速浏览一下它所定义的方法:
-
String getMessage(String code, Object[] args, String default, Locale loc):用来从MessageSource
获取消息的基本方法。如果在指定的locale中没有找到消息,则使用默认的消息。args中的参数将使用标准类库中的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
会用得更多一些,为此提供了一下示例:
<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 MessageSource
into this POJO -->
<bean id="example" class="com.foo.Example">
<property name="messages" ref="messageSource"/>
</bean>
</beans>
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
一同被注入。
分享到:
相关推荐
spring messageSource功能结合ehcache实现提示语句从数据库读取demo,注:sql脚本都在代码中,如有问题及时沟通指点。
只要有了这个配置,然后配置JSP渲染器为JSTL支持的,那么在你的JSP文件中使用fmt标记就可以实现客户浏览器语言国际化了。 如:<fmt:message key="info.login.title" /> 其中的info.login.title和你的资源文件对应
6.8.1. 在Spring中使用AspectJ来为domain object进行依赖注入 6.8.1.1. @Configurable object的单元测试 6.8.1.2. 多application context情况下的处理 6.8.2. Spring中其他的AspectJ切面 6.8.3. 使用Spring IoC来...
6.8.1. 在Spring中使用AspectJ进行domain object的依赖注入 6.8.2. Spring中其他的AspectJ切面 6.8.3. 使用Spring IoC来配置AspectJ的切面 6.8.4. 在Spring应用中使用AspectJ加载时织入(LTW) 6.9. 更多资源 7...
12.2 在Spring中使用Hibernate 12.2.1 配置SessionFactory 12.2.2 使用HibernateTemplate 12.2.3 处理LOB类型数据 12.2.4 添加Hibernate事件监听器 12.2.5 使用原生Hibernate API 12.2.6 使用注解配置 12.2.7 事务...
Spring和其它的框架一样,也提供了国际化功能,它是通过MessageSource接口来实现的 ApplicationContext接口继承了MessageSource 。 MessageSource接口方法
6.8.1. 在Spring中使用AspectJ来为domain object进行依赖注入 6.8.2. Spring中其他的AspectJ切面 6.8.3. 使用Spring IoC来配置AspectJ的切面 6.8.4. 在Spring应用中使用AspectJ Load-time weaving(LTW) 6.9. ...
12.2 在Spring中使用Hibernate 12.2.1 配置SessionFactory 12.2.2 使用HibernateTemplate 12.2.3 处理LOB类型数据 12.2.4 添加Hibernate事件监听器 12.2.5 使用原生Hibernate API 12.2.6 使用注解配置 12.2.7 事务...
6.8.1. 在Spring中使用AspectJ来为domain object进行依赖注入 6.8.2. Spring中其他的AspectJ切面 6.8.3. 使用Spring IoC来配置AspectJ的切面 6.8.4. 在Spring应用中使用AspectJ Load-time weaving(LTW) 6.9. ...
使用普通的旧MessageSource简单的错误消息插值。 可自定义的HTTP错误表示形式。 将参数从异常公开到错误消息。 同时支持传统和React堆。 可自定义的异常日志记录。 支持错误指纹识别。 入门 下载 下载或通过...
6.8.1. 在Spring中使用AspectJ进行domain object的依赖注入 6.8.2. Spring中其他的AspectJ切面 6.8.3. 使用Spring IoC来配置AspectJ的切面 6.8.4. 在Spring应用中使用AspectJ加载时织入(LTW) 6.9. 更多资源 7...
此项目模板与完全相同,但使用注释代替 XML 配置。 带有 REST 服务的 Spring MVC Hibernate HSQL 数据库连接 基于数据库MessageSource的国际化 依赖注入 EJB 启动 Bean 要运行此应用程序,请安装 Jboss AS 7.1.1...
3. New Features and Enhancements in Spring Framework 4.0 ............................................ 17 3.1. Improved Getting Started Experience .........................................................
像Rails,Django和TurboGears这样的动态框架在Web开发领域开辟了一条新的道路,Grails基于这些概念之上,采用动态方法减小了Java平台上进行Web开发的复杂度,不过与那些框架不同的是,Grails是构建在Spring和...
我创建此存储库只是为了作为对如何在Spring项目中配置以下功能的简单参考: 带有REST服务的Spring MVC Hibernate HSQL数据库连接 基于数据库MessageSource的国际化 依赖注入 EJB启动Bean 没有测试,可耻的是我:) ...
像Rails,Django和TurboGears这样的动态框架在Web开发领域开辟了一条新的道路,Grails基于这些概念之上,采用动态方法减小了Java平台上进行Web开发的复杂度,不过与那些框架不同的是,Grails是构建在Spring和...
描述该项目提供了两个注释:AutowiredParam和ResourceParam 这些可用于将Spring上下文Bean“注入”到方法参数中。 Spring已经为许多类型的方法参数提供了注入,但是这些注释使您可以添加自己的注释,而无需编写其他...
易于使用的基于Hibernate的对象-关系映射(ORM)层称为Groovy Server Pages (GSP)的表现层技术基于Spring MVC的控制器层构建于Gant 上的命令行脚本运行环境内置Jetty服务器,不用重新启动服务器就可以进行重新加载利用...
Pebble 模板引擎的翻译扩展,使用 Spring 消息接口 概述 为模板引擎提供t函数,它从 Spring MessageSource 检索消息。 {{ t('users.show.title') }} “懒惰”查找 函数支持,因此您可以在users/show.html查找users....
易于使用的基于Hibernate的对象-关系映射(ORM)层称为Groovy Server Pages (GSP)的表现层技术基于Spring MVC的控制器层构建于Gant 上的命令行脚本运行环境内置Jetty服务器,不用重新启动服务器就可以进行重新加载利用...