SpringMVC study notes | SpringMVC operation flowchart, Spring integration SpringMVC

table of Contents

First, the operation flowchart of SpringMVC

Two, Spring integrates SpringMVC


First, the operation flowchart of SpringMVC

 
13424350-7834b3134618d222.png
 

Two, Spring integrates SpringMVC

The reason why Spring integrates SpringMVC

Under normal circumstances, other frameworks similar to data sources, transactions, and integration are placed in the Spring configuration file, rather than in the SpringMVC configuration file. In fact, Service and Dao are also placed in the IOC container corresponding to the Spring configuration file.

Problems in Spring's integration of SpringMVC

We now create two classes, one for SpringMVC initialization, and the other for Spring initialization.

package com.cerr.springmvc;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class HelloWorld {
    @Autowired
    private UserService userService;
    public HelloWorld() {
        System.out.println("HelloWorld");
    }
    @RequestMapping(value = "/helloWorld")
    public String hello(){
        System.out.println("success");
        return "success";
    }
}
package com.cerr.springmvc;
import org.springframework.stereotype.Service;
@Service
public class UserService {

    public UserService() {
        System.out.println("UserService");
    }
}

SpringMVC configuration file:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <!-- 配置自动扫描的包-->
    <context:component-scan base-package="com.cerr.springmvc"/>

    <!-- 配置视图解析器-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/views/"/>
        <property name="suffix" value=".jsp"/>
    </bean>
</beans>

Spring configuration file:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <!-- 配置自动扫描的包 -->
    <context:component-scan base-package="com.cerr.springmvc"/>

    <!-- 配置数据源,整合其他框架,事务等 -->
</beans>

Then after opening Tomcat, I found that these two classes will be initialized twice. Because the package scanned by Spring's IOC container and the package scanned by SpringMVC's IOC container are fused, this will cause some beans to be created twice.

So how to solve it? There are two solutions

  • Make Spring's IOC container and SpringMVC's IOC container scan the package without overlapping parts, that is, plan the package reasonably so that the package is clearly separated, and then you can set it up
  • You can use the exclude-filterand subnodes in the node that automatically scans the package include-filterto specify the annotations that can only be scanned.

Let's illustrate the second solution:
For example, in this example, for SpringMVC, we only let scan Controllerand ControllerAdviceannotate classes.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <!-- 配置自动扫描的包-->
    <context:component-scan base-package="com.cerr.springmvc" use-default-filters="false">
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
        <context:include-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice" />
    </context:component-scan>

    <!-- 配置视图解析器-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/views/"/>
        <property name="suffix" value=".jsp"/>
    </bean>
</beans>

For Spring configuration files, we let it not scan Controllerand ControllerAdviceannotate classes.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <!-- 配置自动扫描的包 -->
    <context:component-scan base-package="com.cerr.springmvc" >
        <context:exclude-filter type="annotation"
                                expression="org.springframework.stereotype.Controller"/>
        <context:exclude-filter type="annotation"
                                expression="org.springframework.web.bind.annotation.ControllerAdvice"/>
    </context:component-scan>

    <!-- 配置数据源,整合其他框架,事务等 -->

</beans>

This will solve the problem.

The relationship between Spring's IOC container and SpringMVC's IOC container

Multiple Spring IOC containers can be set up as a parent-child relationship to achieve good decoupling. The SpringMVC WEB layer container can be used as a child container of the "business layer" Spring container. That is , the beans in the Spring MVC IOC container can refer to the beans in the Spring IOC container, but the beans in the Spring IOC container cannot refer to the beans in the Spring MVC IOC container.

Guess you like

Origin blog.csdn.net/qq_14810195/article/details/103166836