Configure Spring MVC with WebApplicationInitializer and WebMvcConfigurer

When creating a Spring MVC project using the Spring Tool Suite 3 IDE, you can see, we have a web.xml file that defines the configuration for the DispatcherServlet, the root-context.xml and the servlet-context.xml files define bean configuration in Spring container including ViewResolver, a Controller handle request from the user and .jsp files to display information to the user. If you want to do all of this entirely with Java code, you can use the WebApplicationInitializer and WebMvcConfigurer interfaces. How is it in detail? In this tutorial, I will guide you to configure Spring MVC with WebApplicationInitializer and WebMvcConfigurer!

First, I will create a new Maven project with packaging as “war” for web applications as follows:

Configure Spring MVC with WebApplicationInitializer and WebMvcConfigurer

I will use Java 21 for this example application.:

We will declare the dependencies for Spring MVC as follows:

with the version properties having the following values:

To run this web application, I will use the Maven Jetty Plugin as follows:

OK, now we will go to the main part of this tutorial!

In a nutshell, the WebApplicationInitializer interface is used to define the configuration for the web application with the declaration for the DispatcherServlet and the Spring container. It used to replace that web.xml file!

The WebMvcConfigurer interface is used to declare Spring MVC related configurations like ViewResolver and beans needed for the application to run. We will also declare this class in the class implementing interface WebApplicationInitializer.

I will create a new class SpringConfiguration implementing the WebMvcConfigurer interface to define the Spring container first.

You need to declare a Spring annotation of @EnableWebMvc to Spring import the default configuration from the WebMvcConfigurationSupport class to support Spring MVC. If you want to override any default configuration, you can override them in this SpringConfiguration class.

Here, I need to configure ViewResolver so I will use the addViewControllers() method to do this:

Here, I declare a view name as “index” mapping with the request “/”. And, as you can see, I also added a bean for the ViewResolver section using InternalResourceViewResolver. We will have the .jsp files in the /WEB-INF/views directory to build the View in the MVC model as follows:

Configure Spring MVC with WebApplicationInitializer and WebMvcConfigurer

The content of my index.jsp file is as follows:

Because we have declared the @Bean annotation in this class, you also need to add the @Configuration annotation for Spring to scan itself and create beans in its Spring container.

With the class WebApplicationInitializer, you can add a new class to implement it with the following content:

Here, we are using the AnnotationConfigWebApplicationContext class to configure how Spring will automatically create beans in its container using annotation. Spring will automatically scan the package “com.huongdanjava.springmvc” to do this.

We also initialized the DispatcherServlet and added it to the ServletContext. You can read this tutorial to understand how Spring will work with Java Servlet!

OK, now you can run our web application and go to http://localhost:8080 to check the results:

Mine is as follows:

Configure Spring MVC with WebApplicationInitializer and WebMvcConfigurer

Add Comment