Quick learning - Spring integrates Spring MVC

Chapter 12 Spring integrating Spring MVC

12.1 integration of spring and spring MVC:

  1. Do you need to integrate Spring MVC with Spring?
  2. Do you need to add the IOC container of Spring?
  3. Do you need to configure the ContextLoaderListener in the web.xml file to start the Spring IOC container?

Need: usually, other frameworks like data source, transaction and integration are put in the configuration file of Spring (not in the configuration file of Spring MVC)
In fact, the IOC container corresponding to the Spring configuration file contains the Service and Dao
No need: all of them are placed in the configuration file of Spring MVC. You can also divide the configuration files into several Spring ones, and then use the import node to import other configuration files

  1. How to start the Spring IOC container?
    Non WEB Environment: directly in the main method or junit test method through the new operation to create
    WEB Environment: we hope spring IOC container will be created when WEB application server is started
    Listen to the creation of the ServletContext object through the listener, and create the SpringIOC container when the ServletContext object is created.
    In addition, the container object is bound to the ServletContext so that all web components can share the IOC container object

12.2 Spring integrates spring MVC solution configuration listener

  1. Monitor configuration
<!-- Configuration startup Spring IOC Container Listener -->
<context-param>
	<param-name>contextConfigLocation</param-name>
	<param-value>classpath:beans.xml</param-value>
</context-param>
<listener>
	<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
  1. Create the configuration file of Spring's bean: beans.xml
<?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-4.0.xsd">
 
<!-- Set package for scan component -->
<context:component-scan base-package="com.atguigu.springmvc"/>
 
<!-- Configure data sources, Integrate other frameworks, Affairs, etc.. -->
 
</beans>
  1. Springmvc configuration file: springmvc.xml
<?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"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
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-4.0.xsd">
 
<!-- Set package for scan component -->
<context:component-scan base-package="com.atguigu.springmvc"/> 
<!-- Configure view resolver -->
<bean id="internalResourceViewResolver"
   class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"/>
<property name="suffix" value=".jsp"/>
</bean>
 
<mvc:default-servlet-handler/> 
<mvc:annotation-driven/> 
</beans>

Add construction methods in HelloWorldHandler and UserService classes, start the server, and check the execution of the constructor.
Problem: if there are overlaps between the IOC container of Spring and the package scanned by the IOC container of Spring MVC, some bean s will be created twice

Solve:
The package scanned by Spring's IOC container does not coincide with the package scanned by Spring MVC's IOC container
Use the exclude filter and include filter child nodes to specify only scanned annotations

springmvc.xml

<context:component-scan base-package="com.atguigu.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>

beans.xml

<context:component-scan base-package="com.atguigu.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>
<!-- Configure data sources, Integrate other frameworks, Affairs, etc.. -->

12.3 relationship between spring IOC container and spring MVC IOC container

Beans in the IOC container of spring MVC can refer to bean s in the Spring IOC container
What about the return? Otherwise, it's not. The beans in the Spring IOC container can't refer to the beans in the spring MVC IOC container

  1. Referencing the Bean of the business layer in the Spring MVC configuration file
  2. Multiple Spring IOC containers can be parent-child to achieve good decoupling.
  3. The Spring MVC WEB layer container can be used as a sub container of the "business layer" Spring container:
    That is to say, the WEB layer container can refer to the Bean of the business layer container, while the business layer container cannot access the Bean of the WEB layer container
1349 original articles published, 1119 praised, 100000 visitors+
His message board follow

Tags: Spring xml encoding Junit

Posted on Wed, 12 Feb 2020 06:58:00 -0500 by jonemo