相信对于Spring的国际化资源的使用并不陌生。如果大家忘记了如何使用的话,可以查看博主关于Spring的讲解。在Spring的源码的分析中,我们是基于会使用Spring的基础上的,所以这里我们最关心的是某个功能,Spring的源码是如何实现的。

Spring定义了访问国际化信息的MessageSource接口,并提供了几个易用的实现类。我们可以来看一下MessageSource类图结构。有兴趣的读者可以自己对照源码去了解。(相关资源可到这里下载:http://pan.baidu.com/s/1sjSo9a9)

我们在定义了国际化的资源后,Spring的源码就会去调用initMessageSource函数,那么这个函数是怎样执行的呢?我们先来看一张时序图。

通过上面的时序图,我们可以知道,其实国际化资源的处理就是在AbstractApplicationContext的类中。具体的实现函数如下。

/*** Initialize the MessageSource.* Use parent's if none defined in this context.*/protected void initMessageSource() {ConfigurableListableBeanFactory beanFactory = getBeanFactory();if (beanFactory.containsLocalBean(MESSAGE_SOURCE_BEAN_NAME)) {this.messageSource = beanFactory.getBean(MESSAGE_SOURCE_BEAN_NAME, MessageSource.class);// Make MessageSource aware of parent MessageSource.if (this.parent != null && this.messageSource instanceof HierarchicalMessageSource) {HierarchicalMessageSource hms = (HierarchicalMessageSource) this.messageSource;if (hms.getParentMessageSource() == null) {// Only set parent context as parent MessageSource if no parent MessageSource// registered already.hms.setParentMessageSource(getInternalParentMessageSource());}}if (logger.isDebugEnabled()) {logger.debug("Using MessageSource [" + this.messageSource + "]");}}else {// Use empty MessageSource to be able to accept getMessage calls.DelegatingMessageSource dms = new DelegatingMessageSource();dms.setParentMessageSource(getInternalParentMessageSource());this.messageSource = dms;beanFactory.registerSingleton(MESSAGE_SOURCE_BEAN_NAME, this.messageSource);if (logger.isDebugEnabled()) {logger.debug("Unable to locate MessageSource with name '" + MESSAGE_SOURCE_BEAN_NAME +"': using default [" + this.messageSource + "]");}}}

在initMessageSource的函数中,主要功能是提取配置中定义的messageSource,并将其记录在Spring的容器中,也就是AbstractApplicatrionContext中。上面的方法中有beanFactory.getBean(MESSAGE_SOURCE_BEAN_NAME,MessageSource.class),这就要求资源文件必须为message,否则便会取不到自定义的资源配置,也就是会抛出异常。

Spring源码之ApplicationContext(七)获取消息资源相关推荐

  1. 第五篇:Spring源码篇-ApplicationContext

    Spring源码篇-ApplicationContext   前面通过手写IoC,DI.AOP和Bean的配置.到最后ApplicationContext的门面处理,对于Spring相关的核心概念应该 ...

  2. Spring源码之getBean(获取 bean)方法(二)解读

    目录 前言 `spring` 初始化 `bean` 过程 进入类 `ClassPathXmlApplicationContext` 的构造器 `AbstractApplicationContext` ...

  3. Spring源码 - 从缓存中获取单例Bean

    # Spring源码 - 从缓存中获取单例Bean Spring版本:Spring 5.3.13-release # 1.从缓存中获取单例Bean 单实例Bean在Spring的同一个容器中只会创建一 ...

  4. Spring源码学习第七天==>解析配置注解类与BPP

    关键词: Spring解析配置类注解Bean Spring注册Bean后置增强器(BPP) Spring消息资源和监听器的初始化 一:Spring解析配置类注解Bean==>Configurat ...

  5. Spring源码之ApplicationContext

    ​ 本文是针对Srping的ClassPathXMLApplicationContext来进行源码解析,在本篇博客中将不会讲述spring Xml解析注册代码,因为ApplicationContext ...

  6. Spring 源码解读第七弹!bean 标签的解析

    Spring 源码解读继续. 本文是 Spring 系列第八篇,如果小伙伴们还没阅读过本系列前面的文章,建议先看看,这有助于更好的理解本文. Spring 源码解读计划 Spring 源码第一篇开整! ...

  7. Spring源码学习1.4 获取XML的验证模式

    Spring5源码分析(008)--IoC篇之加载BeanDefinition:获取XML的验证模式 -- 转载 注:<Spring5源码分析>汇总可参考:Spring5源码分析(002) ...

  8. Spring源码解析-applicationContext.xml加载和bean的注册

    applicationContext文件加载和bean注册流程 ​ Spring对于从事Java开发的boy来说,再熟悉不过了,对于我们这个牛逼的框架的介绍就不在这里复述了,Spring这个大杂烩,怎 ...

  9. Spring 源码分析(七)--bean的加载详细分析

    一:缓存中获取单例bean 前面已经提到过,单例在Spring的同一个容器内只会被创建一次,后续再获取bean直接从单例缓存中获取,当然这里也只是尝试加载,首先尝试从缓存中加载,然后再次尝试从sing ...

  10. spring源码之模拟mybatis获取Mapper

    上一章说了,既然mybatis获取Mapper的时候使用的是代理,那么我们是否可以模拟实现一下. 话不多说上代码: 这样就更加清楚mapper的代理实现过程了吧. public class MySql ...

最新文章

  1. 数据挖掘中分类算法小结
  2. hbase hyperbase 区别_大数据之HBase的几个常规性问题
  3. mysql数据表交叉引用_数据定义语句
  4. 【CodeForces - 1042C】Array Product(思维,有坑细节)
  5. 使用Hutool来实现深拷贝
  6. 很多人把红薯当成减肥餐,吃红薯到底是增肥还是减肥?
  7. 通过自定义类加载器进行动态编译与动态实现接口
  8. 游戏开发之测试篇(C++)
  9. 若要允许 GET 请求,请将 JsonRequestBehavior 设置为 AllowGet
  10. Java基于SSH技术的毕业设计管理系统
  11. excel制作跨职能流程图_一款小白轻松上手流程图绘制工具亿图图示
  12. chrome离线小恐龙改造版
  13. highmem 分配使用与物理地址的对应关系
  14. 电子警察系统设计(原理+流程+论文)
  15. flask蓝图buleprint使用
  16. 卡刷android版本不一致,你好,请问一下,关于跨安卓版本刷机的问题
  17. ioncube加密与解密 php代码
  18. 计算机网络自顶向下-套接字编程作业
  19. 二极管质量好坏判断方法,轻松学会-通程创品
  20. python名片管理系统难点总结_Python 知识要点:名片管理系统 2.0

热门文章

  1. 对课程第二次作业的补充与反馈
  2. 版本控制-代码和文档等用SVN管理
  3. 设计模式 - 状态模式、职责连模式
  4. cmd下运行Oracle清屏命令
  5. Windows程序设计学习-API-SetBkMode
  6. 二十四、K8s集群强化2-授权
  7. Kubernetes详解(十八)——Pod就绪性探针实战
  8. HDOJ--4548--美素数
  9. yum安装Jenkins
  10. MySql添加外键报错:Cannot add foreign key constraint