Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
117 views

How To Use Spring WebInitializer

The DispatcherServlet has traditionally served as the front controller in Spring MVC applications and handled requests and responses. Previously, it had to be configured in the web.xml file, but with Servlet 3.0, this is no longer required. The WebApplicationInitializer interface allows programmatic configuration and registration of the DispatcherServlet without using web.xml. A simple implementation registers the servlet and maps requests. The initializer can also build and provide the Spring application context to the DispatcherServlet. This allows modern Spring MVC applications to remove web.xml configuration.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
117 views

How To Use Spring WebInitializer

The DispatcherServlet has traditionally served as the front controller in Spring MVC applications and handled requests and responses. Previously, it had to be configured in the web.xml file, but with Servlet 3.0, this is no longer required. The WebApplicationInitializer interface allows programmatic configuration and registration of the DispatcherServlet without using web.xml. A simple implementation registers the servlet and maps requests. The initializer can also build and provide the Spring application context to the DispatcherServlet. This allows modern Spring MVC applications to remove web.xml configuration.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

How to use Springs WebApplicationInitializer

by Jim White | Jan 17, 2014

The DispatcherServlet has always served as the doorway component or front controller in a Spring
Framework Web MVC application. Its job has been and still is to marshal requests to other
components in the Spring MVC application backend and to orchestrate a response back to clients.

DispatcherServlet and the web.xml


Until Spring 3.1, the DispatcherServlet had to be registered with the Servlet container via the
standard Web application web.xml file. The following elements had to be added to register the
DispatcherServlet.

1 <servlet>
2 <servlet-name>dispatcher</servlet-name>
3 <servlet-class>org.springframework.web.servlet.DispatcherServlet
4 </servlet-class>
5 </servlet>
6 <servlet-mapping>
7 <servlet-name>dispatcher</servlet-name>
8 <url-pattern>*.request</url-pattern>
9 </servlet-mapping>

With the presence of the Servlet 3.0 specification, the web.xml file in a Web application is now
optional. The Servlet 3.0 API brought annotations to the Java Web application world allowing the
registration of servlets to be accomplished by annotation.
However, since you dont write the DispatcherServlet, and the Spring community didnt have the
knowledge and details of your application to be able to annotate the DispatcherServlet for you, there
was a bit of a quandary. How do you get the DispatcherServlet registered with the Web container
without web.xml?
The WebApplicationInitializer
The answer was provided in Spring 3.1 (and of course is available with Spring 4 which came out in
December 2013) with the WebApplicationInitializer. An implementation of the
WebApplicationInitializer interface configures the ServletContext programmatically. 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. A simple implementation of WebApplicationInitializer is shown below.

1 public class WebMVCApplicationInitializer implements


2 WebApplicationInitializer {
3
4 public void onStartup(ServletContext container) {
5 ServletRegistration.Dynamic registration =
6 container.addServlet("dispatcher", new DispatcherServlet());
7 registration.setLoadOnStartup(1);
8 registration.addMapping("*.request");
9 }
10 }

When you need to customize the associated WebApplicationContext (i.e. the Spring container), you
may prefer to build an instance of the Spring container in the WebApplicationInitializer and provide
the container to the DispatcherServlet.

1 public class WebMVCApplicationInitializer implements


2 WebApplicationInitializer {
3
4 public void onStartup(ServletContext container) {
5 XmlWebApplicationContext appContext =
6 new XmlWebApplicationContext();
7 ServletRegistration.Dynamic registration =
8 container.addServlet("dispatcher",
9 new DispatcherServlet(appContext));
10 registration.setLoadOnStartup(1);
11 registration.addMapping("*.request");
12 }
13 }

Wrap Up
With the WebApplicationInitializer, Spring Framework Web MVC applications can now rid
themselves of web.xml. Additional configuration/implementations are available for programmatically
registering the DispatcherServlet and configuring the Spring container see here for more details.

If you would like to learn more about the Spring Framework or Spring Framework Web MVC, I
encourage you to take a look at Intertechs line of Spring Framework training classes. I just
completed updating our Spring classes to Spring Framework 4. There are a lot of new features in
later versions of Spring 3 and now Spring 4. Some of them have been covered in recent blog posts
(see generics, meta annotations, conditional bean configuration, and ordering autowired
collections ), but you can learn more about them in our new classes.

You might also like