spring-boot - 哪一段代码实际上为springMVC注册了调度程序servlet? [英] spring-boot - Which piece of code actually register dispatcher servlet for springMVC?
问题描述
我试图在 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:
-
SpringApplication.run()
创建EmbeddedWebApplicationContext
应用程序上下文; - 调用其
refresh()
方法; - 刷新过程读取起始类
TestSpring
的注释。它寻找导入注释。EnableAutoConfiguration
就是其中之一。对于导入注释,刷新过程从注释值
获取相应的类,并调用其selectImports()
方法; - 在
@EnableAutoConfiguration
对应的类是EnableAutoConfigurationImportSelector
其<$ C的情况下$ c> selectImports()从META-INF / spring.factories
; - 此过程以递归方式继续。此外,还会读取这些导入选择器中的所有bean定义。即它包含由带有
@Bean
注释的方法定义的bean,即需要Spring上下文自动调用相应方法来实例化它们的bean; - 在上一步的读取bean定义中,实现
ServletContextInitializer
的bean是搜索并实例化。其中之一是bean,通过ServletRegistrationBean
的DispatcherServletAutoConfiguration.DispatcherServletRegistrationConfiguration#dispatcherServletRegistration()
方法定义的类型,其延伸ServletContextInitializer
。正如您可以从类的名称猜测的那样,这样的初始化程序将给定的servlet(在本例中为 DispatcherServlet )添加到给定的ServletContext
,onStartup()
方法被调用; - 创建了一个tomcat嵌入式服务器(尚未完全启动)。在上一步中发现的所有
ServletContextInitializer
都传递给了这个tomcat初始化 - 这是onStartup()
调用那些ServletContextInitializer
的方法,并创建DispatcherServlet
并将其注册为servlet ; - 结束
onRefresh()
应用程序上下文; -
finishRefresh( )
被称为其中Tomcat是最后通过开始TomcatEmbeddedServletContainer.start()
; - 结束
刷新()
应用程序上下文和其他最终初始化步骤; - 应用程序正在运行。
加载大量其他导入选择器
'LI>的
resfresh()
继续并达到 onRefresh()
中, createEmbeddedServletContainer( )
方法在内部调用; SpringApplication.run()
createsEmbeddedWebApplicationContext
application context;- Calls its
refresh()
method; - 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 annotationvalue
and invokes itsselectImports()
method; - In case of
@EnableAutoConfiguration
the corresponding class isEnableAutoConfigurationImportSelector
whoseselectImports()
loads tons of other import selectors from theMETA-INF/spring.factories
; - 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; - The
resfresh()
continues and reachesonRefresh()
, thecreateEmbeddedServletContainer()
method is called inside; - Among read bean defitions at the previous step, beans implementing
ServletContextInitializer
are searched for and instantiated. One of them is the bean, defined by theDispatcherServletAutoConfiguration.DispatcherServletRegistrationConfiguration#dispatcherServletRegistration()
method ofServletRegistrationBean
type that extendsServletContextInitializer
. As you can guess from the name of the class, such initializers add a given servlet (in this case DispatcherServlet) to a givenServletContext
, when theironStartup()
method is invoked; - A tomcat embedded server is created (not started completely yet). All found
ServletContextInitializer
s at the previous step are passed to this tomcat initialization - this is where theonStartup()
methods of thoseServletContextInitializer
s are called andDispatcherServlet
gets created and registered as servlet; - End of
onRefresh()
of application context; - The
finishRefresh()
is called where tomcat is finally started byTomcatEmbeddedServletContainer.start()
; - End of
refresh()
of application context and other final initialization steps; - The app is running.
这篇关于spring-boot - 哪一段代码实际上为springMVC注册了调度程序servlet?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!