Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How does Tomcat deal with SpringBoot applications

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces how Tomcat deals with SpringBoot applications, the content is very detailed, interested friends can refer to, hope to be helpful to you.

In the past year or two, due to the reduction of a large number of tedious Spring configuration and the promotion of SpringCloud based on Boot, more and more applications begin to use SpringBoot for development.

SpringBoot starts a Web service in the form of a standard Java application, and hides the existence of the container in a configuration file, which is very convenient to use. Tomcat is one of the containers built into Spring Boot.

This time let's look at how SpringBoot and Tomcat are integrated to provide services.

In Spring Boot, the implementation is basically similar. The difference is that most of the configuration information is default, while other user-specific settings are read and parsed in Boot configuration files such as application.properties and set to various components of Tomcat.

Another difference is that Spring Boot uses Embedded Tomcat. We have also briefly introduced this in the previous article (Embedded Tomcat, my friend, would you like to try it)

Of course, the above two points are the basic principles of using Tomcat throughout the Boot project, but there are some differences between the use of Embedded Tomcat and the use of plug-ins in Boot and Maven.

These are the three embedded containers used by Boot, and Tomcat is started by default.

Where should we start to analyze this problem?

When Boot starts, it clearly tells us such a message.

S.b.c.e.t.TomcatEmbeddedServletContainer: Tomcat initialized with port

The one we see is the output of logback. In front of it is the abbreviated package name, the most important thing is the Container, jump to the class to have a look.

What can match this line of log is the init method of container

The contents of some of the details in front of us do not pay too much attention to at first. This must be the one that comes into the eye.

This.tomcat.start ()

The tomcat here is an instance of the Embedded Tomcat class.

The operation of start here is to start the container.

GetServer and getConnector in the method are known to friends who are familiar with Tomcat. There are several main components in Tomcat:

Egine

Host

Context

Wrapper

Connector

The first four are the top-to-bottom components of the container, which is an inclusive relationship. These alone are not enough for us to access the deployed application. At this time, it is necessary for the container to connect to the external component Connector.

Moreover, when it comes to start, the component configuration of the container has been completed, just to start to provide services. These configured reads were completed before the initial phase.

The following figure is some code when the configuration is read in the initialization phase. There is no special place. Set BaseDir and parse the configuration settings for each component.

In addition, there are several log outputs when the Spring Boot application starts.

We know that Spring MVC distributes and processes requests through DispatcherServlet, and before the emergence of Spring Boot, it needs to be configured in web.xml to intercept requests.

After Servlet 3.0, the concepts of Dynamic Servlet and Dynamic Filter are added to the rules, and components can be dynamically registered to Context at run time.

So the Context we observe is just an empty application, and then dynamically add Servlet, Filter and other content to it.

In addition to directly executing Main methods in the form of Jar, Spring Boot also supports packaging Boot applications into War files and deploying them into standards and containers without using Embedded containers.

Compared to executing the Main method to launch the Spring Boot application, how does the Boot capability provide when it is provided by the Web application?

Looking at the following figure, the META-INF of the Jar file contains a declaration of SCI in the services.

This is the secret that Spring Boot works in a standard Web container.

What does SCI do?

When the container starts, it processes the HandlesTypes annotations of each ServletContainerInitializer in turn, then calls the onStartup methods of all ServletContainerInitializer objects separately, and passes the array of classes obtained from processing HandlesTypes annotations to the onStartup method of ServletContainerInitializer.

In the configure phase, we package Boot into the Initalizer provided by war and run it.

Dealing with dispatcherServlet at this point is no different from starting execution with the Main method.

Therefore, when we see that Boot applications can be easily executed as Web applications with so little configuration, we should clearly realize that the Embedded container behind has done a lot of work, and it is also related to a variety of new J2EE specifications. And the most important thing is that, no matter how it changes, it is essentially the same, as a standard Context, the only difference is whether it is configured by parsing a static file or by dynamically adding it.

So much for sharing about how Tomcat deals with SpringBoot applications. I hope the above content can be of some help and learn more knowledge. If you think the article is good, you can share it for more people to see.

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.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report