栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

springboot的web服务器

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

springboot的web服务器

我们知道,springboot是spring-mvc的整合,其中一项优点是内嵌服务器。但是,他并非一定要使用内嵌服务器,springboot也提供了外部部署的选项。

springboot启动

内嵌服务器外部服务器

内嵌服务器

首先摘入官网的一段话:

Under the hood, Spring Boot uses a different type of ApplicationContext for embedded servlet container support. The ServletWebServerApplicationContext is a special type of WebApplicationContext that bootstraps itself by searching for a single ServletWebServerFactory bean. Usually a TomcatServletWebServerFactory, JettyServletWebServerFactory, or UndertowServletWebServerFactory has been auto-configured.

使用ServletWebServerApplicationContext可以发现服务器。

那么这些服务器在哪里配置呢?按照springboot的套路,一定是在自动配置类中:


他这里有tomcat,jetty,undertow几个选项。


因为我导入了web场景启动器:

 
        org.springframework.boot
        spring-boot-starter-web
    

所以自带tomcat的包。

所以我们的容器中就会有TomcatServletWebServerFactory。


那么,内嵌tomcat是如何启动的呢?我这里截几张图表示重要的点:


内嵌容器一定是走main方法的。

创建容器和刷新容器。我们进入refreshContext方法。



进入getWebServerFactory。


他是按类型去容器拿的,当然,我们容器里有TomcatServletWebServerFactory,他就是个ServletWebServerFactory。



再进入getSelfInitializer。


他返回的是这么一个东西:

@FunctionalInterface
public interface ServletContextInitializer {

	
	void onStartup(ServletContext servletContext) throws ServletException;

}

他接受一个参数,不返回值,像Consumer一样。这时是不会调用的,要等到调用ServletContextInitializer类型的onStartup方法才会将ServletContext传进来,传给selfInitialize使用。

ServletContextInitializer这个东西相当于是spring自己来接管servlet的。


getWebServer这里我们把tomcat给new出来了。


然后走服务器的初始化。


此时tomcat启动。


然后把初始化器丢进去,走ServletContextInitializer的onStartup方法。


这是从IOC容器中取组件构建servlet环境。

外部服务器

使用外部服务器,比如tomcat,该怎么做呢?

首先,改造我们的代码:

    war
    springboot-context

    
        org.springframework.boot
        spring-boot-starter-web
        
            
                org.springframework.boot
                spring-boot-starter-tomcat
            
        
    

    
        javax.servlet
        javax.servlet-api
        3.1.0
        provided
    

    
        
            
                maven-war-plugin
                3.0.0
            
        
    

首先要求打成war包,然后去掉自带的tomcat,加入javax包,以及一个打包工具。

@SpringBootApplication
public class MyStarter extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        return builder.sources(MyStarter.class);
    }
}

主配置类去掉main方法(那是走内嵌服务器的逻辑),继承SpringBootServletInitializer,重写configure方法,把主配置类丢进去。


打成war包后丢到webapp目录,运行tomcat就能启动程序了。

关键是,为什么?

我们问的其实是:tomcat怎么发现我们的程序入口的(这里是MyStarter)?

MyStarter继承了一个SpringBootServletInitializer,这又是什么?

public abstract class SpringBootServletInitializer
 implements WebApplicationInitializer

他是一个WebApplicationInitializer。

那WebApplicationInitializer又是被谁发现的(或者说调用的)?

有这么一个重要的类:

@HandlesTypes(WebApplicationInitializer.class)
public class SpringServletContainerInitializer 
implements ServletContainerInitializer

他的注释很多。

大意是,实现了servlet3.0规范的服务器(我用的是tomcat8,那是实现了的)会去meta-INF/services/ 下面找一个ServletContainerInitializer(SPI机制)。

我们有吗?有的。

文件的内容是:

org.springframework.web.SpringServletContainerInitializer

服务器看你注解里传的什么:

@HandlesTypes(WebApplicationInitializer.class)

是WebApplicationInitializer。然后他把所有的WebApplicationInitializer给你整来,连着ServletContext一起丢给你:

@Override
	public void onStartup(@Nullable Set> 
	webAppInitializerClasses, ServletContext servletContext)
			throws ServletException 


然后循环调用WebApplicationInitializer的onStartup方法。


于是就会走到SpringBootServletInitializer的onStartup方法,springboot由此便可以启动了。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/735065.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号