# SpringBoot | 怎样启动tomcat以及怎样配置tomcat。

SpringBoot | 怎样启动tomcat以及默认的tomcat配置。

标签(空格分隔): springboot tomcat


因为springboot已经被大部分公司运用,所以基于springboot 来讲解tomcat。

  • springboot 怎样引入的tomcat
  • springboot 怎样创建一个tomcat实例
  • springboot 从哪里读取tomcat配置
  • springboot 中tomcat的配置详解

本文主要讲解这三个问题

springboot 怎样引入的tomcat

当我们在项目中加入

加入这个依赖后,maven会把tomcat的一些jar也加入进来。 这些jar包应该是一些开源工作者在维护着。

<dependency>
    <groupId>org.apache.tomcat.embed</groupId>
    <artifactId>tomcat-embed-core</artifactId>
    <version>8.5.31</version>
  </dependency>

上面这个依赖是springboot为我们准备好的,版本号为8.5.31,springboot的版本为:2.0.x。 这个依赖包含了tomcat的核心jar包。springboot 初始化tomcat,启动tomcat都需要这个jar。

当我们 run springboot的引导文件时,springboot 会创建WebServer,
我们跟着这个方法(createWebServer())走下去。

@Override
    protected void onRefresh() {
        super.onRefresh();
        try {
            createWebServer();
        }
        catch (Throwable ex) {
            throw new ApplicationContextException("Unable to start web server", ex);
        }
    }

这里会去获取一个webServier,这个方法里面很多引用的类都存在于上面提到的jar包中。 new Tomcat() 创建一个tomcat 实例。到这里我们已经看到springboot已经创建了一个tomcat实例。

@Override
    public WebServer getWebServer(ServletContextInitializer... initializers) {
        Tomcat tomcat = new Tomcat();
        File baseDir = (this.baseDirectory != null ? this.baseDirectory
                : createTempDir("tomcat"));
        tomcat.setBaseDir(baseDir.getAbsolutePath());
        Connector connector = new Connector(this.protocol);
        tomcat.getService().addConnector(connector);
        customizeConnector(connector);
        tomcat.setConnector(connector);
        tomcat.getHost().setAutoDeploy(false);
        configureEngine(tomcat.getEngine());
        for (Connector additionalConnector : this.additionalTomcatConnectors) {
            tomcat.getService().addConnector(additionalConnector);
        }
        prepareContext(tomcat.getHost(), initializers);
        return getTomcatWebServer(tomcat);
    }

当我们追踪到里面的类时,发现里面已经配置好了端口以及一些其他配置。
这样已经能创建一个默认的tomcat。 我们需要知道的是springboot是怎样吧appplication.property中的配置运用到tomcat实例中的。

探果网

猜你喜欢

转载自www.cnblogs.com/tamguo/p/9712416.html