Servlet容器自动配置类为ServletWebServerFactoryAutoConfiguration

SpringBoot复习(39)Servlet容器的自动配置原理_tomcat


可以看到通过@Import注解导入了三个配置类:

SpringBoot复习(39)Servlet容器的自动配置原理_servlet_02


SpringBoot复习(39)Servlet容器的自动配置原理_spring_03


SpringBoot复习(39)Servlet容器的自动配置原理_spring_04


通过这个这三个配置类可以看出,它们都使用了@ConditionalOnClass注解,当类路径存在tomcat相关的类时,会配置一个TomcatServletWebServerFactory类型的bean, 当类路径存在jetty相关的类时,会配置一个JettyServletWebServerFactory 类型的bean, 当类路径存在undertow相关的类时,会配置一个UndertowServletWebServerFactory类型的bean。

这样就完成了根据pom.xml中引入了不同的依赖来决定使用哪种Servlet容器。

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
        </dependency>
<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-undertow</artifactId>
        </dependency>
<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jetty</artifactId>
        </dependency>