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 springboot helps us save web.xml configuration

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article introduces how springboot helps us omit the configuration of web.xml. The content is very detailed. Interested friends can use it for reference. I hope it will be helpful to you.

Overview

When you start using native springmvc, you will always have the following xml configuration

Spring org.springframework.web.servlet.DispatcherServlet contextConfigLocation classpath:spring-servlet.xml 1 spring / * org.springframework.web.context.ContextLoaderListener contextConfigLocation classpath:config/applicationContext.xml

However, after switching to springboot, tedious configurations such as web.xml are basically gone. Out of curiosity to study what springboot has done for us, we can avoid such a tedious configuration.

Servlet3.0 specification

The first point to study is why web.xml is missing. At the beginning of using native servlet (without using web framework), web.xml is a very important configuration. Servlet, filter and listener all need to be configured in web.xml.

But in servlet3.0, this configuration is simplified. You can omit the web.xml configuration through java configuration (comments, etc.).

The specification of the specific servlet3.0 will not be discussed here, but one of the very important classes. Javax.servlet.ServletContainerInitializer

This class will be called back during the startup phase of the web container, and you can do some registration of servlet, filter, listener, etc., in the onStartup method.

/ * Interface which allows a library/runtime to be notified of a web application's startup phase and perform any required programmatic registration of servlets, filters, and listeners in response to it.*/public interface ServletContainerInitializer {public void onStartup (Set > webAppInitializerClasses, ServletContext servletContext) throws ServletException {List initializers = new LinkedList (); / /.... Omit some fault-tolerant code initializers.add ((WebApplicationInitializer) waiClass.newInstance ()); / /.... AnnotationAwareOrderComparator.sort (initializers); for (WebApplicationInitializer initializer: initializers) {initializer.onStartup (servletContext);}

The logic of startup is simple: after the web container starts, all the onStartup methods of WebApplicationInitializer are called.

The implementation of WebApplicationInitializer SpringBootServletInitializer@Overridepublic void onStartup (ServletContext servletContext) throws ServletException {/ /.... WebApplicationContext rootAppContext = createRootApplicationContext (servletContext); / /...} protected WebApplicationContext createRootApplicationContext (ServletContext servletContext) {/ /. Return run (application);}

Generally, when you use Springboot, you will inherit a class SpringBootServletInitializer, and in the onStartup method of this class, the entire Spring container is started.

When we start springboot locally, we usually write a main method like this.

The above analysis also explains why when the springboot application is deployed on the machine, tomcat can find the entrance to springboot and launch it.

Configuration of DispatcherServlet

I won't talk about how springboot loads classes and starts them here.

Here's how Springboot configures DispatcherServlet.

1) this configuration takes effect when DispatcherServlet exists under the classpath.

2) this configuration will be configured after the DispatcherServletAutoConfiguration has been configured.

DispatcherServletAutoConfiguration configuration

See here is the very familiar use of springboot. Springboot configures and registers DispatcherServlet in the DispatcherServletConfiguration class.

After the server, such as tomcat, starts the web application, loading and starting springboot,springboot automatically configures DispatcherServlet through @ AutoConfiguration, @ Bean, @ Conditional and other annotations.

So much for sharing about how springboot helps us save web.xml configuration. I hope the above content can be of some help to you 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