Spring MVC - integration with spring

Posted by clodagh2000 on Wed, 12 Feb 2020 11:28:29 +0100

I. overview

1.1 ask questions

  1. Spring MVC is a part of spring. Do you need to integrate spring MVC?
  2. Do you need to add Spring's IOC container?
  3. Do you need to configure the ContextLoadListener in the web.xml file to start the Spring IOC container?

1.2 problem solving

  1. Need. The configuration file of spring MVC is related to the website forwarding logic and website functions (view parser, file upload parser, ajax support...). Spring's configuration files are business related (transaction control, data sources...).
  2. There are also services and Dao that need to be put into the IOC container corresponding to the Spring configuration file.
  3. Both need and don't need, don't need: all the configuration files placed in Spring MVC, can also be divided into multiple Spring configuration files, and then use the import node to import other configuration files

2, Spring integration spring MVC experimental solution

We already know that spring and spring MVC are sub containers: Spring manages business logic components; spring MVC manages controller components;

1. Configure the listener for Spring startup and the front-end controller of Spring MVC in web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <!-- The front controller of this Spring Web application, responsible for handling all application requests -->

  <!-- Spring Monitor configuration -->
  <!-- needed for ContextLoaderListener -->

	<!-- Bootstraps the root web application context before servlet initialization -->

  <!-- SpringMVC Front end controller configuration -->

	<!-- Map all requests to the DispatcherServlet for handling -->
	<!-- Support Rest Style changing filter -->

2. Create the Spring configuration file: spring.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	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">

<context:component-scan base-package="com.test">
   <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>

<!--Configure data sources, integrate other frameworks, transactions, etc-->

3. Create the configuration file of spring MVC: springmvc.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	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">

<context:component-scan base-package="com.test" use-default-filters="false">

    <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>

  <!-- Configure view resolver -->
  <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="internalResouceViewResolver">
     <property name="suffix" value="/WEB-INF/pages/"></property>
     <property name="prefix" value=".jsp"></property>



Problem: if only the package scanning component is set, if there are overlaps between the IOC container of Spring and the IOC container of Spring MVC, bean s will be created twice. Solution: use the exclude filter and include filter child nodes to specify the scanned annotations.

4. Test: create BookController and BookService, add construction methods to these two classes, start the server, and view the execution of the constructor.

package com.test.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.test.service.BookService;

public class BookController {
	private BookService bookService;
	public BookController()
	public String hello(){
		return "forward:/index.jsp";


package com.test.service;

import org.springframework.stereotype.Service;

public class BookService {

	public BookService(){

3, The relationship between Spring IOC container and spring MVC IOC container

In the above integration, the two components of Controller and Service are respectively in different containers, but Service can be automatically injected into Controller. This shows that the beans in the IOC container of Spring MVC can reference the beans in the Spring container. What's the reverse? Not vice versa. Beans in the Spring IOC container cannot refer to beans in the Spring MVC IOC container.

  • Reference the bean of the business layer in the spring MVC configuration file.
  • Multiple Spring IOC containers can be set as parent-child relationship to achieve good decoupling
  • The Spring MVC WEB container can be used as a sub container of the "business layer" Spring container: that is, 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 container.

4, Spring MVC vs. Struts

① . the entry of Spring MVC is Servlet, while Struts2 is Filter

② Spring MVC is slightly faster than struts 2. Spring MVC is based on method design, Sturts2 is based on class, and every request will instance an Action

③ . Spring MVC is simpler to use, and its development efficiency is indeed higher than struts 2: it supports JSR 303 and is more convenient to handle ajax requests

④ . the OGNL expression of struts 2 makes the page development more efficient than Spring MVC

97 original articles published, praised 11, 20000 visitors+
Private letter follow

Topics: Spring xml encoding JSP