Spring MVC 和许多其他 Web 框架一样,是围绕前控制器设计的 模式,其中中心 、 提供共享算法 用于请求处理,而实际工作由可配置的委托组件执行。 该模型非常灵活,支持多种工作流程。ServletDispatcherServletSpring中文文档

和 任何 一样,需要根据 通过使用 Java 配置或在 中添加到 Servlet 规范。 反过来,使用 Spring 配置来发现 请求映射、视图解析、异常所需的委托组件 处理DispatcherServletServletweb.xmlDispatcherServletSpring中文文档

以下 Java 配置注册和初始化示例 ,由 Servlet 容器自动检测 (参见 Servlet 配置):DispatcherServletSpring中文文档

public class MyWebApplicationInitializer implements WebApplicationInitializer {

	@Override
	public void onStartup(ServletContext servletContext) {

		// Load Spring web application configuration
		AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
		context.register(AppConfig.class);

		// Create and register the DispatcherServlet
		DispatcherServlet servlet = new DispatcherServlet(context);
		ServletRegistration.Dynamic registration = servletContext.addServlet("app", servlet);
		registration.setLoadOnStartup(1);
		registration.addMapping("/app/*");
	}
}
class MyWebApplicationInitializer : WebApplicationInitializer {

	override fun onStartup(servletContext: ServletContext) {

		// Load Spring web application configuration
		val context = AnnotationConfigWebApplicationContext()
		context.register(AppConfig::class.java)

		// Create and register the DispatcherServlet
		val servlet = DispatcherServlet(context)
		val registration = servletContext.addServlet("app", servlet)
		registration.setLoadOnStartup(1)
		registration.addMapping("/app/*")
	}
}
除了直接使用 ServletContext API 之外,您还可以扩展和覆盖特定方法 (请参阅上下文层次结构下的示例)。AbstractAnnotationConfigDispatcherServletInitializer
除了直接使用 ServletContext API 之外,您还可以扩展和覆盖特定方法 (请参阅上下文层次结构下的示例)。AbstractAnnotationConfigDispatcherServletInitializer
对于编程用例,a 可以用作 替代 .有关详细信息,请参见 GenericWebApplicationContext javadoc。GenericWebApplicationContextAnnotationConfigWebApplicationContext
对于编程用例,a 可以用作 替代 .有关详细信息,请参见 GenericWebApplicationContext javadoc。GenericWebApplicationContextAnnotationConfigWebApplicationContext

以下配置示例注册并初始化:web.xmlDispatcherServletSpring中文文档

<web-app>

	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>

	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>/WEB-INF/app-context.xml</param-value>
	</context-param>

	<servlet>
		<servlet-name>app</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value></param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>

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

</web-app>
Spring Boot 遵循不同的初始化顺序。而不是钩入 Servlet 容器的生命周期,Spring Boot 使用 Spring 配置来 引导自身和嵌入的 Servlet 容器。 和声明 在 Spring 配置中被检测到并注册到 Servlet 容器。 有关详细信息,请参阅 Spring Boot 文档FilterServlet
Spring Boot 遵循不同的初始化顺序。而不是钩入 Servlet 容器的生命周期,Spring Boot 使用 Spring 配置来 引导自身和嵌入的 Servlet 容器。 和声明 在 Spring 配置中被检测到并注册到 Servlet 容器。 有关详细信息,请参阅 Spring Boot 文档FilterServlet