Spring MVC Application Without the web.xml File, we have eliminated the web.xml file, but we have left with the spring config XML file that is this file “application-config.xml“. So here, we are going to see how to eliminate the spring config XML file and build a spring application without any .xml configuration.
Implementation: Project
Let us demonstrate Spring Application Without Any .xml Configuration stepwise that is as follows:
Step 1: Set up the project
Note: We are going to use Spring Tool Suite 4 IDE for this project. Please for refer prior to install STS on your local machine How to Download and Install Spring Tool Suite (Spring Tools 4 for Eclipse) IDE?
Go to your STS IDE then create a new maven project, File > New > Maven Project, and choose the following archetype as shown in the below image as follows:
Step 2: Adding Some Maven Dependencies
Add the following maven dependencies and plugin to your pom.xml file.
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.3.18</version> </dependency> <!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api --> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>4.0.1</version> <scope>provided</scope> </dependency> <!-- plugin --> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-war-plugin</artifactId> <version>2.6</version> <configuration> <failOnMissingWebXml>false</failOnMissingWebXml> </configuration> </plugin> </plugins> </build>
Below is the complete code for the pom.xml file after adding these dependencies.
File: pom.xml
XML
< modelVersion >4.0.0</ modelVersion > < groupId >com.neveropen</ groupId > < artifactId >spring-calculator</ artifactId > < packaging >war</ packaging > < version >0.0.1-SNAPSHOT</ version > < name >spring-calculator Maven Webapp</ name > < dependencies > < dependency > < groupId >junit</ groupId > < artifactId >junit</ artifactId > < version >3.8.1</ version > < scope >test</ scope > </ dependency > < dependency > < groupId >org.springframework</ groupId > < artifactId >spring-webmvc</ artifactId > < version >5.3.18</ version > </ dependency > < dependency > < groupId >javax.servlet</ groupId > < artifactId >javax.servlet-api</ artifactId > < version >4.0.1</ version > < scope >provided</ scope > </ dependency > </ dependencies > < build > < finalName >spring-calculator</ finalName > < plugins > < plugin > < groupId >org.apache.maven.plugins</ groupId > < artifactId >maven-war-plugin</ artifactId > < version >2.6</ version > < configuration > < failOnMissingWebXml >false</ failOnMissingWebXml > </ configuration > </ plugin > </ plugins > </ build > </ project > |
Step 3: Building Spring Application Without Any .xml Configuration
Before moving into the coding part let’s have a look at the file structure in the below image. As you can see in the file structure we have no file with .xml configuration.
So at first create an src/main/java folder and inside this folder create a class named CalculatorApplicationInitializer and put it inside the com.neveropen.calculator.config package and implement the WebApplicationInitializer interface. Refer to the below image.
Now we have to create the spring config class and register the class with our WebApplicationContext. So go to the src/main/java folder and inside this folder create a class named CalculatorAppConfig and put it inside the com.neveropen.calculator.config package.
File: CalculatorAppConfig.java file
Java
// Java Illusion for CalculatorAppConfig Class package com.neveropen.calculator.config; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; // Class @Configuration @ComponentScan (basePackages = "com.neveropen.calculator.controllers" ) public class CalculatorAppConfig { } |
We have used the @Configuration and @ComponentScan annotation in the above class. Overall we can say this class is the replacement of the following .xml file.
XML
<? xml version = "1.0" encoding = "UTF-8" ?> xsi:schemaLocation="http://www.springframework.org/schema/beans < context:component-scan base-package = "com.neveropen.calculator.controllers" ></ context:component-scan > </ beans > |
File: CalculatorApplicationInitializer.java
Java
// Java Program to Illustrate // CalculatorApplicationInitializer Class package com.neveropen.calculator.config; // Importing required classes import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.ServletRegistration; import org.springframework.web.WebApplicationInitializer; import org.springframework.web.context.support.AnnotationConfigWebApplicationContext; import org.springframework.web.servlet.DispatcherServlet; // Class // Implementing WebApplicationInitializer interface public class CalculatorApplicationInitializer implements WebApplicationInitializer { // Method public void onStartup(ServletContext servletContext) throws ServletException { AnnotationConfigWebApplicationContext webApplicationContext = new AnnotationConfigWebApplicationContext(); // Registering the spring config class webApplicationContext.register( CalculatorAppConfig. class ); // Creating a dispatcher servlet object DispatcherServlet dispatcherServlet = new DispatcherServlet(webApplicationContext); // Registering Dispatcher Servlet // with Servlet Context ServletRegistration .Dynamic myCustomDispatcherServlet = servletContext.addServlet( "myDispatcherServlet" , dispatcherServlet); // Setting load on startup myCustomDispatcherServlet.setLoadOnStartup( 1 ); // Adding mapping URL myCustomDispatcherServlet.addMapping( "/gfg.com/*" ); } } |
Step 4: Create Controller and Test The Application
Go to the src/main/java folder and inside this folder create a class named GfgController and put it inside the ‘com.neveropen.calculator.controllers’ package. Below is the code for the GfgController.java file.
File: GfgController.java
Java
// Java Program to Illustrate GfgController Class package com.neveropen.calculator.controllers; // Importing required classes import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; // Class @Controller public class GfgController { // Testor method @RequestMapping ( "/welcome" ) @ResponseBody public String helloGfg() { return "Welcome to Lazyroar!" ; } } |
Step 5: Run The Application
Now run your spring MVC application and hit the following URL
http://localhost:8080/spring-calculator/gfg.com/welcome
And we can see the output as shown in the below image as depicted below as follows: