spring 什么时候使用 AbstractAnnotationConfigDispatcherServletInitializer 和 WebApplicationInitializer?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26676782/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 00:10:46  来源:igfitidea点击:

When use AbstractAnnotationConfigDispatcherServletInitializer and WebApplicationInitializer?

springspring-mvcspring-3spring-4

提问by Manuel Jordan

I am working with Spring 4.0.7

我正在使用 Spring 4.0.7

I did a research about configure Spring MVC through JavaConfig.

我做了一个关于通过JavaConfig配置Spring MVC的研究。

Practically until yesterday I have seen two configurations using these two options

实际上直到昨天我已经看到使用这两个选项的两种配置

  1. extends AbstractAnnotationConfigDispatcherServletInitializer
  2. extends WebMvcConfigurerAdapter andimplements WebApplicationInitializer
  1. 扩展 AbstractAnnotationConfigDispatcherServletInitializer
  2. 扩展 WebMvcConfigurerAdapter实现 WebApplicationInitializer

Note: (2) are two classes, one for extension and the other for implementation

注意:(2)是两个类,一个用于扩展,另一个用于实现

I am using (2) because I have found many examples where I am able to configure converters, formatters, resources handlers etc…

我使用 (2) 是因为我发现了许多可以配置转换器、格式化程序、资源处理程序等的示例……

But in the latest days I have tried to help a question on StackOverflow and I did realize (1) exists.. I did some overview on Google about (1) and exists some examples working with (1)

但最近几天我试图帮助一个关于 StackOverflow 的问题,我确实意识到 (1) 存在..我在谷歌上做了一些关于 (1) 的概述并存在一些使用 (1) 的例子

My question is how the title of this post describe.

我的问题是这篇文章的标题是如何描述的。

Thank You

谢谢你

回答by M. Deinum

With the release of the Servlet 3.0 spec it became possible to configure your Servlet Container with (almost) no xml. For this there is the ServletContainerInitializerin the Servlet specification. In this class you can register filters, listeners, servlets etc. as you would traditionally do in a web.xml.

随着 Servlet 3.0 规范的发布,使用(几乎)没有 xml 配置您的 Servlet 容器成为可能。为此ServletContainerInitializer,在 Servlet 规范中有 。在这个类中,您可以注册过滤器、侦听器、servlet 等,就像您在web.xml.

Spring provides a an implementation the SpringServletContainerInitializerwhich knows how to handle WebApplicationInitializerclasses. Spring also provides a couple of base classes to extend to make your life easier the AbstractAnnotationConfigDispatcherServletInitializeris one of those. It registers a ContextLoaderlistener(optionally) and a DispatcherServletand allows you to easily add configuration classes to load for both classes and to apply filters to the DispatcherServletand to provide the servlet mapping.

Spring 提供了一个实现,SpringServletContainerInitializer它知道如何处理WebApplicationInitializer类。Spring 还提供了几个基类来扩展,使您的生活更轻松,AbstractAnnotationConfigDispatcherServletInitializer就是其中之一。它注册 a ContextLoaderlistener(可选)和 a,DispatcherServlet并允许您轻松添加配置类以加载这两个类,并将过滤器应用于DispatcherServlet和提供 servlet 映射。

The WebMvcConfigurerAdapteris for configuring Spring MVC, the replacement of the xml file loaded by the DispatcherServletfor configuring Spring MVC. The WebMvcConfigurerAdaptershould be used for a @Configurationclass.

WebMvcConfigurerAdapter是配置Spring MVC的,更换已加载的XML文件DispatcherServlet的配置Spring MVC。本WebMvcConfigurerAdapter应该用于@Configuration类。

@Configuration
@EnableWebMvc
public class WebConfiguration 
    extends WebMvcConfigurerAdapter implements WebApplicationInitializer
{ ... }

I wouldn't recommend mixing those as they are basically 2 different concerns. The first is for configuring the servlet container, the latter for configuring Spring MVC.

我不建议将它们混合,因为它们基本上是 2 个不同的问题。第一个用于配置servlet容器,后者用于配置Spring MVC。

You would want to split those into 2 classes.

您可能希望将它们分成 2 个类。

For the configuration.

对于配置。

@Configuration
@EnableWebMvc
public class WebConfiguration extends WebMvcConfigurerAdapter { ... }

For bootstrapping the application.

用于引导应用程序。

public class MyWebApplicationInitializer
    extends AbstractAnnotationConfigDispatcherServletInitializer
{

    protected Class<?>[] getRootConfigClasses() {
        return new Class[] {RootConfig.class};
    }

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

    protected String[] getServletMappings() {
        return new String[] {"/"};
    }

}

An added advantage is that you now can use the convenience classes provided by Spring instead of manually configuring the DispatcherServletand/or ContextLoaderListener.

一个额外的好处是您现在可以使用 Spring 提供的便利类,而不是手动配置DispatcherServlet和/或ContextLoaderListener

回答by Witold Kaczurba

To start from the beginning it is worth looking into how servlet container starts.

要从头开始,值得研究 servlet 容器是如何启动的。

So to start - SpringServletContainerInitializer has to find the right class implementing WebApplicationInitializer. There are two ways of making it happen:

所以开始 - SpringServletContainerInitializer 必须找到实现WebApplicationInitializer的正确类。有两种方法可以实现:

  1. One is by implementing WebApplicationInitializeron its own; the interface was introduced in Spring 3.1
  2. The second is by extending AbstractAnnotationConfigDispatcherServletInitializerclass which also implements WebApplicationInitializer. The class was introduced in Spring 3.2 for convenience and it is "the preferred approach for applications that use Java-based Spring configuration." - see the link. It enables you to start servlet application context as well as root application context.
  1. 一种是通过自己实现WebApplicationInitializer;该接口是在 Spring 3.1 中引入的
  2. 第二种是通过扩展AbstractAnnotationConfigDispatcherServletInitializer类,该类也实现了 WebApplicationInitializer。为了方便起见,Spring 3.2 中引入了该类,它是“使用基于 Java 的 Spring 配置的应用程序的首选方法”。- 见链接。它使您能够启动 servlet 应用程序上下文以及根应用程序上下文。

I would also like to higlight that WebMvcConfigurerAdapteryou mention should not be confused with WebApplicationInitializer. As it name suggests - it has to do with configuring "Mvc". It is an adapter class that implements empty methods from WebMvcConfigurer. You use it when you configure your Mvc controller with @EnableWebMvcannotation.

我还想强调您提到的WebMvcConfigurerAdapter不应与 WebApplicationInitializer 混淆。顾名思义 - 它与配置“Mvc”有关。它是一个适配器类,它实现了来自WebMvcConfigurer 的空方法。在使用@EnableWebMvc注释配置 Mvc 控制器时使用它。

Hope this helps.

希望这可以帮助。