ssss我们依旧以SpringBoot官网的官方文档为准学习。
ssdsdsdsdsdssSpring Boot Features ==> 7.4.3. The ServletWebServerApplicationContext
文档介绍
sdsdsUnder 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.
默认支持的webServer:
sdsdsTomcat, Jetty, or Undertow
sdsdsServletWebServerApplicationContext 容器启动寻找ServletWebServerFactory 并引导创建服务器
原理
sdsd1.如果SpringBoot应用启动发现当前是Web应用。则自动会导入(web场景包-)tomcat包。
sdsd2.web应用会创建一个web版的ioc容器 ServletWebServerApplicationContext
sdsdsd
sdsd3.ServletWebServerApplicationContext 启动的时候寻找 ServletWebServerFactory(Servlet 的web服务器工厂 ==> Servlet 的web服务器)
sdsd4.SpringBoot底层默认有很多的WebServer工厂,常用的有 TomcatServletWebServerFactory, JettyServletWebServerFactory, or UndertowServletWebServerFactory
sdsd5.底层直接会有一个自动配置类。ServletWebServerFactoryAutoConfiguration
sdsd6.ServletWebServerFactoryAutoConfiguration导入了ServletWebServerFactoryConfiguration(配置类)(通过上图也可以发现,ServletWebServerFactoryAutoConfiguration需要@import,也就是提前要配置好后面三个类,分别是三个服务器的工厂)
ssddsd
sdsd7.ServletWebServerFactoryConfiguration 配置类 根据动态判断系统中到底导入了那个Web服务器的包(如上图)。(默认是web-starter导入tomcat包),容器中就有TomcatServletWebServerFactory.
sdsd8.TomcatServletWebServerFactory 创建出Tomcat服务器并启动;TomcatWebServer 的构造器拥有初始化方法initialize—this.tomcat.start();
sdsdsdsds
sdsd9.内嵌服务器,就是手动把启动服务器的代码调用(tomcat核心jar包存在)
切换嵌入式Servlet容器(服务器)
<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>
sdsd很简单,除去原有的tomcat的jar包,然后添加别的服务器jar包。(官方文档”Using Spring Boot“查找)
定制Servlet服务器
sdsd官网文档:Spring Boot Features ==> 7.4.4. Customizing Embedded Servlet Containers
sdsd方法一:实现 WebServerFactoryCustomizer< ConfigurableServletWebServerFactory >
sddssd 把配置文件的值和 ServletWebServerFactory 进行绑定
server.servlet.session.timeout=60m
sdsd方法二:直接自定义 ConfigurableServletWebServerFactory
ssdsddsdds
sdsd方法三:实现 WebServerFactoryCustomizer< ConfigurableServletWebServerFactory > (它会把配置文件的值和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);
}
}
sdsdsdsdsddsddd
sddssd【注】:在SpringBoot中见到 xxxxxCustomizer :定制化器,它可以改变xxxx的默认规则
定制化原理
sdsd原理分析套路:某一场景starter —》 xxxxAutoConfiguration ----》 导入xxx组件 ----》 绑定xxxProperties ----》 绑定配置文件项(我们只需要修改配置文件项)
定制化的常见方式:
s sd 1、修改配置文件(推荐,这里只能说明挥用);
s sd 2、xxxxxCustomizer (通过定制化器来改变一些参数配置项等);
sssd3、编写自定义的配置类 xxxConfiguration;+ @Bean替换、增加容器中默认组件,比如:视图解析器 (也是常见的方法) 。
s ds 4、Web应用,自定义Web功能:==> 官方文档:7.1.1. Spring MVC Auto-configuration
sds ds 4.1. 编写一个配置类实现 WebMvcConfigurer 即可定制化web功能;+ @Bean给容器中再扩展一些组件(必须会)
sds ds4.2(其实和4.1一样). 修改 RequestMappingHandlerMapping, RequestMappingHandlerAdapter, or ExceptionHandlerExceptionResolver ,在WebMvcConfigurer 实现类中使用 WebMvcRegistrations + @Bean 实现 (但是这个需要对SpringBoot掌握的很透彻)
sdsadasds d
sds ds4.3. @EnableWebMvc + WebMvcConfigurer —— @Bean 可以全面接管SpringMVC,所有规则全部自己重新配置; 实现定制和扩展功能(功能更加严格)
sdsdsadasdsd
sds原理:
sds ds1.WebMvcAutoConfiguration 默认的SpringMVC的自动配置功能类。静态资源、欢迎页…
sds ds2.一旦使用 @EnableWebMvc ,会 @Import(DelegatingWebMvcConfiguration.class)
导入这个组件,该组件的作用只保证了SpringMVC的最基本的使用:
sdsds ds(1)把所有系统中的 WebMvcConfigurer 拿过来。所有功能的定制都是这些WebMvcConfigurer 合起来一起生效
sddssdasdd
ssdsdsdsdddssdasdd
sddsdsdsdsdsdsdssdasdd
sdsds ds(2)自动配置了一些非常底层的组件。RequestMappingHandlerMapping、这些组件依赖的组件都是从容器中获取
public RequestMappingHandlerMapping requestMappingHandlerMapping(
@Qualifier("mvcContentNegotiationManager") ContentNegotiationManager contentNegotiationManager,
@Qualifier("mvcConversionService") FormattingConversionService conversionService,
@Qualifier("mvcResourceUrlProvider") ResourceUrlProvider resourceUrlProvider) {...}
sdsddssdssddsdss ds
sds ds3.WebMvcAutoConfiguration 里面的配置要能生效必须满足:@ConditionalOnMissingBean(WebMvcConfigurationSupport.class)
sds ds
sds ds而WebMvcConfigurationSupport
被DelegatingWebMvcConfiguration
继承
ssdsdds ds
sds ds总结:@EnableWebMvc 引入DelegatingWebMvcConfiguration就相当于引入了WebMvcConfigurationSupport,从而导致了 WebMvcAutoConfiguration 没有生效。 因此,当使用了@EnableWebMvc,我们就需要配置WebMvcAutoConfiguration中的内容,因为他们都失效了 。