`
meiyx
  • 浏览: 180672 次
  • 性别: Icon_minigender_2
  • 来自: 北京
社区版块
存档分类
最新评论

spring中ResourceBundleMessageSource与ReloadableResourceBundleMessageSource查找资源的区别:

阅读更多
spring中ResourceBundleMessageSource与ReloadableResourceBundleMessageSource查找资源的区别:

1.ResourceBundleMessageSource在xml配置中无法指定编码:

     <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">

     <property name="basenames">

          <list>

               <value >error</value >

               <value >message</value >

          </list>      

         </property>

     </bean>

而ReloadableResourceBundleMessageSource可以指定编码,譬如:

     <bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">

        <property name="defaultEncoding" value ="gbk" />

        <property name="basename" value ="message" />

</bean>

2.加载资源文件的方式不同:

1).下面看下它们的源代码:

ResourceBundleMessageSource的加载,使用ClassUtils.getDefaultClassLoader()加载器,getDefaultClassLoader的方法代码如下:

p lic static ClassLoader getDefaultClassLoader()

{

    ClassLoader cl = null;

    try {

      cl = Thread.currentThread().getContextClassLoader();

   }

    catch (Throwable ex) {

      logger.debug("Cannot access thread context ClassLoader - falling back to system class loader", ex);

    }

    if (cl == null)

    {

      cl = ClassUtils.class.getClassLoader();

    }

    return cl;

}

//这种方式也是JVM默认的加载方式,先从当前线程中获取类加载器,如果没有,就获取这个类本身的类加载器

2).ReloadableResourceBundleMessageSource默认也使用ClassUtils.getDefaultClassLoader()加载器,它加载资源的方式如下:

p lic Resource getResource(String location)

{

    Assert.notNull(location, "Location must not be null");

    if (location.startsWith("classpath:")) {

      return new ClassPathResource(location.s string("classpath:".length()), getClassLoader());

    }

    try

    {

      URL url = new URL(location);

      return new UrlResource(url);

    }

    catch (MalformedURLException ex)

    {

      return getResourceByPath(location);

    }

}

3). 小结:ResourceBundleMessageSource从classloader中加载资源文件,可以找到,

ReloadableResourceBundleMessageSource加载时,默认使用DefaultResourceLoader,他会先判断资源path是否带有classpath:前缀,如果有,用 ClassPathResource去加载资源文件,如果没有试着用文件协议的url去访问,再没有就在contextPath即WEB-INF下查找。

下面做一个Spring的MessageSource的示例:

1.我们单独新建一个spring消息文件beans-message.xml中加如下配置:

   <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">

       <property name="basenames">

            <list>

                 <value >error</value>

                 <value >message</value >

            </list>

       </property>

   </bean>

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

msg.common.serverBusy = \非\常\抱\歉,\系\统\十\分\繁\忙\!

#非常抱歉,系统十分繁忙!

msg.argument.required={0}\是\个\必\填\项\!

#{0}是个必填项!

3.再写一个测试类:

p lic class MessageTest {

     p lic static void main(String[] args) {

         MessageSource resources = new ClassPathXmlApplicationContext("beans-message.xml");

         String message = resources.getMessage("msg.common.serverBusy", null, "Default", null);

         System.out.println(message);

         String message1 = resources.getMessage("msg.argument.required", new Object[] { "'联系方式'" }, null, Locale.CHINA);

         System.out.println(message1);

     }

}

结果输入为:

非常抱歉,系统十分繁忙!

'联系方式'是个必填项!

3.在我们的项目中,MessageSource不会单独使用,通常我们会把它和自己的业务一起使用,这时候我们可以直接用它本身的方法,我们也可以在其中加入我们自己的逻辑:如,自定义的一个消息类:

p lic class MessageSourceHelper {

     private ResourceBundleMessageSource messageSource;

     p lic String getMessage(String code, Object[] args, String defaultMessage, Locale locale) {

         String msg = messageSource.getMessage(code, args, defaultMessage, locale);

         return msg != null ? msg.trim() : msg;

     }

    

     p lic void setMessageSource(ResourceBundleMessageSource messageSource) {

         this.messageSource = messageSource;

     }

}

在beans-message.xml中注入:

   <bean id="messageSourceHelper" class="com.myspring.message.MessageSourceHelper">

       <property name="messageSource">

           <ref local="messageSource" />

       </property>

   </bean>

4.我们可以在MessageSourceHelper中加入自己的业务,注入依赖后,就可以在其他类中调用MessageSourceHelper中的方法。

5.理论简要: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):本质上和上一个方法相同,其区别在:没有指定默认值,如果没找到消息,会抛出一个NoS hMessageException异常。

     · 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会用得更多一些,

6. 更多的资料参考spring官方开发手册,很详尽的!
分享到:
评论
1 楼 xsz 2016-07-15  
 

相关推荐

    Spring 3 MVC 国际化 ResourceBundleMessageSource spring:message

    一个简单的基于Maven 3 和 Spring mvc 3 框架搭建的国际化网站雏形。 spring mvc 入门配置 国际化配置 易扩展。 error_messages_en.properties error_messages_zh.properties 没有库文件,maven配好了会自动加载库...

    spring源码分析(1-10)

    Spring源代码解析(一):Spring中的事务处理 Spring源代码解析(二):ioc容器在Web容器中的启动 Spring源代码分析(三):Spring JDBC Spring源代码解析(四):Spring MVC Spring源代码解析(五):Spring AOP获取Proxy ...

    浅析Spring配置中的classpath:与classpath*:的区别

    主要介绍了Spring配置中的"classpath:"与"classpath*:"的区别,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

    Spring技术内幕:深入解析 Spring架构与设计原理.pdf

    第三部分讲述了ACEGI安全框架、DM模块以及Flex模块等基于Spring的典型应用的设计与实现。 无论你是Java程序员、Spring开发者,还是平台开发人员、系统架构师,抑或是对开源软件源代码着迷的代码狂人,都能从本书中...

    Spring源代码解析(一):IOC容器.doc

    Spring源代码解析(一):IOC容器.doc

    Spring源代码解析

    Spring源代码解析(二):IoC容器在Web容器中的启动 Spring源代码解析(三):Spring JDBC Spring源代码解析(四):Spring MVC Spring源代码解析(五):Spring AOP获取Proxy Spring源代码解析(六):Spring声明式事务...

    spring源码分析

    1.Spring源代码解析(一):Spring中的事务处理 2. Spring源代码解析(二):ioc容器在Web容器中的启动 3.Spring源代码解析(三):Spring JDBC 4.Spring源代码解析(四):Spring MVC 5.Spring源代码解析(五):Spring ...

    Spring技术内幕:深入解析Spring架构与设计原理(第2部分)

     如果你以一种淡定的心态翻开这本书,无论你是Java程序员、Spring开发者,还是平台开发人员、系统架构师,抑或是对开源软件源代码着迷的代码狂人,都能从《Spring技术内幕:深入解析Spring架构与设计原理》中受益。...

    Spring源码学习文档,绝对值得好好研究~~

    Spring源代码解析(一)Spring中的事务处理.doc Spring源代码解析(二):ioc容器在Web容器中的启动.doc Spring源代码分析(三):Spring JDBC.doc Spring源代码解析(四):Spring MVC.doc Spring源代码解析(五):Spring ...

    spring-webmvc-5.0.8.RELEASE-API文档-中文版.zip

    赠送jar包:spring-webmvc-5.0.8.RELEASE.jar; 赠送原API文档:spring-webmvc-5.0.8.RELEASE-javadoc.jar; 赠送源代码:spring-webmvc-5.0.8.RELEASE-sources.jar; 赠送Maven依赖信息文件:spring-webmvc-5.0.8....

    spring-context-support-1.0.10-API文档-中文版.zip

    赠送jar包:spring-context-support-1.0.10.jar; 赠送原API文档:spring-context-support-1.0.10-javadoc.jar; 赠送源代码:spring-context-support-1.0.10-sources.jar; 赠送Maven依赖信息文件:spring-context-...

    Spring技术内幕:深入解析Spring架构与设计原理.pdf

    Spring技术内幕:深入解析Spring架构与设计原理.pdf

    Spring技术内幕:深入解析Spring架构与设计原理

    下载频道&gt;资源分类&gt;开发技术&gt;Java&gt;Spring技术内幕:深入解析Spring架构与设计原理 1/2 Spring技术内幕:深入解析Spring架构与设计原理 1/2资源大小:59MB 上传日期:2011-11-15 资源积分:5分 下载次数:30 上 传 者...

    2023最新《Spring Boot基础教程》

    Spring Boot 2.x基础教程:加密配置中的敏感信息 API开发 Spring Boot 2.x基础教程:构建RESTful API与单元测试 Spring Boot 2.x基础教程:使用Swagger2构建强大的API文档 Spring Boot 2.x基础教程:JSR-303实现请求...

    Spring Boot实战与原理分析视频课程包含14-18

    Spring Boot实战与原理分析视频课程包含14-18,本视频教程为网络整理,如有侵权,请联系删除。谢谢 Spring Boot实战与原理分析视频课程 课程目录: 1 Spring Boot概述与课程概要介绍20:33 2 Spring4 快速入门59:56...

    spring-aop-5.0.10.RELEASE-API文档-中文版.zip

    赠送jar包:spring-aop-5.0.10.RELEASE.jar; 赠送原API文档:spring-aop-5.0.10.RELEASE-javadoc.jar; 赠送源代码:spring-aop-5.0.10.RELEASE-sources.jar; 赠送Maven依赖信息文件:spring-aop-5.0.10.RELEASE....

    Spring boot 示例 官方 Demo

    spring-boot-helloWorld:spring-boot的helloWorld版本 spring-boot-mybaits-annotation:注解版本 spring-boot-mybaits-xml:xml配置版本 spring-boot-mybatis-mulidatasource:springboot+mybatis多数据源最简解决...

    Spring集成MongoDB官方指定jar包:spring-data-mongodb-1.4.1.RELEASE.jar

    Spring集成MongoDB官方指定jar包:spring-data-mongodb-1.4.1.RELEASE.jar

    spring-jdbc-5.3.15-API文档-中文版.zip

    赠送jar包:spring-jdbc-5.3.15.jar; 赠送原API文档:spring-jdbc-5.3.15-javadoc.jar; 赠送源代码:spring-jdbc-5.3.15-sources.jar; 赠送Maven依赖信息文件:spring-jdbc-5.3.15.pom; 包含翻译后的API文档:...

Global site tag (gtag.js) - Google Analytics