文章目录
路还在继续,梦还在期许
1、切换嵌入式Servlet容器
1.1、默认支持的webServer
TomcatWebServer
JettyWebServer
UndertowWebServer
ServletWebServerApplicationContext 容器启动寻找ServletWebServerFactory 并引导创建服务器。
1.2、切换服务器
想要切换服务器,只需要导入服务器对应的场景启动器,默认服务器是web场景导入的。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions> <!--让web场景排除默认Tomcat依赖-->
<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-undertow</artifactId>
</dependency>
2、原理
创建spring boot 应用,无需在外置部署服务器,应用内置了服务器,应用启动,内置服务器就会自动启动,spring boot 默认启动的服务器就是 Tomcat。
2.1、ServletWebServerApplicationContext
位置:org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext
SpringBoot应用启动发现当前是Web应用,因为导入web场景包-导入tomcat,判断出是web应用会创建一个web版的ioc容器 ServletWebServerApplicationContext。
2.2、作用
ServletWebServerApplicationContext 会在启动的时候寻找 ServletWebServerFactory。(Servlet 的web服务器工厂—> Servlet 的web服务器)
SpringBoot底层默认有很多的WebServer工厂。
TomcatServletWebServerFactory
JettyServletWebServerFactory
UndertowServletWebServerFactory
2.3、ServletWebServerFactoryAutoConfiguration
位置:org.springframework.boot.autoconfigure.web.servlet.ServletWebServerFactoryAutoConfiguration
底层的一个自动配置类,ServletWebServerFactoryAutoConfiguration,用来配置WebServer工厂。
2.4、作用
@Configuration(proxyBeanMethods = false)
@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导入了ServletWebServerFactoryConfiguration(配置类)
2.5、ServletWebServerFactoryConfiguration 配置类
内部有三个web服务器工厂,tomcatServletWebServerFactory、JettyServletWebServerFactory、undertowServletWebServerFactory。
根据动态判断系统中到底导入了哪个Web服务器的包。(默认是web-starter导入tomcat包),容器中就有TomcatServletWebServerFactory。
2.6、web服务器工厂作用
TomcatServletWebServerFactory 创建出Tomcat服务器并启动。
TomcatWebServer 的构造器拥有初始化方法:initialize—this.tomcat.start();
内嵌服务器,就是手动把启动服务器的代码调用。(tomcat核心jar包存在)
3、定制Servlet容器
3.1、方式一:修改配置文件
修改配置文件 server.xxx
3.2、方式二:放入自定义ServletWeb容器工厂
在配置类中放入自定义 ConfigurableServletWebServerFactory
@Bean
public ConfigurableServletWebServerFactory webServerFactory(){
TomcatServletWebServerFactory factory = new TomcatServletWebServerFactory();
factory.setPort(9999);
return factory;
}
3.3、方式三:ServletWeb容器工厂定制化器
可以后置的修改一些ServletWeb容器工厂的规则。
xxxxxCustomizer:定制化器,可以改变xxxx的默认规则
把配置文件的值和ServletWebServerFactory 进行绑定。
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.boot.web.servlet.server.ConfigurableServletWebServerFactory;
import org.springframework.stereotype.Component;
@Component
public class CustomizationBean implements WebServerFactoryCustomizer<ConfigurableServletWebServerFactory> {
@Override
public void customize(ConfigurableServletWebServerFactory server) {
server.setPort(9000);
}
}