Java --- springboot3之嵌入式容器原理

目录

一、嵌入式容器

1.1、自动配置原理

1.2、相关操作

二、切换服务器


一、嵌入式容器

Servlet容器:管理、运行Servlet组件(Servlet、Filter、Listener)的环境,一般指服务器

1.1、自动配置原理

1、SpringBoot 默认嵌入Tomcat作为Servlet容器。

2、自动配置类ServletWebServerFactoryAutoConfiguration,EmbeddedWebServerFactoryCustomizerAutoConfiguration

@AutoConfiguration(after = SslAutoConfiguration.class)
@AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE)
@ConditionalOnClass(ServletRequest.class)
@ConditionalOnWebApplication(type = Type.SERVLET)
@EnableConfigurationProperties(ServerProperties.class)
@Import({ ServletWebServerFactoryAutoConfiguration.BeanPostProcessorsRegistrar.class,
		ServletWebServerFactoryConfiguration.EmbeddedTomcat.class,
		ServletWebServerFactoryConfiguration.EmbeddedJetty.class,
		ServletWebServerFactoryConfiguration.EmbeddedUndertow.class })
public class ServletWebServerFactoryAutoConfiguration {}

①、ServletWebServerFactoryAutoConfiguration 自动配置了嵌入式容器场景

②、绑定了ServerProperties配置类,所有和服务器有关的配置 server。

Java --- springboot3之嵌入式容器原理_第1张图片

 ③、ServletWebServerFactoryAutoConfiguration 导入了 嵌入式的三大服务器 TomcatJettyUndertow。

 、导入 TomcatJettyUndertow 都有条件注解。系统中有这个类才行(也就是导了包)。

   、默认 Tomcat配置生效。给容器中放 TomcatServletWebServerFactory。

   、都给容器中 ServletWebServerFactory放了一个 web服务器工厂(造web服务器的)

Java --- springboot3之嵌入式容器原理_第2张图片

  、web服务器工厂 都有一个功能,该类的getWebServer获取web服务器。

Java --- springboot3之嵌入式容器原理_第3张图片

 、TomcatServletWebServerFactory 创建了 tomcat对象。

④、ServletWebServerApplicationContext ioc容器,启动的时候会调用创建web服务器。

Java --- springboot3之嵌入式容器原理_第4张图片

⑤、Spring容器刷新(启动)的时候,会预留一个时机,刷新子容器。onRefresh()

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

⑥、refresh() 容器刷新 十二大步的刷新子容器会调用 onRefresh()。

总结:

Web场景的Spring容器启动,在onRefresh的时候,会调用创建web服务器的方法。

Web服务器的创建是通过WebServerFactory搞定的。容器中又会根据导了什么包条件注解,启动相关的 服务器配置,默认EmbeddedTomcat会给容器中放一个 TomcatServletWebServerFactory,导致项目启动,自动创建出Tomcat。

1.2、相关操作

1、修改server下的相关配置就可以修改服务器参数。

2、通过给容器中放一个ServletWebServerFactory,来禁用掉SpringBoot默认放的服务器工厂,实现自定义嵌入任意服务器

二、切换服务器


    3.1.0


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



    org.springframework.boot
    spring-boot-starter-jetty

切换服务器本身意义不大,并不能显著提高程序性能

你可能感兴趣的:(springboot3,servlet,spring,java)