In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly shows you "how to use SpringBoot built-in web server", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "how to use SpringBoot built-in web server" this article.
1. SpringBoot default web server?
The default web server used in SpringBoot is Tomcat, and you can start with the source code to understand why it is Tomcat.
The configuration of the web server is also found in the automatic configuration. After learning the knowledge of SpringBoot automatic configuration of WebMVC, it can be inferred that the configuration of the Web server should also be carried out in an automatic configuration class, so you can go to the / META-INF/spring.factories file to find the automatic configuration of WebMVC. In this automatic configuration, you can indirectly find the configuration of the Web server.
Org.springframework.boot.autoconfigure.web.servlet.ServletWebServerFactoryAutoConfiguration,\
Find the Web server autoconfiguration class under this path in the directory of the SpringBoot package above.
The automatic configuration class of the Web server, we can see that this configuration class supports three kinds of web servers (Tomcat,Jetty,Undertow). The specific server to be configured is determined by ServletWebServerFactoryConfiguration, and an order is defined here, which is Tomcat- > Jetty- > Undertow.
What kind of server should I choose? Look at ServletWebServerFactoryConfiguration.
In this web server factory configuration class, the above three servers are defined:
Definition of Tomcat: determine whether the dependencies Servlet.class, Tomcat.class, and UpgradeProtocol.class required by Tomcat are introduced into the environment, and users do not configure the Web server themselves (for example, manually configure the web server by implementing the ServletWebServerFactory interface), then the Tomcat server will take effect.
Definition of Jetty: the required dependencies are Servlet.class, Server.class, Loader.class, WebAppContext.class
Definition of Undertow: the required dependencies are Servlet.class, Undertow.class, SslClientAuthMode.class
So the question is, SpringBoot, if you have all of these, how do you choose? Configure classes from ServletWebServerFactoryAutoConfiguration
@ Import ({ServletWebServerFactoryAutoConfiguration.BeanPostProcessorsRegistrar.class, ServletWebServerFactoryConfiguration.EmbeddedTomcat.class, ServletWebServerFactoryConfiguration.EmbeddedJetty.class, ServletWebServerFactoryConfiguration.EmbeddedUndertow.class}) public class ServletWebServerFactoryAutoConfiguration
You can see from @ Import that there is an order defined here, which is Tomcat- > Jetty- > Undertow, which means that when there are dependencies satisfied by Tomcat in the environment, Tomcat will be used first, and then pushed back.
In general, SpringBoot dependencies have been introduced by default in tomcat dependencies, so here it will always be true for tomcat, so Tomcat will always be selected as the default server by SpringBoot as a constant condition.
2. How to configure the current web container?
If you want to configure the current Web container, you can make SpringBoot load and parse the configuration automatically through yml configuration, or you can ignore SpringBoot automatic configuration by providing a custom @ Bean method.
Why can you skip SpringBoot's automatic web server configuration by giving the Bean of ServletWebServerFactory and WebServerFactoryCustomizer through @ Bean to Spring? Can be analyzed from the source code as follows:
For WebServerFactoryCustomizer, when configuring Tomcat,Jetty in the above ServletWebServerFactoryConfiguration configuration class Factory, it will be determined that there is a manually added ServletWebServerFactory on the note, then it will no longer be automatically configured:
For WebServerFactoryCustomizer, when the ServletWebServerFactoryAutoConfiguration server automatically configures class loading, if there is a self-defined WebServerFactoryCustomizer, then a WebServerFactoryCustomizerBeanPostProcessor post processor will be triggered, in which the WebServerFactoryCustomizer will be traversed and the internal customize method will be executed, thus skipping automatic configuration and switching to custom configuration:
Third, how to switch embedded Web servers (from tomcat to jetty)?
As you can see from the source code above, in general, Tomcat will always be selected as the default server by SpringBoot as a constant condition.
But what should we do if we don't want to use Tomcat as the default server, such as switching to Jetty?
We can remove the related dependencies of Tomcat from the spring-boot-starter-web in pom.xml, so that the environment no longer has Tomcat dependencies, and add the dependencies of Jetty so that Jetty can be selected by SpringBoot as a satisfying condition.
Org.springframework.boot spring-boot-starter-web spring-boot-starter-tomcat org.springframework.boot org.springframework.boot spring-boot-starter-jetty
In this way, when SpringBoot is restarted, it will switch to the Jetty server.
4. How to configure Web containers automatically?
For the automatic configuration of Web containers, Tomcat can see the TomcatServletWebServerFactory mentioned above in the future, which is automatically injected into a factory class of Tomcat through @ Bean:
Some initialization operations are performed on Tomcat inside the factory class, the most important of which is in the getWebServer method:
First of all, this class is provided by the SpringBoot package and is configured with the lowest-level tomcat instance (through new Tomcat, and this Tomcat is an instance class package org.apache.catalina.startup of the tomcat source package). The specific configuration details are not described, and ports, protocols, tomcat component objects are initialized and encapsulated:
Initialize the Web application information to be released Context to tomcat:
Encapsulate and start the initialized tomcat:
Finally, the tomcat object is encapsulated as a TomcatWebServer object to be called when SpringBoot starts.
To sum up, the automatic configuration of the web container is actually SpringBoot initializes the ports, protocols and components of the object by creating a native Tomcat object, and encapsulates the Web application information Context object into the tomcat object, and then starts tomcat after the Web application information configuration lifecycle monitoring takes effect, and finally starts the process
Encapsulated into a WebServer object that can be called when SpringBoot starts.
5. Web container starts source code parsing?
When did SpringBoot run a web server? This is analyzed from the SpringBootApplication.run () method. Take tomcat as an example, as mentioned above, the startup process should call the TomcatServletWebServerFactory.getWebServer method to get such an tomcat instance.
The call chain is shown in the following illustration:
SpringBootApplication.run ():
Context = createApplicationContext (): create a Context environment. In this method, different Context will be initialized according to the current environment, and AnnotationConfigServletWebApplicationContext will be initialized in the case of Web environment:
After initializing AnnotationConfigServletWebApplicationContext, call the refresh method of the context-- > onRefresh method-- after the constructor:
When the onRefresh method is called, the onRefresh method of ServletWebServerApplicationContext is called, within which the creation operation createWebServer () is performed on the web server:
In the createWebServer () method, it determines whether the application is published externally or in a built-in way, performing different logical operations. We learn here in a built-in way:
In this way, when SpringBoot starts up, when creating a Web server, it performs the operation of getWebServer, and then creates, initializes and starts the Web server.
To sum up: when SpringBoot's run starts, the current environment is determined.
In the case of a Web environment, create a ServletWebServerApplicationContext, execute the refresh method of the constructor, override the onRefresh method within the refresh method, and execute the create createWebServer () method, which determines how to get the web server based on whether the current application is released internally or externally.
If it is a built-in way, then through the TomcatServletWebServerFactory factory class to get a preferred web server, and then initialize the configuration of the server, application loading and server startup operations.
6. SpringBoot built-in server does not use SPI mechanism.
Finally, there is a conclusion to remember: the built-in server for SpringBoot will not go through the SPI mechanism (the official website also has a special description), because SpringBoot created the web server for us to publish applications. The purpose of not using the SPI mechanism is to minimize possible conflicts between internal and external web servers, and let web applications be managed by SpringBoot itself. Detailed reasons and principles are not studied here.
The above is all the contents of the article "how to use SpringBoot built-in web Server". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.