DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
View Events Video Library
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Integrating PostgreSQL Databases with ANF: Join this workshop to learn how to create a PostgreSQL server using Instaclustr’s managed service

Mobile Database Essentials: Assess data needs, storage requirements, and more when leveraging databases for cloud and edge applications.

Monitoring and Observability for LLMs: Datadog and Google Cloud discuss how to achieve optimal AI model performance.

Automated Testing: The latest on architecture, TDD, and the benefits of AI and low-code tools.

Related

  • Distributed Tracing System (Spring Cloud Sleuth + OpenZipkin)
  • Java, Spring Boot, and MongoDB: Performance Analysis and Improvements
  • How To Build Web Service Using Spring Boot 2.x
  • Migration of Microservice Applications From WebLogic to Openshift

Trending

  • The API-Centric Revolution: Decoding Data Integration in the Age of Microservices and Cloud Computing
  • The Winds of Change: How Generative AI is Revolutionizing Cybersecurity
  • How TIBCO Is Evolving Its Platform To Embrace Developers and Simplify Cloud Integration
  • How To Verify Database Connection From a Spring Boot Application
  1. DZone
  2. Coding
  3. Frameworks
  4. How Spring Boot Initializes the Spring MVC Application Context

How Spring Boot Initializes the Spring MVC Application Context

In this article, we use Spring Boot which has been bootstrapped in order to enable and use the web services functionality Spring provides.

Abdus Mondal user avatar by
Abdus Mondal
·
Jul. 27, 17 · Tutorial
Like (9)
Save
Tweet
Share
57.25K Views

Join the DZone community and get the full member experience.

Join For Free

When the Spring Boot is bootstrapped using the below code, it loads the Spring MVC configuration automatically.

package hello;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication

public class Application {

    public static void main(String[] args) {

        SpringApplication.run(Application.class, args);

    }

}

Under the hood of @SpringBootApplication, when the above code is executed, it adds the below annotation.

  1. @Configuration tags the class as a source of bean definitions for the application context.

  2. @EnableAutoConfiguration tells Spring Boot to start adding beans based on classpath settings, other beans, and various property settings.

Normally you would add @EnableWebMvc for a Spring MVC app (explicit declaration ), but Spring Boot adds it automatically when it sees spring-webmvc on the classpath. This flags the application as a web application and activates key behaviors such as setting up a DispatcherServlet.

@ComponentScan tells Spring to look for other components, configurations, and services in the hello package, allowing it to find the controllers.

Spring boot MVC dependency jar

Here are some different options you can use to initialize the Spring MVC context if you're NOT using Spring Boot.

  1. @EnableWebMvc + extending WebMvcConfigurerAdapter and customizing some of the default configurations. With this mechanism, you are basically doing some autoconfiguration provided by @EnableWebMvc.

  2. Just extend WebMvcConfigurationSupport and customize the default configuration.

  3. If you're using Spring Boot, add @EnableAutoConfiguration annotation and extend WebMvcConfigurerAdapter. No need to use @EnableMvc.

So option 3 is more generic.

Below is a chart I created to illustrate the difference between WebMvcConfigurerAdapter and WebMvcConfigurationSupport.

WebMvcConfigurerAdapter

WebMvcConfigurationSupport

org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter class.

org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport class

implements WebMvcConfigurer.

Implements: Aware, ApplicationContextAware, ServletContextAware

Main purpose is to customize the default configuration.

This is the main class providing the configuration behind the MVC Java config. As we see it implements the *Aware  interfaces.                                            

The whole process is manual.

It works under the hood of @EnableWebMvc to an application @Configuration class. If you add @EnableWebMvc in your MySpringMVC.java configuration, WebMvcConfigurationSupport is imported and does the configuration and initialization. Fully automated.


So Spring Boot initializes WebMvcConfigurationSupport for you. If you see the Java API doc. It done the following –

This is the main class providing the configuration behind the MVC Java config. It is typically imported by adding @EnableWebMvc to an application's @Configuration class. An alternative, more advanced, option is to extend directly from this class and override methods as necessary, remembering to add @Configuration to the subclass and @Bean to overridden @Bean methods. For more details, see the Java doc of @EnableWebMvc.

1. This class registers the following HandlerMappings:

  • RequestMappingHandlerMapping - ordered at 0 for mapping requests to annotated controller methods.

  • HandlerMapping - ordered at 1 to map URL paths directly to view names.

  • BeanNameUrlHandlerMapping - ordered at 2 to map URL paths to controller bean names.
  • HandlerMapping - ordered at Integer.MAX_VALUE-1 to serve static resource requests.   
  • HandlerMapping - ordered at Integer.MAX_VALUE to forward requests to the default servlet.
  • 2. Registers these HandlerAdapters:

    • RequestMappingHandlerAdapter - for processing requests with annotated controller methods.  
    • HttpRequestHandlerAdapter - for processing requests with HttpRequestHandler.
    • SimpleControllerHandlerAdapter - for processing requests with interface-based Controllers.   

    3.Registers a HandlerExceptionResolverComposite with this chain of exception resolvers:

    • ExceptionHandlerExceptionResolver - for handling exceptions through @ExceptionHandler methods.

    • ResponseStatusExceptionResolver - for exceptions annotated with @ResponseStatus.

    • DefaultHandlerExceptionResolver - for resolving known Spring exception types.

    4.Registers an AntPathMatcher and a UrlPathHelper to be used by:

    • The RequestMappingHandlerMapping.

    • The HandlerMapping for ViewControllers.

    • The HandlerMapping for serving resources.

    Note that those beans can be configured with a PathMatchConfigurer.

    Both the RequestMappingHandlerAdapter and the ExceptionHandlerExceptionResolver are configured with default instances of the following by default:

    • A ContentNegotiationManager.

    • A DefaultFormattingConversionService.

    • An OptionalValidatorFactoryBean if a JSR-303 implementation is available on the classpath.

    • A range of HttpMessageConverters depending on the third-party libraries available on the classpath. 

    The flow diagram looks like below:

    Spring MVC application context initialization flow

    Spring Framework Spring Boot application

    Opinions expressed by DZone contributors are their own.

    Related

    • Distributed Tracing System (Spring Cloud Sleuth + OpenZipkin)
    • Java, Spring Boot, and MongoDB: Performance Analysis and Improvements
    • How To Build Web Service Using Spring Boot 2.x
    • Migration of Microservice Applications From WebLogic to Openshift

    Comments

    Partner Resources

    X

    ABOUT US

    • About DZone
    • Send feedback
    • Careers
    • Sitemap

    ADVERTISE

    • Advertise with DZone

    CONTRIBUTE ON DZONE

    • Article Submission Guidelines
    • Become a Contributor
    • Visit the Writers' Zone

    LEGAL

    • Terms of Service
    • Privacy Policy

    CONTACT US

    • 3343 Perimeter Hill Drive
    • Suite 100
    • Nashville, TN 37211
    • support@dzone.com

    Let's be friends: