问题描述
我正在使用Spring 4.0.7
I am working with Spring 4.0.7
我对通过JavaConfig配置Spring MVC进行了研究.
I did a research about configure Spring MVC through JavaConfig.
实际上直到昨天,我已经看到了使用这两个选项的两种配置
Practically until yesterday I have seen two configurations using these two options
- 扩展了AbstractAnnotationConfigDispatcherServletInitializer
- 扩展了WebMvcConfigurerAdapter ,并实现了WebApplicationInitializer
- extends AbstractAnnotationConfigDispatcherServletInitializer
- extends WebMvcConfigurerAdapter and implements WebApplicationInitializer
注意 :( 2)是两个类,一个用于扩展,另一个用于实现
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…
但是在最近的几天里,我试图解决有关StackOverflow的问题,但我确实意识到(1)存在..我在Google上对(1)进行了一些概述,并存在一些与(1)一起工作的示例
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)
我的问题是这篇文章的标题如何描述.
My question is how the title of this post describe.
谢谢
推荐答案
随着Servlet 3.0规范的发布,可以(几乎)没有xml配置Servlet容器.为此,在其中的 ServletContainerInitializer
Servlet规范.在该类中,您可以注册过滤器,侦听器,servlet等,就像您通常在web.xml
中所做的那样.
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 ServletContainerInitializer
in the Servlet specification. In this class you can register filters, listeners, servlets etc. as you would traditionally do in a web.xml
.
Spring提供了 SpringServletContainerInitializer
知道如何处理 WebApplicationInitializer
类. Spring还提供了几个基类来扩展以使您的生活更轻松,而AbstractAnnotationConfigDispatcherServletInitializer
就是其中之一.它注册ContextLoaderlistener
(可选)和DispatcherServlet
,并允许您轻松添加配置类以为这两个类加载,并将过滤器应用于DispatcherServlet
并提供servlet映射.
Spring provides a an implementation the SpringServletContainerInitializer
which knows how to handle WebApplicationInitializer
classes. Spring also provides a couple of base classes to extend to make your life easier and the AbstractAnnotationConfigDispatcherServletInitializer
is one of those. It registersa ContextLoaderlistener
(optionally) and a DispatcherServlet
and allows you to easily add configuration classes to load for both classes and to apply filters to the DispatcherServlet
and to provide the servlet mapping.
WebMvcConfigurerAdapter
用于配置Spring MVC,由DispatcherServlet
加载的用于配置Spring MVC的xml文件的替换. WebMvcConfigurerAdapter
应该用于@Configuration
类.
The WebMvcConfigurerAdapter
is for configuring Spring MVC, the replacement of the xml file loaded by the DispatcherServlet
for configuring Spring MVC. The WebMvcConfigurerAdapter
should be used for a @Configuration
class.
@Configuration
@EnableWebMvc
public class WebConfiguration
extends WebMvcConfigurerAdapter implements WebApplicationInitializer
{ ... }
我不建议您将它们混在一起,因为它们基本上是两个不同的问题.第一个用于配置servlet容器,第二个用于配置Spring MVC.
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类.
You would want to split those into 2 classes.
用于配置.
@Configuration
@EnableWebMvc
public class WebConfiguration extends WebMvcConfigurerAdapter { ... }
用于引导应用程序.
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[] {"/"};
}
}
另一个优点是,您现在可以使用Spring提供的便利类,而不必手动配置DispatcherServlet
和/或ContextLoaderListener
.
An added advantage is that you now can use the convenience classes provided by Spring instead of manually configuring the DispatcherServlet
and/or ContextLoaderListener
.
这篇关于什么时候使用AbstractAnnotationConfigDispatcherServletInitializer和WebApplicationInitializer?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!