11、SpringBoot2 Web开发之嵌入式容器使用及其原理

1、嵌入式容器的使用

1.1 支持的类型及其默认容器

SpringBoot支持Tomcat、Jetty、Undertow、Netty,默认容器为tomcat

1.2 如何切换容器

将tomcat容器对应starter移除,加入相应的容器starter,如下使用jetty容器

<dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-web</artifactId>
     <exclusions>
         <exclusion>
             <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-starter-tomcat</artifactId>
         </exclusion>
     </exclusions>
 </dependency>
 <dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-jetty</artifactId>
 </dependency>

1.3 如何定制容器

1.3.1 修改配置文件

通过修改server.xxxx进行修改容器配置,其中不通容器的特有属性可通过server.tomcat.xxxx或server.jettyt.xxx来进行修改

1.3.2 实现WebServerFactoryCustomizer

通过实现WebServerFactoryCustomizer ,将配置的的值与ServletWebServerFactory进行绑定,达到修改容器工厂的配置值达到修改创建的容器配置值。

1.3.3 直接自定义 ConfigurableServletWebServerFactory

2、嵌入式容器原理

2.1 各个配置容器生效原理

  • SpringBoot容器启动会自动注入ServletWebServerFactoryAutoConfiguration
@Configuration(
    proxyBeanMethods = false
)
@AutoConfigureOrder(-2147483648)
@ConditionalOnClass({ServletRequest.class})
@ConditionalOnWebApplication(
    type = Type.SERVLET
)
@EnableConfigurationProperties({ServerProperties.class})
@Import({ServletWebServerFactoryAutoConfiguration.BeanPostProcessorsRegistrar.class, EmbeddedTomcat.class, EmbeddedJetty.class, EmbeddedUndertow.class})
public class ServletWebServerFactoryAutoConfiguration {
}
  • 同时导入EmbeddedTomcat、EmbeddedJetty、EmbeddedUndertow,根据ConditionalOnClass判断是否生效
@Configuration(
    proxyBeanMethods = false
)
@ConditionalOnClass({Servlet.class, Undertow.class, SslClientAuthMode.class})
@ConditionalOnMissingBean(
    value = {ServletWebServerFactory.class},
    search = SearchStrategy.CURRENT
)
static class EmbeddedUndertow {
}

@Configuration(
    proxyBeanMethods = false
)
@ConditionalOnClass({Servlet.class, Server.class, Loader.class, WebAppContext.class})
@ConditionalOnMissingBean(
    value = {ServletWebServerFactory.class},
    search = SearchStrategy.CURRENT
)
static class EmbeddedJetty {
}

@Configuration(
    proxyBeanMethods = false
)
@ConditionalOnClass({Servlet.class, Tomcat.class, UpgradeProtocol.class})
@ConditionalOnMissingBean(
    value = {ServletWebServerFactory.class},
    search = SearchStrategy.CURRENT
)
static class EmbeddedTomcat {
}
  • 默认容器中假如的Tomcat相关的jar包,所以默认是Tomcat容器启动

2.2 容器启动原理

  • web应用会创建一个web版的ioc容器 ServletWebServerApplicationContext,并且会执行onRefresh方法
//ServletWebServerApplicationContext
protected void onRefresh() {
     super.onRefresh();
     try {
         this.createWebServer();
     } catch (Throwable var2) {
         throw new ApplicationContextException("Unable to start web server", var2);
     }
 }
  • 创建web容器
private void createWebServer() {
     WebServer webServer = this.webServer;
     ServletContext servletContext = this.getServletContext();
     if (webServer == null && servletContext == null) {
     	//根据容器中注入的ServletWebServerFactory 获取web容器工厂
         ServletWebServerFactory factory = this.getWebServerFactory();
         //根据容器工厂获取容器
         this.webServer = factory.getWebServer(new ServletContextInitializer[]{this.getSelfInitializer()});
         this.getBeanFactory().registerSingleton("webServerGracefulShutdown", new WebServerGracefulShutdownLifecycle(this.webServer));
         this.getBeanFactory().registerSingleton("webServerStartStop", new WebServerStartStopLifecycle(this, this.webServer));
     } else if (servletContext != null) {
         try {
             this.getSelfInitializer().onStartup(servletContext);
         } catch (ServletException var4) {
             throw new ApplicationContextException("Cannot initialize servlet context", var4);
         }
     }

     this.initPropertySources();
 }
  • 以tomcat工厂获取容器为例
//获取tomcat服务
public WebServer getWebServer(ServletContextInitializer... initializers) {
	//....................
   return this.getTomcatWebServer(tomcat);
}
protected TomcatWebServer getTomcatWebServer(Tomcat tomcat) {
     return new TomcatWebServer(tomcat, this.getPort() >= 0, this.getShutdown());
}

public TomcatWebServer(Tomcat tomcat, boolean autoStart, Shutdown shutdown) {
	//.................
    this.initialize();
}
//启动tomcat
private void initialize() throws WebServerException {
	//...............
	this.tomcat.start();
	//..........
}

2.3 WebServerFactoryCustomizer原理

  • WebServerFactoryCustomizerBeanPostProcessor实现了BeanPostProcessor,实例创建完成会执行postProcessBeforeInitialization方法
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
     if (bean instanceof WebServerFactory) {
         this.postProcessBeforeInitialization((WebServerFactory)bean);
     }

     return bean;
 }
  • 调用WebServerFactoryCustomizer中customize方法进行配置值的修改
private void postProcessBeforeInitialization(WebServerFactory webServerFactory) {
    ((Callbacks)LambdaSafe.callbacks(WebServerFactoryCustomizer.class, this.getCustomizers(), webServerFactory, new Object[0]).withLogger(WebServerFactoryCustomizerBeanPostProcessor.class)).invoke((customizer) -> {
        customizer.customize(webServerFactory);
    });
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值