Spring Quick Start Raiders to Build Backend Web Projects (5)

Chapter 4 - Anatomy of the SpringMVC Architecture

foreword

Next, we start to learn the SpringMVC architecture, which is the core idea of ​​the entire back-end project. Before that, you need to make sure that you have mastered the idea of ​​MVC.


1. Architecture diagram


2. Analysis

1. In terms of layers

First of all, the entire architecture can be roughly regarded as a B/S architecture (the database is also included in the server, because relative to the user, it does not care who your server needs to interact with), we only need to focus on the generalized "S" "Server" section in the "Server" section. (This architecture diagram is also fully applicable to the C/S architecture)

The server side, from top to bottom, is roughly divided into four levels:

  • Servlet
  • Controller
  • Service
  • Dao

Among them, the Servlet layer includes DispatcherServlet and Handler Mapping, which is completed by the Spring framework, but we need to configure it in xml.

What developers need to complete is the configuration of the remaining three levels and some related xml files.


2. In terms of function

DispatcherServlet can be understood as a customs, all front-end requests and back-end responses pass through here.

Handler Mapping is a navigation map of the entry and exit hall, telling where the entry request should go.

The Controller is the guide who is waiting for the request at the door with the XXX request reception desk. He is only responsible for bringing the request to the destination or bringing the response back to the exit.

Service is the person in charge of the business, arranges the relevant Dao to operate the database according to the request, and then sorts or filters or packs the results to the Controller.

Dao is a database operator, and each Dao is responsible for a large type of business.

The Model wraps or transforms the results so that other layers can identify and distinguish them.

web.xml is the authority of the entire Server, and he has the final say in all levels.

ApplicationContext.xml can store various configuration parameter information, such as the authentication information of Dao operation database: database address, port, database name, account number, password, etc.


3. In terms of data flow

DispatcherServlet intercepts front-end requests and hands them over to Handler Mapping for distribution.

Handler Mapping is the mapping relationship between request information and Controller, and is distributed to the corresponding Controller according to the request information.

The Controller needs to be annotated with @Controller to tell the Spring framework that this is a Controller. Use the @RequestMapping annotation to set the mapped request information. Use the @RequestParam annotation to get the parameters contained in the request. The requested data is then handed over to the Service for processing.

Service needs to be annotated with @Service to tell the Spring framework that this is a Service. The Service selects the appropriate Dao to process the data according to the received data.

Dao needs to be annotated with @Repository to tell Spring framework that this is a Dao. Based on the received data, Dao obtains the authentication information of the database, performs database operations, and hands the results to the Model for packaging.

Model performs initial processing of the original results, including data type conversion, data combination, etc., and hands it to Dao after processing.

Dao hands data to Service.

The Service reprocesses the data from one or more Dao, and then packs the final result to the Controller.

The Controller needs to use the @ResponseBody annotation to tell the Spring framework that it should return the response body, and then pass the result from the Service to the front end through the DispatcherServlet.


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324661660&siteId=291194637