Integration between Spring and Spring MVC

Integration of Spring and Spring MVC

When using Spring MVC in a project, since Spring and Spring MVC are from the same origin, many developers often put all configurations into the Spring MVC configuration file without distinguishing between Spring and Spring MVC configurations. I am used to splitting the two configurations: Spring configures data sources, transactions, and integration with other frameworks, while Spring MVC configures web-related content.

Using only Spring MVC configuration

Let's first look at how to configure when only Spring MVC configuration is used

First configure web.xml, you only need to configure DispatcherServlet. Then put all Spring configurations and Spring MVC configurations into the springmvc.xml configuration file


    dispatcher
    
        org.springframework.web.servlet.DispatcherServlet
    
    
        contextConfigLocation
        classpath:springmvc.xml
    
    1


    dispatcher
    /

Distinguishing between Spring and Spring MVC configurations

This is the focus of this article. When integrating the two, we will have two configuration files, which form Spring's parent-child container: ContextLoaderListener loads the Spring configuration file and creates the parent container; DispatcherServlet loads the Spring MVC configuration file and creates the child container.

Let's look at how web.xml is configured in this case



    contextConfigLocation
    classpath:applicationContext.xml



    org.springframework.web.context.ContextLoaderListener


    dispatcher
    org.springframework.web.servlet.DispatcherServlet
    
        contextConfigLocation
        classpath:springmvc.xml
    
    1


    dispatcher
    /

Then configure related beans in the Spring and Spring MVC configuration files respectively

Here I will explain some problems you may encounter when integrating the two configurations

Duplicate bean creation

When the Spring MVC configuration file and the Spring configuration file are separated, since annotation-based configuration is very common now, most projects use component scanning.

If both configuration files use for component scanning, these components will be scanned by both configuration files, and all these beans will be created twice

At this point, you can use and to configure filtering rules

Spring MVC only needs to manage Controller controllers, so configure this in the Spring MVC configuration file


    

And the Spring configuration file only needs to exclude the components scanned by Spring MVC accordingly


    

Container relationship

The parent container is the Spring configuration loaded by ContextLoaderListener, and the child container is the Spring MVC configuration loaded by DispatcherServlet. The child container can access beans in the parent container, but the reverse is not true.

Obtaining the Spring container context

When the project starts, the listener will store the Spring context content in the application context during the web application environment initialization

servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, this.context);

So to retrieve it, you only need to get the application context first

req.getServletContext()

Then retrieve it directly

context.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE)

https://zhhll.icu/2021/framework/springmvc/basics/8.integration-of-spring-and-springmvc/

This article is published across multiple platforms via mdnice


This is a discussion topic separated from the original topic at https://juejin.cn/post/7369097402501038120