
SpringBoot:在使用Web一般是基于servlet。在启动的时候默认是Tomcat。
<parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.3.12.RELEASE</version></parent><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependencies>
1. spring-boot-starter-web.pom引入依赖
<dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter</artifactId><version>2.3.12.RELEASE</version><scope>compile</scope></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-json</artifactId><version>2.3.12.RELEASE</version><scope>compile</scope></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-tomcat</artifactId><version>2.3.12.RELEASE</version><scope>compile</scope></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-web</artifactId><version>5.2.15.RELEASE</version><scope>compile</scope></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-webmvc</artifactId><version>5.2.15.RELEASE</version><scope>compile</scope></dependency></dependencies>
2.spring-boot-starter.pom引入主要依赖
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot</artifactId><version>2.3.12.RELEASE</version><scope>compile</scope></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-autoconfigure</artifactId><version>2.3.12.RELEASE</version><scope>compile</scope></dependency>
1.读取META-INF/spring-factories
参考[:https://blog.csdn.net/swg321321/article/details/125547644] 中自动读取下面文件
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\org.springframework.boot.autoconfigure.web.servlet.DispatcherServletAutoConfiguration,\org.springframework.boot.autoconfigure.web.servlet.ServletWebServerFactoryAutoConfiguration,\org.springframework.boot.autoconfigure.web.servlet.error.ErrorMvcAutoConfiguration,\org.springframework.boot.autoconfigure.web.servlet.HttpEncodingAutoConfiguration,\org.springframework.boot.autoconfigure.web.servlet.MultipartAutoConfiguration,\org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration
2. WebMvcAutoConfiguration
Web应用自动配置类
Web应用自动配置
@Configuration(proxyBeanMethods=false)@ConditionalOnWebApplication(type=Type.SERVLET)//当{}内类存在 WebMvcAutoConfiguration这个类才会被加载@ConditionalOnClass({Servlet.class,DispatcherServlet.class,WebMvcConfigurer.class})@ConditionalOnMissingBean(WebMvcConfigurationSupport.class)@AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE+10)@AutoConfigureAfter({DispatcherServletAutoConfiguration.class,TaskExecutionAutoConfiguration.class,ValidationAutoConfiguration.class})publicclassWebMvcAutoConfiguration{}
DispatcherServletAutoConfiguration
@AutoConfigureOrder(Ordered.HIGHEST_PRECEDENCE)@Configuration(proxyBeanMethods=false)@ConditionalOnWebApplication(type=Type.SERVLET)@ConditionalOnClass(DispatcherServlet.class)@AutoConfigureAfter(ServletWebServerFactoryAutoConfiguration.class)publicclassDispatcherServletAutoConfiguration{}
ServletWebServerFactoryAutoConfiguration
@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})publicclassServletWebServerFactoryAutoConfiguration{}
ServletWebServerFactoryConfiguration
在这里实现加载已经引入了对应容器的依赖
Tomcat、Jetty、Undertow
@Configuration(proxyBeanMethods=false)classServletWebServerFactoryConfiguration{@Configuration(proxyBeanMethods=false)@ConditionalOnClass({Servlet.class,Tomcat.class,UpgradeProtocol.class})@ConditionalOnMissingBean(value=ServletWebServerFactory.class, search=SearchStrategy.CURRENT)staticclassEmbeddedTomcat{@BeanTomcatServletWebServerFactorytomcatServletWebServerFactory(ObjectProvider<TomcatConnectorCustomizer> connectorCustomizers,ObjectProvider<TomcatContextCustomizer> contextCustomizers,ObjectProvider<TomcatProtocolHandlerCustomizer<?>> protocolHandlerCustomizers){TomcatServletWebServerFactory factory=newTomcatServletWebServerFactory(); factory.getTomcatConnectorCustomizers().addAll(connectorCustomizers.orderedStream().collect(Collectors.toList())); factory.getTomcatContextCustomizers().addAll(contextCustomizers.orderedStream().collect(Collectors.toList())); factory.getTomcatProtocolHandlerCustomizers().addAll(protocolHandlerCustomizers.orderedStream().collect(Collectors.toList()));return factory;}}/** * Nested configuration if Jetty is being used. */@Configuration(proxyBeanMethods=false)@ConditionalOnClass({Servlet.class,Server.class,Loader.class,WebAppContext.class})@ConditionalOnMissingBean(value=ServletWebServerFactory.class, search=SearchStrategy.CURRENT)staticclassEmbeddedJetty{@BeanJettyServletWebServerFactoryJettyServletWebServerFactory(ObjectProvider<JettyServerCustomizer> serverCustomizers){JettyServletWebServerFactory factory=newJettyServletWebServerFactory(); factory.getServerCustomizers().addAll(serverCustomizers.orderedStream().collect(Collectors.toList()));return factory;}}/** * Nested configuration if Undertow is being used. */@Configuration(proxyBeanMethods=false)@ConditionalOnClass({Servlet.class,Undertow.class,SslClientAuthMode.class})@ConditionalOnMissingBean(value=ServletWebServerFactory.class, search=SearchStrategy.CURRENT)staticclassEmbeddedUndertow{@BeanUndertowServletWebServerFactoryundertowServletWebServerFactory(ObjectProvider<UndertowDeploymentInfoCustomizer> deploymentInfoCustomizers,ObjectProvider<UndertowBuilderCustomizer> builderCustomizers){UndertowServletWebServerFactory factory=newUndertowServletWebServerFactory(); factory.getDeploymentInfoCustomizers().addAll(deploymentInfoCustomizers.orderedStream().collect(Collectors.toList())); factory.getBuilderCustomizers().addAll(builderCustomizers.orderedStream().collect(Collectors.toList()));return factory;}@BeanUndertowServletWebServerFactoryCustomizerundertowServletWebServerFactoryCustomizer(ServerProperties serverProperties){returnnewUndertowServletWebServerFactoryCustomizer(serverProperties);}}}
配置可以参考:META-INF/spring-configuration-metadata.json
- @ConfigurationProperties(prefix = “server”, ignoreUnknownFields = true)
public class ServerProperties {} - @ConfigurationProperties(prefix = “spring.mvc”)
public class WebMvcProperties {}