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
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
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

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Related

  • A Practical Guide to Creating a Spring Modulith Project
  • Distributed Tracing System (Spring Cloud Sleuth + OpenZipkin)
  • Java, Spring Boot, and MongoDB: Performance Analysis and Improvements
  • Spring Boot Secured By Let's Encrypt

Trending

  • Creating a Web Project: Caching for Performance Optimization
  • Secrets Sprawl and AI: Why Your Non-Human Identities Need Attention Before You Deploy That LLM
  • Exploring Intercooler.js: Simplify AJAX With HTML Attributes
  • Cloud Security and Privacy: Best Practices to Mitigate the Risks
  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.

By 
Abdus Mondal user avatar
Abdus Mondal
·
Jul. 27, 17 · Tutorial
Likes (9)
Comment
Save
Tweet
Share
58.2K 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

    • A Practical Guide to Creating a Spring Modulith Project
    • Distributed Tracing System (Spring Cloud Sleuth + OpenZipkin)
    • Java, Spring Boot, and MongoDB: Performance Analysis and Improvements
    • Spring Boot Secured By Let's Encrypt

    Partner Resources

    ×

    Comments
    Oops! Something Went Wrong

    The likes didn't load as expected. Please refresh the page and try again.

    ABOUT US

    • About DZone
    • Support and feedback
    • Community research
    • Sitemap

    ADVERTISE

    • Advertise with DZone

    CONTRIBUTE ON DZONE

    • Article Submission Guidelines
    • Become a Contributor
    • Core Program
    • 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:

    Likes
    There are no likes...yet! 👀
    Be the first to like this post!
    It looks like you're not logged in.
    Sign in to see who liked this post!