溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

springboot是怎樣幫我們省去web.xml配置的

發(fā)布時間:2021-09-29 17:53:05 來源:億速云 閱讀:119 作者:柒染 欄目:大數據

這篇文章給大家介紹springboot是怎樣幫我們省去web.xml配置的,內容非常詳細,感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。

概述

最開始使用原生的springmvc時,總是免不了有如下xml配置

<!-- Spring MVC配置 -->
<!-- ====================================== -->
<servlet>
    <servlet-name>spring</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:spring-servlet.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>spring</servlet-name>
    <url-pattern>/*</url-pattern>
</servlet-mapping>
  

<!-- Spring配置 -->
<!-- ====================================== -->
<listener>
   <listenerclass>
 org.springframework.web.context.ContextLoaderListener
   </listener-class>
</listener>
  

<!-- 指定Spring Bean的配置文件所在目錄。默認配置在WEB-INF目錄下 -->
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:config/applicationContext.xml</param-value>
</context-param>

但是,切換到springboot之后,web.xml之類的繁瑣的配置基本上都不見了。出于好奇研究了下springboot究竟幫我們做了什么,我們可以免于這樣的繁瑣配置。

Servlet3.0規(guī)范

首先研究的第一點,為什么web.xml不見了。剛開始使用原生servlet(不使用web框架),web.xml就是非常重要的一個配置,無論是servlet、filter、listener都需要在web.xml里面配置下。

但是在servlet3.0里,這個配置得到了簡化??梢酝ㄟ^java配置(注解等)省去web.xml配置。

具體servlet3.0的規(guī)范這里就不討論了,說下其中一個非常重要的類。javax.servlet.ServletContainerInitializer

這個類會在web容器啟動階段被回調,可以在onStartup方法里做一些servlet、filter、listener的注冊等操作。

/**
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<Class<?>> c, ServletContext ctx)
        throws ServletException; 
}

springboot的實現

首先spring在META-INF/services下配置了這個類,讓整個web容器啟動后可以找到并啟動這個類

ServletContainerInitializer配置

SpringServletContainerInitializer
/**
* @HandlesTypes這個注解標明了該ServletContainerInitializer需要在啟動時候處理哪些類,
然后服務器會把找到的這些類傳到onStartup的第一個參數里

注意這里的類包括所配置類的子類,比如這里配置WebApplicationInitializer,
啟動之后,就會把這個WebApplicationInitializer的子類都傳進去
*/
@HandlesTypes(WebApplicationInitializer.class)
public class SpringServletContainerInitializer implements ServletContainerInitializer {
        
    @Override
    public void onStartup(Set<Class<?>> webAppInitializerClasses, ServletContext servletContext)
            throws ServletException {

        List<WebApplicationInitializer> initializers = new LinkedList<WebApplicationInitializer>();

        //.... 省略容錯的一些代碼
        initializers.add((WebApplicationInitializer) waiClass.newInstance());
        //.... 
    AnnotationAwareOrderComparator.sort(initializers);
        for (WebApplicationInitializer initializer : initializers) {
            initializer.onStartup(servletContext);
        }
    }

}

startup的邏輯很簡單,web容器啟動后,調用所有WebApplicationInitializer的onStartup方法。

WebApplicationInitializer 的實現SpringBootServletInitializer
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
   //....
    WebApplicationContext rootAppContext = createRootApplicationContext(
            servletContext);
   //...
}
protected WebApplicationContext createRootApplicationContext(
        ServletContext servletContext) {
    //...
    return run(application);
}

一般使用Springboot的時候,都會繼承一個類SpringBootServletInitializer,在這個類的onStartup方法中,啟動了整個Spring容器。

本地啟動springboot時,我們一般會寫一個類似于這樣的main方法。

上述分析也解釋了為啥把springboot應用部署到機器上,tomcat能夠找到springboot的入口,并啟動它。

DispatcherServlet的配置

關于springboot如何加載類并啟動的這里就不介紹了。
這里說明下究竟Springboot如何配置DispatcherServlet的

1)當類路徑下存在DispatcherServlet時候,該配置生效。
2)這個配置會在DispatcherServletAutoConfiguration配置完之后再配置。

DispatcherServletAutoConfiguration配置

看到這里就是我們非常熟悉的springboot的使用了。springboot在DispatcherServletConfiguration這個類里對DispatcherServlet進行了配置以及注冊。

服務器如tomcat在web應用啟動后,加載并啟動springboot,springboot通過@AutoConfiguration、@Bean、@Conditional等注解自動配置了DispatcherServlet。

關于springboot是怎樣幫我們省去web.xml配置的就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI