看这篇文章之前可以先了解之前的跟踪流程,https://www.jianshu.com/p/4934233f0ead

代码过宽,可以shift + 鼠标滚轮 左右滑动查看

AbstractApplicationContext类中refresh()方法的第一个调用方法prepareRefresh()的跟踪。

  1. @Override
  2. public void refresh() throws BeansException, IllegalStateException {
  3. synchronized (this.startupShutdownMonitor) {
  4. // Prepare this context for refreshing.
  5. // 准备上下文的刷新
  6. prepareRefresh();
  7. ···
  8. }

直接进入。

prepareRefresh(零)

prepareRefresh方法在AbstractApplicationContext类中,此类是XmlWebApplicationContext的上层父类

  1. // Prepare this context for refreshing.
  2. // 准备 context 的刷新
  3. prepareRefresh();
  4. /**
  5. * Prepare this context for refreshing, setting its startup date and
  6. * active flag as well as performing any initialization of property sources.
  7. *
  8. * 准备 context 的刷新,设置他的启动数据、激活标志以及执行一些属性源的初始化
  9. */
  10. protected void prepareRefresh() {
  11. // 启动时间
  12. this.startupDate = System.currentTimeMillis();
  13. // 此 context 是否已经被关闭
  14. this.closed.set(false);
  15. // active 表示这个 context 当前是否处于活跃状态
  16. this.active.set(true);
  17. if (logger.isInfoEnabled()) {
  18. logger.info("Refreshing " + this);
  19. }
  20. // Initialize any placeholder property sources in the context environment
  21. // 1.在 context 的 environment 中初始化占位符属性源
  22. initPropertySources();
  23. // Validate that all properties marked as required are resolvable
  24. // see ConfigurablePropertyResolver#setRequiredProperties
  25. // 验证所有被标记为必要的属性是否可解析,如果有遗失属性则不能解析并抛出异常,
  26. // 可以参考setRequiredProperties方法
  27. getEnvironment().validateRequiredProperties();
  28. // Allow for the collection of early ApplicationEvents,
  29. // to be published once the multicaster is available...
  30. // 一旦有可利用的广播,允许早期的ApplicationEvents集合被发布
  31. this.earlyApplicationEvents = new LinkedHashSet<ApplicationEvent>();
  32. }

1.initPropertySources

跟踪标记1的方法

initPropertySources 方法在 AbstractApplicationContext 中默认不做任何事情,具体业务流程由其子类覆盖完成,也就是在 AbstractRefreshableWebApplicationContext 类中进行,AbstractRefreshableWebApplicationContext 是XmlWebApplicationContext的父类

  1. // Initialize any placeholder property sources in the context environment
  2. // 1.在 context environment 中初始化占位符属性源
  3. initPropertySources();
  4. /**
  5. * {@inheritDoc}
  6. * <p>Replace {@code Servlet}-related property sources.
  7. *
  8. * 替换Servlet相关的属性源
  9. */
  10. @Override
  11. protected void initPropertySources() {
  12. // 在设置 root web application context 的 contextConfigLocation 属性时,已经调用过此方法。
  13. // 在ContextLoader类中的configureAndRefreshWebApplicationContext方法中,
  14. // 又调用了 initPropertySources 方法对 environment 进行了初始化
  15. // 此时 environment 已存在,直接返回。
  16. ConfigurableEnvironment env = getEnvironment();
  17. if (env instanceof ConfigurableWebEnvironment) {
  18. // 1.1初始化属性源
  19. ((ConfigurableWebEnvironment) env).initPropertySources(this.servletContext, this.servletConfig);
  20. }
  21. }

1.1 initPropertySources

跟踪标记1.1的方法

env(也就是environment对象)调用了initPropertySources方法,该方法的具体实现由ConfigurableWebEnvironment的子类StandardServletEnvironment进行。

environment对象是StandardServletEnvironment类的实例。

  1. //1.1初始化属性源
  2. ((ConfigurableWebEnvironment) env).initPropertySources(this.servletContext, this.servletConfig);
  3. /**
  4. * Replace any {@linkplain
  5. * org.springframework.core.env.PropertySource.StubPropertySource stub property source}
  6. * instances acting as placeholders with real servlet context/config property sources
  7. * using the given parameters.
  8. *
  9. * 用真正的servlet context/config 属性源来替换作为占位符存在的 stub 属性源
  10. */
  11. @Override
  12. public void initPropertySources(ServletContext servletContext, ServletConfig servletConfig) {
  13. // 工具类来初始化属性源,进入这个方法
  14. WebApplicationContextUtils.initServletPropertySources(getPropertySources(), servletContext, servletConfig);
  15. }
  16. /**
  17. * Replace {@code Servlet}-based {@link StubPropertySource stub property sources} with
  18. * actual instances populated with the given {@code servletContext} and
  19. * {@code servletConfig} objects.
  20. * <p>This method is idempotent with respect to the fact it may be called any number
  21. * of times but will perform replacement of stub property sources with their
  22. * corresponding actual property sources once and only once.
  23. *
  24. * 用<由servletContext和servletConfig对象填充的>实例去替换基于servlet的 stub 属性源。
  25. * 这个方法幂等遵守这个情况:他可能被调用任意次数,但是有且仅会执行一次 stub 属性源与它所匹配
  26. * 的实际属性源的替换
  27. */
  28. public static void initServletPropertySources(
  29. MutablePropertySources propertySources, ServletContext servletContext, ServletConfig servletConfig) {
  30. Assert.notNull(propertySources, "'propertySources' must not be null");
  31. // 此条件第三项不通过。
  32. // 在ContextLoader类的configureAndRefreshWebApplicationContext方法中
  33. // 已调用过一次StandardServletEnvironment对象的initPropertySources方法,
  34. // 完成了根属性源的替换,所以第三项不再通过
  35. if (servletContext != null && propertySources.contains(StandardServletEnvironment.SERVLET_CONTEXT_PROPERTY_SOURCE_NAME) &&
  36. propertySources.get(StandardServletEnvironment.SERVLET_CONTEXT_PROPERTY_SOURCE_NAME) instanceof StubPropertySource) {
  37. propertySources.replace(StandardServletEnvironment.SERVLET_CONTEXT_PROPERTY_SOURCE_NAME,
  38. new ServletContextPropertySource(StandardServletEnvironment.SERVLET_CONTEXT_PROPERTY_SOURCE_NAME, servletContext));
  39. }
  40. //此处servletConfig为空,所以跳过
  41. if (servletConfig != null && propertySources.contains(StandardServletEnvironment.SERVLET_CONFIG_PROPERTY_SOURCE_NAME) &&
  42. propertySources.get(StandardServletEnvironment.SERVLET_CONFIG_PROPERTY_SOURCE_NAME) instanceof StubPropertySource) {
  43. propertySources.replace(StandardServletEnvironment.SERVLET_CONFIG_PROPERTY_SOURCE_NAME,
  44. new ServletConfigPropertySource(StandardServletEnvironment.SERVLET_CONFIG_PROPERTY_SOURCE_NAME, servletConfig));
  45. }
  46. }

prepareRefresh方法只有这么多,接下来跟踪obtainFreshBeanFactory方法:

https://www.jianshu.com/p/144af98965d9

总结

  • 设置 context 关闭状态、激活标志等
  • 在 context 的 environment 中初始化占位符属性源,用真正的servlet context/config 属性源来替换作为占位符存在的 stub 属性源
  • 验证所有被标记为必要的属性是否可解析

prepareRefresh方法源码跟踪的更多相关文章

  1. prepareRefresh()方法源码探究

    该方法目的是做刷新上下文前的准备工作: 首先清空bean扫描器map中的内容,然后调用父类的prepareRefresh方法: 父类的准备刷新方法,主要做了3个工作: 1.简单的标志赋值----> ...

  2. loadBeanDefinitions方法源码跟踪(一)

    看这篇文章之前可以先了解之前的跟踪流程,https://www.jianshu.com/p/4934233f0ead 代码过宽,可以shift + 鼠标滚轮 左右滑动查看 AbstractBeanDe ...

  3. postProcessBeanFactory方法源码跟踪

    看这篇文章之前可以先了解之前的跟踪流程,https://www.jianshu.com/p/4934233f0ead 代码过宽,可以shift + 鼠标滚轮 左右滑动查看 AbstractApplic ...

  4. prepareBeanFactory方法源码跟踪

    看这篇文章之前可以先了解之前的跟踪流程,https://www.jianshu.com/p/4934233f0ead 代码过宽,可以shift + 鼠标滚轮 左右滑动查看 AbstractApplic ...

  5. loadBeanDefinitions方法源码跟踪(三)

    因为字数超过了限制,所以分成了三篇,承接上篇: https://www.jianshu.com/p/46e27afd7d96 代码过宽,可以shift + 鼠标滚轮 左右滑动查看 4.parseCus ...

  6. obtainFreshBeanFactory方法源码跟踪

    看这篇文章之前可以先了解之前的跟踪流程,https://www.jianshu.com/p/4934233f0ead 代码过宽,可以shift + 鼠标滚轮 左右滑动查看 AbstractApplic ...

  7. loadBeanDefinitions方法源码跟踪(二)

    因为字数超过了限制,所以分成了三篇,承接上篇: https://www.jianshu.com/p/a0cfaedf3fc5 代码过宽,可以shift + 鼠标滚轮 左右滑动查看 3.parseDef ...

  8. erlang下lists模块sort(排序)方法源码解析(一)

    排序算法一直是各种语言最简单也是最复杂的算法,例如十大经典排序算法(动图演示)里面讲的那样 第一次看lists的sort方法的时候,蒙了,几百行的代码,我心想要这么复杂么(因为C语言的冒泡排序我记得不 ...

  9. Java源码跟踪阅读技巧

    转:https://www.jianshu.com/p/ab865109070c 本文基于Eclipse IDE 1.Quick Type Hierarchy 快速查看类继承体系. 快捷键:Ctrl ...

随机推荐

  1. 学习 Python,怎能不懂点PEP 呢?

    或许你是一个初入门 Python 的小白,完全不知道 PEP 是什么.又或许你是个学会了 Python 的熟手,见过几个 PEP,却不知道这玩意背后是什么.那正好,本文将系统性地介绍一下 PEP,与大 ...

  2. laravel 排除csrf验证

    中(*排除所有路由)

  3. cnblog 开通啦!

    喜大普奔! 终于开通cnblog了!以后有blog都会放这里哦 > o < 希望大家可以关注窝哦.

  4. icos下配置snake test

    Topo: # $language = "Python" # $interface = "1.0"# Author:Bing Song# Date:6/21/2 ...

  5. 吴裕雄--天生自然TensorFlow2教程:误差计算

    import tensorflow as tf y = tf.constant([1, 2, 3, 0, 2]) y = tf.one_hot(y, depth=4) # max_label=3种 y ...

  6. RedHat OpenShift QuickStart 1.2

    一.在容器中传入/出文件 1. 创建一个初始化项目 oc login -u developer -p developer oc new-project myproject 2. 在容器中下载文件 先通 ...

  7. spring boot中不能识别RestController

    参考:https://blog.csdn.net/qq_16739693/article/details/80271987

  8. swoole 消息队列

    <?php /** * 场景: * 监控订单表状态 队列通信 * 一个进程向队列发布消息 另外两个进程争抢 */ //设置主进程名 echo '主进程id:' . posix_getpid() ...

  9. mybatis利用generator自动生成的代码

    /** * 排序规则 */ protected String orderByClause; /** * 去重规则 */ protected boolean distinct; /** * where条 ...

  10. 7(计算机网络) ICMP与ping

    无论是在宿舍,还是在办公室,或者运维一个数据中心,我们常常会遇到网络不通的问题.那台机器明明就在那里,你甚至都可以通过机器的终端连上去看.它看着好好的,可是就是连不上去,究竟是哪里出了问题呢? ICM ...