Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When use AbstractAnnotationConfigDispatcherServletInitializer and WebApplicationInitializer?

I am working with Spring 4.0.7

I did a research about configure Spring MVC through JavaConfig.

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

  1. extends AbstractAnnotationConfigDispatcherServletInitializer
  2. extends WebMvcConfigurerAdapter and implements WebApplicationInitializer

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

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

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.

Thank You

like image 756
Manuel Jordan Avatar asked Oct 31 '14 14:10

Manuel Jordan


People also ask

What is the use of AbstractAnnotationConfigDispatcherServletInitializer?

Mostly, developers use AbstractAnnotationConfigDispatcherServletInitializer , which is an implementation of the WebApplicationInitializer , to create Spring web applications. Traditionally, Java web applications based on Servlets were using web. xml file to configure a Java web application.

What is the use of WebApplicationInitializer?

The WebApplicationInitializer In particular, it allows for the creation, configuration, and registration of the DispatcherServlet programmatically. Thereby allowing the web. xml file to be removed from modern Spring MVC applications.

How do you implement WebApplicationInitializer?

Implementing the WebApplicationInitializer interface. Overriding the onStartup method we create a new XmlWebApplicationContext configured with the same file passed as contextConfigLocation to the servlet in the XML example.

What is the use of context loader listener in Spring?

ContextLoaderListener creates a root web-application-context for the web-application and puts it in the ServletContext. This context can be used to load and unload the spring-managed beans ir-respective of what technology is being used in the controller layer(Struts or Spring MVC).


Video Answer


1 Answers

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 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 registers a 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.

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 { ... } 

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.

You would want to split those into 2 classes.

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 DispatcherServlet and/or ContextLoaderListener.

like image 154
M. Deinum Avatar answered Oct 06 '22 16:10

M. Deinum