Servlet Config

在 Servlet 环境中,可以选择以编程方式配置 Servlet 容器,作为`web.xml`文件的替代或组合部分。以下示例注册了一个`DispatcherServlet`:

In a Servlet environment, you have the option of configuring the Servlet container programmatically as an alternative or in combination with a web.xml file. The following example registers a DispatcherServlet:

  • Java

  • Kotlin

import org.springframework.web.WebApplicationInitializer;

public class MyWebApplicationInitializer implements WebApplicationInitializer {

	@Override
	public void onStartup(ServletContext container) {
		XmlWebApplicationContext appContext = new XmlWebApplicationContext();
		appContext.setConfigLocation("/WEB-INF/spring/dispatcher-config.xml");

		ServletRegistration.Dynamic registration = container.addServlet("dispatcher", new DispatcherServlet(appContext));
		registration.setLoadOnStartup(1);
		registration.addMapping("/");
	}
}
import org.springframework.web.WebApplicationInitializer

class MyWebApplicationInitializer : WebApplicationInitializer {

	override fun onStartup(container: ServletContext) {
		val appContext = XmlWebApplicationContext()
		appContext.setConfigLocation("/WEB-INF/spring/dispatcher-config.xml")

		val registration = container.addServlet("dispatcher", DispatcherServlet(appContext))
		registration.setLoadOnStartup(1)
		registration.addMapping("/")
	}
}

WebApplicationInitializer`是 Spring MVC 提供的一个接口,可确保检测到你的实现,并自动使用它来初始化任何 Servlet 3 容器。名为`AbstractDispatcherServletInitializer 的`WebApplicationInitializer`抽象基类实现使得注册`DispatcherServlet`变得更加容易,只需重写方法即可指定 Servlet 映射和`DispatcherServlet`配置的位置。

WebApplicationInitializer is an interface provided by Spring MVC that ensures your implementation is detected and automatically used to initialize any Servlet 3 container. An abstract base class implementation of WebApplicationInitializer named AbstractDispatcherServletInitializer makes it even easier to register the DispatcherServlet by overriding methods to specify the servlet mapping and the location of the DispatcherServlet configuration.

适用于使用基于 Java 的 Spring 配置的应用程序,以下示例展示了这一点:

This is recommended for applications that use Java-based Spring configuration, as the following example shows:

  • Java

  • Kotlin

public class MyWebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {

	@Override
	protected Class<?>[] getRootConfigClasses() {
		return null;
	}

	@Override
	protected Class<?>[] getServletConfigClasses() {
		return new Class<?>[] { MyWebConfig.class };
	}

	@Override
	protected String[] getServletMappings() {
		return new String[] { "/" };
	}
}
class MyWebAppInitializer : AbstractAnnotationConfigDispatcherServletInitializer() {

	override fun getRootConfigClasses(): Array<Class<*>>? {
		return null
	}

	override fun getServletConfigClasses(): Array<Class<*>>? {
		return arrayOf(MyWebConfig::class.java)
	}

	override fun getServletMappings(): Array<String> {
		return arrayOf("/")
	}
}

如果你使用基于 XML 的 Spring 配置,则应该直接从 AbstractDispatcherServletInitializer 扩展,以下示例显示了这一点:

If you use XML-based Spring configuration, you should extend directly from AbstractDispatcherServletInitializer, as the following example shows:

  • Java

  • Kotlin

public class MyWebAppInitializer extends AbstractDispatcherServletInitializer {

	@Override
	protected WebApplicationContext createRootApplicationContext() {
		return null;
	}

	@Override
	protected WebApplicationContext createServletApplicationContext() {
		XmlWebApplicationContext cxt = new XmlWebApplicationContext();
		cxt.setConfigLocation("/WEB-INF/spring/dispatcher-config.xml");
		return cxt;
	}

	@Override
	protected String[] getServletMappings() {
		return new String[] { "/" };
	}
}
class MyWebAppInitializer : AbstractDispatcherServletInitializer() {

	override fun createRootApplicationContext(): WebApplicationContext? {
		return null
	}

	override fun createServletApplicationContext(): WebApplicationContext {
		return XmlWebApplicationContext().apply {
			setConfigLocation("/WEB-INF/spring/dispatcher-config.xml")
		}
	}

	override fun getServletMappings(): Array<String> {
		return arrayOf("/")
	}
}

AbstractDispatcherServletInitializer 还提供了一种方便的方式来添加 Filter 实例,并让它们自动映射到 DispatcherServlet,以下示例展示了这一点:

AbstractDispatcherServletInitializer also provides a convenient way to add Filter instances and have them be automatically mapped to the DispatcherServlet, as the following example shows:

  • Java

  • Kotlin

public class MyWebAppInitializer extends AbstractDispatcherServletInitializer {

	// ...

	@Override
	protected Filter[] getServletFilters() {
		return new Filter[] {
			new HiddenHttpMethodFilter(), new CharacterEncodingFilter() };
	}
}
class MyWebAppInitializer : AbstractDispatcherServletInitializer() {

	// ...

	override fun getServletFilters(): Array<Filter> {
		return arrayOf(HiddenHttpMethodFilter(), CharacterEncodingFilter())
	}
}

每个筛选器会根据其具体类型添加一个默认名称,并自动映射到 DispatcherServlet

Each filter is added with a default name based on its concrete type and automatically mapped to the DispatcherServlet.

AbstractDispatcherServletInitializer 受保护的方法 isAsyncSupported 提供了一个单一位置来启用 DispatcherServlet 上的异步支持以及映射到它的所有筛选器。默认情况下,此标志被设置为 true

The isAsyncSupported protected method of AbstractDispatcherServletInitializer provides a single place to enable async support on the DispatcherServlet and all filters mapped to it. By default, this flag is set to true.

最后,如果需要进一步自定义 DispatcherServlet 本身,你可以重写 createDispatcherServlet 方法。

Finally, if you need to further customize the DispatcherServlet itself, you can override the createDispatcherServlet method.