闲情居|Spring Boot源码解析——Spring Boot系列( 三 )


④ org.springframework.boot.SpringApplication#refresh
⑤ org.springframework.context.support.AbstractApplicationContext#refresh
到了AbstractApplicationContext#refresh方法 , 之前讲过Spring IoC源码解析讲过该方法的12大步 , 这里就不细说 , 详细可以参考:Spring系列(三):Spring IoC源码解析 , 里面说过有一步就是onRefresh() , 这个方法默认是空的 , 由子类根据自身需要去实现
⑥ org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext#onRefresh
该onRefresh方法分2步
第一:super.onRefresh(); 调用父类的onRefresh()
第二:createWebServer();创建Web服务 , 很重要 , 很重要 , 很重要!!!
⑦ createWebServer()方法
第一:ServletContext servletContext = getServletContext(); 获取Servlet的上下文
第二:ServletWebServerFactory factory = getWebServerFactory();获取Tomcat的Web服务工厂
第三:this.webServer = factory.getWebServer(getSelfInitializer()); 创建一个Web服务器
闲情居|Spring Boot源码解析——Spring Boot系列⑧ TomcatServletWebServerFactory#getWebServer()方法 , 主要用于创建一个Tomcat Web容器
闲情居|Spring Boot源码解析——Spring Boot系列到此我们知道Spring Boot的启动通过Spring IoC的refresh中的的onRefresh()带动了Tomcat的启动 , 跟我们之前我们学Spring Mvc的时候刚好相反 , Spring Mvc的是Tomcat的启动带动了Spring容器的启动;
三、普通Web工程启动1、普通的web工程 , 我们找到web.xml , 会发现都配置了如下的加载Spring的配置 。
闲情居|Spring Boot源码解析——Spring Boot系列2、Tomcat启动的时候会调用该上下文加载的的监听器的contextInitialized方法 , 我们进入到该方法:
闲情居|Spring Boot源码解析——Spring Boot系列3、进入初始化Web应用上下文initWebApplicationContext方法中:

  • this.context = createWebApplicationContext(servletContext);
  • configureAndRefreshWebApplicationContext(cwac, servletContext);
4、进去到configureAndRefreshWebApplicationContext(cwac, servletContext)方法中:
闲情居|Spring Boot源码解析——Spring Boot系列5、进入到refresh方法实际就到了org.springframework.context.support.AbstractApplicationContext#refresh的方法这个方法很熟悉了 , Spring IoC的refresh的12大步;
四、Spring Boot启动流程图
闲情居|Spring Boot源码解析——Spring Boot系列分类: spring boot
最后 , 咱给小编:
1. 点赞+关注
2. 点头像关注后多多评论 , 转发给有需要的朋友 。
谢谢!!


推荐阅读