springboot启动时,会自动识别出当前环境是否是web环境还是非web环境。

默认的web环境的context(DEFAULT_WEB_CONTEXT_CLASS):org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext
默认的非web环境的context(DEFAULT_CONTEXT_CLASS):org.springframework.context.annotation.AnnotationConfigApplicationContext 以下代码为springboot启动类springApplication创建context代码:
ConfigurableApplicationContext org.springframework.boot.SpringApplication.createApplicationContext():

    /**
* Strategy method used to create the {@link ApplicationContext}. By default this
* method will respect any explicitly set application context or application context
* class before falling back to a suitable default.
* @return the application context (not yet refreshed)
* @see #setApplicationContextClass(Class)
*/
protected ConfigurableApplicationContext createApplicationContext() {
Class<?> contextClass = this.applicationContextClass;
if (contextClass == null) {
try {
switch (this.webApplicationType) {
case SERVLET:
contextClass = Class.forName(DEFAULT_WEB_CONTEXT_CLASS);
break;
case REACTIVE:
contextClass = Class.forName(DEFAULT_REACTIVE_WEB_CONTEXT_CLASS);
break;
default:
contextClass = Class.forName(DEFAULT_CONTEXT_CLASS);
}
}
catch (ClassNotFoundException ex) {
throw new IllegalStateException(
"Unable create a default ApplicationContext, "
+ "please specify an ApplicationContextClass",
ex);
}
}
return (ConfigurableApplicationContext) BeanUtils.instantiateClass(contextClass);
}

那么我们来详细看下默认的web环境的AnnotationConfigServletWebServerApplicationContext的具体实现:

public class AnnotationConfigServletWebServerApplicationContext extends ServletWebServerApplicationContext implements AnnotationConfigRegistry

发现AnnotationConfigServletWebServerApplicationContext继承了org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext,

我们看AnnotationConfigServletWebServerApplicationContext的代码,发现并没有和webServer(tomcat,jetty....)相关的代码。那么我们继续来看下父类ServletWebServerApplicationContext的实现:

ServletWebServerApplicationContext:发现这个类是boot包下的,继承了GenericWebApplicationContext是spring-web这个jar包,推断springboot嵌入webServer的逻辑很有可能是在这个ServletWebServerApplicationContext

类结构:public class ServletWebServerApplicationContext extends GenericWebApplicationContext implements ConfigurableWebServerApplicationContext

我们终于在org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext看到了和webServer相关的代码:

    @Override
protected void onRefresh() {
super.onRefresh();
try {
createWebServer();
}
catch (Throwable ex) {
throw new ApplicationContextException("Unable to start web server", ex);
}
}
@Override
protected void finishRefresh() {
super.finishRefresh();
WebServer webServer = startWebServer();
if (webServer != null) {
publishEvent(new ServletWebServerInitializedEvent(webServer, this));
}
}

其中onRefresh方法会在void org.springframework.context.support.AbstractApplicationContext.refresh() 中会在context注册BeanFactoryPostProcessor、执行BeanFactoryPostProcessor、注册BeanPostProcessor之后调用,

finishRefresh方法会在void org.springframework.context.support.AbstractApplicationContext.finishRefresh()中会在context完成各种初始化(初始化bean)后被调用到,从而在context启动完成后,启动WebServer(tomcat、jetty)...

接下来我们看下创建WebServer和启动WebServer的逻辑:

private void createWebServer() {
WebServer webServer = this.webServer;
ServletContext servletContext = getServletContext();
if (webServer == null && servletContext == null) {
ServletWebServerFactory factory = getWebServerFactory();
this.webServer = factory.getWebServer(getSelfInitializer());
}
else if (servletContext != null) {
try {
getSelfInitializer().onStartup(servletContext);
}
catch (ServletException ex) {
throw new ApplicationContextException("Cannot initialize servlet context",
ex);
}
}
initPropertySources();
}
private WebServer startWebServer() {
WebServer webServer = this.webServer;
if (webServer != null) {
webServer.start();
}
return webServer;
}

