• Menu
  • Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

JavaBeat

Java Tutorial Blog

  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)

WebApplicationInitializer in Spring MVC

May 15, 2014 //  by Krishna Srinivasan

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.

[wpdm_file id=97]

Category: Spring FrameworkTag: Spring MVC

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.

Previous Post: « Spring MVC : How To Return Custom 404 Error Pages
Next Post: Spring Data Neo4j 3 REST Exporter (Java Application) »

Primary Sidebar

Follow Us

  • Facebook
  • Pinterest

FEATURED TUTORIALS

How to Implement getActiveCount() Method of ThreadPoolExeceutor in Java

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Introductiion to Jakarta Struts

What’s new in Struts 2.0? – Struts 2.0 Framework

JavaBeat

Copyright © by JavaBeat · All rights reserved
Privacy Policy | Contact