Tomcat如何在SpringBoot中使用

  

Tomcat如何在SpringBoot中使用?很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。


导入依赖:

& lt; dependency>   & lt;才能groupId> org.springframework.boot</groupId>   & lt;才能artifactId> spring-boot-starter-web</artifactId>   & lt;/dependency>

项目启动:

@SpringBootApplication   public  class  Application  {   public 才能;static  void  main (String [], args), {   ,,,SpringApplication.run (Application.class, args);   ,,}   }

以上是一段常见的SpringBoot项目依赖和启动的代码。引入<代码> spring-boot-starter-web> spring-boot-starter-tomcat> @SpringBootApplication 主要实现弹簧组件扫描和自动配置。该注解是复合注解,其中<代码> @EnableAutoConfiguration 本身也是一个复合注解,包含以下内容:

@AutoConfigurationPackage   @ import (AutoConfigurationImportSelector.class)   public  @interface  EnableAutoConfiguration  {   }

这两个注解比较关键。<代码> @AutoConfigurationPackage 主要实现自动配置包,会扫描<代码> @SpringbootApplication> @ import>

Tomcat如何在SpringBoot中使用

ServletWebServerFactoryAutoConfiguration中支持好几种web容器,比如Tomcat、Jetty和Undertow。

Tomcat如何在SpringBoot中使用

而EmbeddedTomcat则是Tomcat组件相关的类,本身是一个FactoryBean,用来实例化TomcatServletWebServerFactory。此时TomcatServletWebServerFactory中就包含了创建和启动Tomcat的方法getWebServer()。

Tomcat如何在SpringBoot中使用

Tomcat组件启动

SpringBoot是在项目启动的时候才同时启动Tomcat的,很显然getWebServer()是在项目启动的过程中调用的。跟踪SpringApplication的run(),其中存在refreshContext(context),此时主要完成容器的刷新。

Tomcat如何在SpringBoot中使用

容器刷新跟踪到最后是AbstractApplicationContext中的onRefresh(),显然这是一个钩子函数,应用了模板方法,查看所有的实现方法,其中有一个ServletWebServerApplicationContext,则是当前Web容器的实现。

Tomcat如何在SpringBoot中使用

而ServletWebServerApplicationContext中主要是去获得ServletWebServerFactory对象,同时调用getWebServer创建WebServer对象。

Tomcat如何在SpringBoot中使用

此时,主要处理的是Tomcat容器对象的创建、环境配置和启动。

Tomcat如何在SpringBoot中使用

Tomcat如何在SpringBoot中使用

看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注行业资讯频道,感谢您对的支持。

Tomcat如何在SpringBoot中使用