springboot中如何启动tomcat的更多相关文章

  1. Linux 中指定启动 tomcat 的 jdk 版本

    环境: RHEL6.5. tomcat8.5.jdk1.8.0_181 修改 catalina.sh.setclasspath.sh 文件 进入目录 $ cd /data01/server/apach ...

  2. IDEA中debug启动tomcat报错。Error running t8:Unable to open debugger port(127.0.0.1:49225):java.net.BindException"Address alread in use:JVM_Bind"

    解决办法: 1,如下图打开项目配置的tomcat的“Edit Configurations...” 2,打开“Startup/Connection”--------"Debug"- ...

  3. SpringBoot启动tomcat源码解读

    一.SpringBoot自动拉起Tomcat 原文链接:http://www.studyshare.cn/blog-front/blog/details/1136 SpringBoot框架是当前比较流 ...

  4. 在Eclipse中启动tomcat后访问tomcat首页时出现404

    在Eclipse中配置好tomcat后,把一个web项目发布到tomcat上去,当使用http://localhost:8080访问tomcat首页时出现404错误,但可以正常访问web页面,然而当在 ...

  5. 通过Eclipse3.1以上启动Tomcat访问不到tomcat管理界面的问题(转载)

    通过Eclipse插件启动Tomcat的问题 默认分类   2009-10-23 15:54   阅读118   评论0   字号: 大  中  小 目前在通过Eclipse中插件启动Tomcat时遇 ...

  6. 启动tomcat内存溢出

    在运行项目的过程中,启动tomcat内存溢出.查阅了一些解决办法,总结出来留个笔记. 1.使用Myeclipse2014+tomcat 7 ,在MyEclipse中将项目部署到Tomcat下,启动to ...

  7. SpringBoot应用部署到Tomcat中无法启动问题

    SpringBoot应用部署到Tomcat中无法启动问题   背景 最近公司在做一些内部的小型Web应用时, 为了提高开发效率决定使用SpringBoot, 这货自带Servlet容器, 你在开发We ...

  8. SpringBoot应用部署到Tomcat中无法启动问题(初识)

    参考http://blog.csdn.net/asdfsfsdgdfgh/article/details/52127562 背景 最近公司在做一些内部的小型Web应用时, 为了提高开发效率决定使用Sp ...

  9. SpringBoot中的Tomcat是如何启动的?

    <dependency>    <groupId>org.springframework.boot</groupId>    <artifactId>s ...

随机推荐

  1. (三)创建基于maven的javaFX+springboot项目创建

    创建基于maven的javaFx+springboot项目有两种方式,第一种为通过非编码的方式来设计UI集成springboot:第二种为分离用户界面(UI)和后端逻辑集成springboot,其中用 ...

  2. 10 select、poll以及epoll

    IO复用:为了解释这个名词,首先来理解下复用这个概念,复用也就是共用的意思,这样理解还是有些抽象,为此,咱们来理解下复用在通信领域的使用, 在通信领域中为了充分利用网络连接的物理介质,往往在同一条网络 ...

  3. 【JavaScript】js中的call方法

    moziila官方文档链接:https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Funct ...

  4. http请求204

    项目中发现一个奇怪的问题,请求的时候同一个接口有两个请求,而且有一个状态为204,有一个为200 在网上查看资料后得知,是因为跨域而引起的,OPTIONS是一种“预检请求”,浏览器在处理跨域访问的请求 ...

  5. TCP保活定时器

    TCP有Keepalive功能,它和HTTP的Keepalive功能目的不一样.TCP服务器希望知道客户端是否崩溃.重新启动或者中间路由不通.保活定时器就提供这种功能. 在进一步介绍TCP的保活定时器 ...

  6. multiple类型的select option在django后台如何取值

    之前前端的select都是单选类型,在新的场景中允许用户选择多个条件, 前端的代码如下: <form action="{% url 'info:result-list' %}" ...

  7. cmake 判断操作系统平台

    转载自 cmake 判断操作系统平台 MESSAGE(STATUS "operation system is ${CMAKE_SYSTEM}") IF (CMAKE_SYSTEM_ ...

  8. electron api sendInputEvent 源码

    electron-master\electron-master\shell\browser\api\atom_api_web_contents.cc // Copyright (c) 2014 Git ...

  9. spring-02

    spring-02 1.谈谈你对 Spring 的理解 Spring 是一个开源框架,为简化企业级应用开发而生.Spring 可以是使简单的 JavaBean 实现以前只有 EJB 才能实现的功能.S ...

  10. except用法

    #!/usr/bin/expect set timeout 20 spawn ssh -l root 172.25.254.102 expect "(yes/no)?" send ...