03: Spring MVC with Java Configs instead of XML configs step by step

This tutorial extends Spring MVC form submission tutorial step by step. This tutorial gets rid of web.xml and applicationContext.xml files.

pom.xml changes

Step 1: Add “JSTL” (i.e. JSP Standard Tag Library ) jar dependency, and also override default “maven-war-plugin” by explicitly adding the plugin and setting the “failOnMissingWebXml” to false.

Java based Spring configs

Step 2: Create a new package “com.mytutorial.config” in “src/main/java”

Step 3: Add a Spring Java config file named “SimpleWebConfiguration.java” with the required Spring annotations @Configuration, @EnableWebMvc and @ComponentScan to replace what we defined in the “applicationContext.xml”.

Step 4: Define “SimpleWebAppInitializer.java” that implements “WebApplicationInitializer”, which builds on Servlet 3’s javax.servlet.ServletContainerInitializer facility. This class will be scanned by Spring on application startup and bootstrapped. WebApplicationInitializer implementation called AbstractAnnotationConfigDispatcherServletInitializer is extended.

Step 5: Delete “web.xml” & “applicationContext.xml” files in “src/main/webapp/WEB-INF”. Now we are XMLs free with the help of Java based config files and annotations.

Build & deploy simpleWeb.war

All other artifacts and deployment/running the simpleWeb are same as described in Spring MVC form submission tutorial step by step.

300+ Java Interview FAQs

Java & Big Data Tutorials