Spring 4 Annotation Configuration without web.xml file using WebApplicationInitializer - Java @ Desk

Saturday, February 11, 2017

Spring 4 Annotation Configuration without web.xml file using WebApplicationInitializer

Spring 4 Annotation Configuration without web.xml file using WebApplicationInitializer

Spring 4 allows annotation based configuration to initialize the web application without having the web.xml as well as spring configuration xml file. The spring controller and services are loaded using the annotation @ComponentScan.

With servlet 3.x there is no need of the spring configuration file defined in web.xml. Servlet 3.x allows annotation based loading of configuration classes for controllers and services.

Below example will load the controllers and services classes on server start up i.e. there will be a class to replace web.xml to map the Spring Dispatcher Servlet and load it on server start up. Rest controller example is implemented here that returns the JSON response to the caller.

pom.xml - Add Spring 4.x release and Servlet 3.x release to support annotation based loading of Dispatcher Servlet
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

AppConfig.java - This class bootstrap Spring 4 MVC Rest in our application on server startup
package com.rest.spring4.annotationconfiguration.config;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

@ComponentScan(basePackages = "com.rest.spring4.annotationconfiguration")
public class AppConfig {


AppInitializer.java - This class is loaded by Servlet 3.x. This class will replace web.xml and it will map the spring’s dispatcher servlet and bootstrap it.
package com.rest.spring4.annotationconfiguration.config;

import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

public class AppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
 protected Class[] getRootConfigClasses() {
  return new Class[] { AppConfig.class };

 protected Class[] getServletConfigClasses() {
  return null;

 protected String[] getServletMappings() {
  return new String[] { "/" };

Person.java - Domain object for JSON response
package com.rest.spring4.annotationconfiguration.pojo;

public class Person {

 private String id;

 private String name;

 public String getId() {
  return id;

 public void setId(String id) {
  this.id = id;

 public String getName() {
  return name;

 public void setName(String name) {
  this.name = name;

PersonRestController.java - This class contains the Rest mapping to load the list of person objects in JSON format
package com.rest.spring4.annotationconfiguration.restcontroller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import com.rest.spring4.annotationconfiguration.pojo.Person;

public class PersonRestController {

 public ResponseEntity getPersons() {
  List<Person> persons = new ArrayList<Person>();

  Person person = new Person();
  person.setId("Id 1");
  person.setName("Person Name 1");


  Person person2 = new Person();
  person2.setId("Id 2");
  person2.setName("Person Name 2");


  return new ResponseEntity(persons, HttpStatus.OK);

[{"id":"Id 1","name":"Person Name 1"},{"id":"Id 2","name":"Person Name 2"}]

No comments:

Post a Comment