网站建设与管理课程总结,免费拓客软件排行榜,帮人推广注册app的平台,手机营销策划方案文章目录 1、切换嵌入式Servlet容器1.1、默认支持的webServer1.2、切换服务器 2、原理2.1、ServletWebServerApplicationContext2.2、作用2.3、ServletWebServerFactoryAutoConfiguration2.4、作用2.5、ServletWebServerFactoryConfiguration 配置类2.6、web服务器工厂作用 3、… 文章目录 1、切换嵌入式Servlet容器1.1、默认支持的webServer1.2、切换服务器 2、原理2.1、ServletWebServerApplicationContext2.2、作用2.3、ServletWebServerFactoryAutoConfiguration2.4、作用2.5、ServletWebServerFactoryConfiguration 配置类2.6、web服务器工厂作用 3、定制Servlet容器3.1、方式一修改配置文件3.2、方式二放入自定义ServletWeb容器工厂3.3、方式三ServletWeb容器工厂定制化器 【尚硅谷】SpringBoot2零基础入门教程-讲师雷丰阳 笔记 路还在继续梦还在期许 1、切换嵌入式Servlet容器
1.1、默认支持的webServer
TomcatWebServer JettyWebServer UndertowWebServer
ServletWebServerApplicationContext 容器启动寻找ServletWebServerFactory 并引导创建服务器。 1.2、切换服务器
想要切换服务器只需要导入服务器对应的场景启动器默认服务器是web场景导入的。
dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-web/artifactIdexclusions !--让web场景排除默认Tomcat依赖--exclusiongroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-tomcat/artifactId/exclusion/exclusions
/dependency
!--加入其它服务器场景--
dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-undertow/artifactId
/dependency2、原理
创建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 WebServerFactoryCustomizerConfigurableServletWebServerFactory {Overridepublic void customize(ConfigurableServletWebServerFactory server) {server.setPort(9000);}}