Servlet 配置

在Servlet环境中,您可以选择以编程方式配置Servlet容器,作为替代方案或与web.xml文件结合使用。以下示例注册了一个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容器。名为AbstractDispatcherServletInitializerWebApplicationInitializer的抽象基类实现使注册DispatcherServlet更加容易,方法是覆盖方法以指定servlet映射和DispatcherServlet配置的位置。

对于使用基于Java的Spring配置的应用程序,建议使用此方法,如下例所示

  • 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继承,如下例所示

  • 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,如下例所示

  • 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

AbstractDispatcherServletInitializerisAsyncSupported受保护方法提供了一个单一位置来启用DispatcherServlet及其映射到的所有过滤器的异步支持。默认情况下,此标志设置为true

最后,如果您需要进一步自定义DispatcherServlet本身,您可以覆盖createDispatcherServlet方法。