springboot中如何启动tomcat
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代码:
/**
* 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的更多相关文章
- Linux 中指定启动 tomcat 的 jdk 版本
环境: RHEL6.5. tomcat8.5.jdk1.8.0_181 修改 catalina.sh.setclasspath.sh 文件 进入目录 $ cd /data01/server/apach ...
- 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"- ...
- SpringBoot启动tomcat源码解读
一.SpringBoot自动拉起Tomcat 原文链接:http://www.studyshare.cn/blog-front/blog/details/1136 SpringBoot框架是当前比较流 ...
- 在Eclipse中启动tomcat后访问tomcat首页时出现404
在Eclipse中配置好tomcat后,把一个web项目发布到tomcat上去,当使用http://localhost:8080访问tomcat首页时出现404错误,但可以正常访问web页面,然而当在 ...
- 通过Eclipse3.1以上启动Tomcat访问不到tomcat管理界面的问题(转载)
通过Eclipse插件启动Tomcat的问题 默认分类 2009-10-23 15:54 阅读118 评论0 字号: 大 中 小 目前在通过Eclipse中插件启动Tomcat时遇 ...
- 启动tomcat内存溢出
在运行项目的过程中,启动tomcat内存溢出.查阅了一些解决办法,总结出来留个笔记. 1.使用Myeclipse2014+tomcat 7 ,在MyEclipse中将项目部署到Tomcat下,启动to ...
- SpringBoot应用部署到Tomcat中无法启动问题
SpringBoot应用部署到Tomcat中无法启动问题 背景 最近公司在做一些内部的小型Web应用时, 为了提高开发效率决定使用SpringBoot, 这货自带Servlet容器, 你在开发We ...
- SpringBoot应用部署到Tomcat中无法启动问题(初识)
参考http://blog.csdn.net/asdfsfsdgdfgh/article/details/52127562 背景 最近公司在做一些内部的小型Web应用时, 为了提高开发效率决定使用Sp ...
- SpringBoot中的Tomcat是如何启动的?
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>s ...
随机推荐
- 今天朋友带领去谷歌ok2eat 餐厅吃免费午餐
中午去队友公司免费吃饭.朋友带我们去她的楼下吃,就算是googler真的也不是所有cafe都知道,感谢朋友带领.
- promise使用的正确方式
一开始恨不能理解下面的代码,为什么可以一直then下去,什么时候要直接return xxx,什么时候return 一个promise,什么时候用Promise.resolve() function ...
- 第二十五篇 jQuery 学习7 获取并设置 CSS 类
jQuery 学习7 获取并设置 CSS 类 jQuery动态控制页面,那么什么是动态呢?我们就说一下静态,静态几乎又纯html+css完成,就是刷新页面之后,不会再出现什么变动,一个实打实的静态 ...
- OSCP-FristiLeaks
环境搭建 靶机下载: https://www.vulnhub.com/entry/fristileaks-13,133/ 安装:直接用virtualbox打开 网络桥接 找到靶机IP 虚拟机启动就显示 ...
- Java学习笔记【五、字符串】
String类 11种构造,不一一列举 常用方法 s.length() 返回字符串长度 s1.contact(s2) 连接s1.s2 String.format("aaa %f bbb %d ...
- 如何解决Win10系统更新显示0x80080300代码的错误?
Win10系统自推出以来就不断的在完善更新,其越来越丰富的功能也吸引了越来越多的用户.好系统Win10系统:https://www.vkebao.com/os/index_2.html但最近有用户反映 ...
- Linux工具之sar
1.sar简介 sar(System Activity Reporter 系统活动情况报告)是目前 Linux 上最为全面的系统性能分析工具之一,可以从多方面对系统的活动进行报告, 包括:文件的读写情 ...
- Hadoop_04_Hadoop 的HDFS客户端shell命令
1.Hdfs shell客户端命令操作: 1.1.查看命令列表:hadoop fs 帮助如下: Usage: hadoop fs [generic options] [-appendToFile &l ...
- [AWS - EC2]如何使用 PuTTY 连接到 Amazon Linux 2 实例。How to Connect Amazon Linux 2 Instance from Windows Using PuTTY
1. 下载 PuTTY 2. 安装到任意目录 3. 打开 PuTTYgen (注意不是 PuTTY), 如图: 选择 RSA , 点击 Load. 找到从 AWS 创建实例的时候生成的 key,要选择 ...
- python的索引与切片和元祖
'''索引: 1.索引从0开始 2.末尾元素为 -1 3.能被for循环,有序的数据集合 切片: 1.顾头不顾尾 2.a = "123abcdfg" print(a[0::2]) ...