WebApplicationInitializer in Spring MVC

Spring 3.1 has introduced a new feature for configuring the deployment descriptor using the Java Config approach. We don’t need to write the web.xml file for initializing the web applications. Everything can be done through a Java class itself. The Java class has to implement the org.springframework.web.WebApplicationInitializer which will be loaded when web application starts. If you look at the example, a class implementing WebApplicationInitializer loads the spring context configuration file.

Lets look at the example.

1. Spring MVC Controller

SpringMVCController.java

package javabeat.net.spring.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class SpringMVCController {

	@RequestMapping(value = "/springmvctest", method = RequestMethod.GET)
	public String redirectTest(Model model) {
		model.addAttribute("msg", "Test Message");
		return "hello";
	}
}

2. WebApplicationInitializer

SpringWebAppInitializer.java

package javabeat.net.spring.controller;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;

import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.support.XmlWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;

public class SpringWebAppInitializer implements WebApplicationInitializer {

    @Override
    public void onStartup(ServletContext container) throws ServletException {
        XmlWebApplicationContext appContext = new XmlWebApplicationContext();
        appContext.setConfigLocation("/WEB-INF/spring-dispatcher-servlet.xml");

        ServletRegistration.Dynamic dispatcher = container.addServlet(
                "spring-dispatcher", new DispatcherServlet(appContext));
        dispatcher.setLoadOnStartup(1);
        dispatcher.addMapping("/");
    }

}

3. Views

hello.jsp

<html>
<body>
	<h1>JavaBeat Spring MVC - WebApplicationInitializer</h1>
	<h2>Value : ${msg}</h2>
</body>
</html>

4. Spring Configurations

spring-dispatcher-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans-3.0.xsd


http://www.springframework.org/schema/mvc


http://www.springframework.org/schema/mvc/spring-mvc.xsd


http://www.springframework.org/schema/context


http://www.springframework.org/schema/context/spring-context-3.0.xsd">

	<context:component-scan base-package="javabeat.net.spring.controller" />
	<bean id="jspViewResolver"
		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/jsp/" />
		<property name="suffix" value=".jsp" />
	</bean>
</beans>

5. Run WebApplicationInitializer using JavaConfig

You can just run the above spring mvc example without any extra changes. The class SpringWebAppInitializer will be loaded by the server and initialize the spring context. Note that, you have to use Spring 3.1 and Tomcat 7.0 to make it work. This feature is introduced from Servlet 3.0 release.

Comments

comments

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Speak Your Mind

*