spring启动流程

打印 上一主题 下一主题

主题 901|帖子 901|积分 2703

Spring启动流程

随着springboot的功能越来越强大,我们渐渐忘记了spring,但是每当碰到问题时缺无从下手,
我们在享受springboot给我们带来的便利的同时更应该了解其底层原理,知其然更要知其以是然,下面我们一起进入spring的世界探索吧
Spring启动

我们从springboot的入口(不利用springboot的方式放在背面文章讲解)来看spring是怎样被启动的
我们都有启动类如下
AttachCode
  1. @SpringBootApplication
  2. public class Main {
  3.     public static void main(String[] args) {
  4.         SpringApplication.run(Main.class, args);
  5.     }
复制代码
我们先来看下SpringApplication.run(Main.class, args);我们今天的主要内容不是springboot,以是我们一路跟踪到spring的refesh方法
AttachCode
  1.    public static ConfigurableApplicationContext run(Class<?>[] primarySources, String[] args) {
  2.         return (new SpringApplication(primarySources)).run(args);
  3.     }
复制代码
AttachCode
  1. public void refresh(){
  2. //省略部分代码
  3. this.refreshContext(context);
  4. //省略部分代码
  5. }
复制代码
AttachCode
  1.     private void refreshContext(ConfigurableApplicationContext context) {
  2.         if (this.registerShutdownHook) {
  3.             shutdownHook.registerApplicationContext(context);
  4.         }
  5.         this.refresh(context);
  6.     }
复制代码
今天的主角AttachCode
  1. @Override
  2.         public void refresh() throws BeansException, IllegalStateException {
  3.                 synchronized (this.startupShutdownMonitor) {
  4.                         StartupStep contextRefresh = this.applicationStartup.start("spring.context.refresh");
  5.                         // Prepare this context for refreshing.
  6.                         prepareRefresh();
  7.                         // Tell the subclass to refresh the internal bean factory.
  8.                         ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();
  9.                         // Prepare the bean factory for use in this context.
  10.                         prepareBeanFactory(beanFactory);
  11.                         try {
  12.                                 // Allows post-processing of the bean factory in context subclasses.
  13.                                 postProcessBeanFactory(beanFactory);
  14.                                 StartupStep beanPostProcess = this.applicationStartup.start("spring.context.beans.post-process");
  15.                                 // Invoke factory processors registered as beans in the context.
  16.                                 invokeBeanFactoryPostProcessors(beanFactory);
  17.                                 // Register bean processors that intercept bean creation.
  18.                                 registerBeanPostProcessors(beanFactory);
  19.                                 beanPostProcess.end();
  20.                                 // Initialize message source for this context.
  21.                                 initMessageSource();
  22.                                 // Initialize event multicaster for this context.
  23.                                 initApplicationEventMulticaster();
  24.                                 // Initialize other special beans in specific context subclasses.
  25.                                 onRefresh();
  26.                                 // Check for listener beans and register them.
  27.                                 registerListeners();
  28.                                 // Instantiate all remaining (non-lazy-init) singletons.
  29.                                 finishBeanFactoryInitialization(beanFactory);
  30.                                 // Last step: publish corresponding event.
  31.                                 finishRefresh();
  32.                         }
  33.                         catch (BeansException ex) {
  34.                                 if (logger.isWarnEnabled()) {
  35.                                         logger.warn("Exception encountered during context initialization - " +
  36.                                                         "cancelling refresh attempt: " + ex);
  37.                                 }
  38.                                 // Destroy already created singletons to avoid dangling resources.
  39.                                 destroyBeans();
  40.                                 // Reset 'active' flag.
  41.                                 cancelRefresh(ex);
  42.                                 // Propagate exception to caller.
  43.                                 throw ex;
  44.                         }
  45.                         finally {
  46.                                 // Reset common introspection caches in Spring's core, since we
  47.                                 // might not ever need metadata for singleton beans anymore...
  48.                                 resetCommonCaches();
  49.                                 contextRefresh.end();
  50.                         }
  51.                 }
  52.         }
复制代码
今天我们先到这里
本文由 idea的插件"AnNote"协助完成,是一款源码学习笔记,不但可以或许帮助我们做笔记、分享笔记还可以学习学霸的笔记。欢迎大家试用
本文由博客群发一文多发等运营工具平台 OpenWrite 发布

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

千千梦丶琪

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表