spring-boot - 哪一段代码实际上为springMVC注册了调度程序servlet? [英] spring-boot - Which piece of code actually register dispatcher servlet for springMVC?

查看:108
本文介绍了spring-boot - 哪一段代码实际上为springMVC注册了调度程序servlet?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在 spring-boot 中找到, WebApplicationInitializer 的实现实际上注册了调度程序servlet。

I am trying to find out in spring-boot, which implementation of WebApplicationInitializer actually register the dispatcher servlet.

我没有从 SpringBootServletInitializer 或其父类型中找到任何片段代码。

I didn't found any piece code from SpringBootServletInitializer or its parent types did that.

相反, AbstractDispatcherServletInitializer 完成工作,但它是抽象的,我在Eclipse的帮助下找不到任何具体的实现。

Instead, AbstractDispatcherServletInitializer does the job, but it's abstract, I can't find any of its concrete implementation with help of Eclipse.

那么,实际调用哪一类代码来注册springMVC的调度程序servlet?

So, which piece of code from which class is actually invoked to register the dispatcher servlet for springMVC?

这是一个随后的问题: spring-boot如何能够提供特定网址吗?

推荐答案

以下是最终Spring Boot初始化步骤的说明注册 DispatcherServlet

Below is the description of Spring Boot initialization steps that eventually register DispatcherServlet.

@EnableAutoConfiguration
public class TestSpring {

    public static void main(String[] args) throws Exception {
        SpringApplication.run(TestSpring.class, args);
    }
}



Spring Boot初始化步骤



以下是步骤:

Spring Boot Initialization Steps

Here are the steps:


  1. SpringApplication.run()创建 EmbeddedWebApplicationContext 应用程序上下文;

  2. 调用其 refresh()方法;

  3. 刷新过程读取起始类 TestSpring 的注释。它寻找导入注释。 EnableAutoConfiguration 就是其中之一。对于导入注释,刷新过程从注释获取相应的类,并调用其 selectImports()方法;

  4. @EnableAutoConfiguration 对应的类是 EnableAutoConfigurationImportSelector 其<$ C的情况下$ c> selectImports()从 META-INF / spring.factories ;

  5. 加载大量其他导入选择器
  6. 此过程以递归方式继续。此外,还会读取这些导入选择器中的所有bean定义。即它包含由带有 @Bean 注释的方法定义的bean,即需要Spring上下文自动调用相应方法来实例化它们的bean;

  7. 'LI>的 resfresh()继续并达到 onRefresh()中, createEmbeddedServletContainer( )方法在内部调用;
  8. 在上一步的读取bean定义中,实现 ServletContextInitializer 的bean是搜索并实例化。其中之一是bean,通过 ServletRegistrationBean DispatcherServletAutoConfiguration.DispatcherServletRegistrationConfiguration#dispatcherServletRegistration()方法定义的类型,其延伸 ServletContextInitializer 。正如您可以从类的名称猜测的那样,这样的初始化程序将给定的servlet(在本例中为 DispatcherServlet )添加到给定的 ServletContext onStartup()方法被调用;

  9. 创建了一个tomcat嵌入式服务器(尚未完全启动)。在上一步中发现的所有 ServletContextInitializer 都传递给了这个tomcat初始化 - 这是 onStartup()调用那些 ServletContextInitializer 的方法,并创建 DispatcherServlet 并将其注册为servlet ;

  10. 结束 onRefresh()应用程序上下文;

  11. finishRefresh( )被称为其中Tomcat是最后通过开始TomcatEmbeddedServletContainer.start();

  12. 结束刷新()应用程序上下文和其他最终初始化步骤;

  13. 应用程序正在运行。

  1. SpringApplication.run() creates EmbeddedWebApplicationContext application context;
  2. Calls its refresh() method;
  3. Refresh process reads annotations of the starting class TestSpring. It looks for import annotations. EnableAutoConfiguration is one of them. For an import annotation the refresh process gets the corresponding class from the annotation value and invokes its selectImports() method;
  4. In case of @EnableAutoConfiguration the corresponding class is EnableAutoConfigurationImportSelector whose selectImports() loads tons of other import selectors from the META-INF/spring.factories;
  5. This process continues recursively. Also, all bean definitions, that are inside these import selectors, are read. I.e. it includes beans defined by a method with the @Bean annotation, i.e. beans that require the Spring context to call the corresponding method automatically to instantiate them;
  6. The resfresh() continues and reaches onRefresh(), the createEmbeddedServletContainer() method is called inside;
  7. Among read bean defitions at the previous step, beans implementing ServletContextInitializer are searched for and instantiated. One of them is the bean, defined by the DispatcherServletAutoConfiguration.DispatcherServletRegistrationConfiguration#dispatcherServletRegistration() method of ServletRegistrationBean type that extends ServletContextInitializer. As you can guess from the name of the class, such initializers add a given servlet (in this case DispatcherServlet) to a given ServletContext, when their onStartup() method is invoked;
  8. A tomcat embedded server is created (not started completely yet). All found ServletContextInitializers at the previous step are passed to this tomcat initialization - this is where the onStartup() methods of those ServletContextInitializers are called and DispatcherServlet gets created and registered as servlet;
  9. End of onRefresh() of application context;
  10. The finishRefresh() is called where tomcat is finally started by TomcatEmbeddedServletContainer.start();
  11. End of refresh() of application context and other final initialization steps;
  12. The app is running.

这篇关于spring-boot - 哪一段代码实际上为springMVC注册了调度程序servlet?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