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

What is the method of omitting web.xml configuration for springboot

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

Share

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

This article mainly explains "what is the method of springboot omitting web.xml configuration". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "what is the method of springboot omitting web.xml configuration".

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.

Thank you for your reading, the above is the content of "what is the method of springboot omitting web.xml configuration". After the study of this article, I believe you have a deeper understanding of what the method of springboot omitting web.xml configuration is, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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