preface

Text has been included to my lot warehouse, welcome Star:https://github.com/bin392328206 plant a tree is the best time ten years ago, followed by now

six-finger-web

The wheels of a Web backend framework roll themselves (easy) from handling Http requests [Netty request-level Web servers] to MVC [interface encapsulating and forwarding], to IOC [dependency injection], to AOP [aspect], to RPC [remote procedure calls] and finally to ORM [database operations].

github

Why the wheel

Actually is such, small six six oneself peacetime? Sometimes like to look at the source code such as Spring, but the level may not how, every time dazed at all, and then feel the inside of the details too difficult, then I can only view the overall thought, then I think if I can according to each elder some thought, rolled out a simple wheel, Is it easier for me to understand the author’s ideas? So six-Finger-Web came out, and it was really a learning process for me, and THEN I opened it up to help those of you who are having trouble learning the source code. In addition, we can exercise our coding ability, because we usually use Java API for CRUD. As time goes by, we are not familiar with many framework API classes, so we take this opportunity to exercise.

The characteristics of

  • Built-in HTTP server written by Netty, without additional dependence on Web services such as Tomcat.
  • Code is simple to understand (small 66 can not write their own framework big guy that kind of high clustering, low coupling code), ability a little bit stronger to see the code can understand, weakness also does not matter, small 66 has a supporting from 0 build tutorial.
  • Support MVC-related annotations to ensure that they are used similarly to SpringMVC
  • Support for Spring IOC and Aop related features
  • Support similar to Mybatis related functions
  • Supports RPC-related functionality similar to Dubbo
  • For data returns, only the Json format is supported

omg

This tutorial is only suitable for intermediate level, because the author’s own level is not high, do not like spray, today is the second article, implement a simple SpringMVC, reference is Guide brother jsonCat. Ok, let me give you one by one to go through the construction process

  • Suitable for beginners and intermediate Java programmer training manual to build the entire Web project from 0 (A)

It is recommended to download the source code while looking at it, if there is a problem

Summary package structure

So let’s first look at the package structure once we’ve written MVC, just so you know what’s going on


The modification of pom

First step, I will show you the new place of POM file


One of them is reflection, because writing wheels, reflection must be used a lot, and another is the JSON conversion tool

Concrete MVC implementation

So I’m going to give you the general idea here, but this is just a simple implementation, and I still have a lot of details

First, let’s take a look at what our business looks like, as shown below

Ok, so that’s what we’re going to end up with, right, what does our browser request look like?

It looks like this, and it’s pretty clear that what SpringMVC is going to do is take different approaches for different requests, so that’s the core idea. In general, there are three parts

  • One is the transformation of the original server
  • One is the initialization of springMVC related container components
  • One is the request flow handling of HTTP requests

Now, LET me explain it one by one

Modify the request part of the Netty server

Here I solve the problem and then find the process of class method to you, you go to see the source code

HttpRequestHandler->channelRead0


Actually not the server side need to reform, is through the request, to find a different processor, and then to deal with our business, and then according to the return value of the business, then we could go to encapsulate some success or failure, then side to see server package the following class, specific code, we will go to the oneself, each class basically have comments

Initialization of springMVC related components

First let’s take a look at our startup class, class ApplicationServer

Then we can go down with the code, in fact, we look at the source code is also the same, but the source code details, after all, so difficult point

Class DispatcherMethodMapper – > loadRoutes

This class of things is still more, basically a lot of places I wrote notes, if you do not understand can privately consult, and then we roughly to analyze it!

  • The first one must be the scan path to load the package, and since I’m annotation-based, I configure the scan path in the configuration file
  • And then I’m going to go through the.class file inside, and then I’m going to find the annotation on the class, if there’s a RestController that says it’s a class that handles requests, and I’m going to go through the methods in that class, And then I’m going to analyze PostMapping RequestMapping GetMapping if else and then I’m going to take those methods, and I’m going to encapsulate the URL of the request into a SpringMVC container, and when the request comes in, I’m going to match it in the container, and then I’m going to find the method, To execute the method, fill in the corresponding parameters. I suggest you follow the source code

Then comes our main flow, let’s look at the theme flow how to go

HttpRequestHandler->channelRead0

So let’s look at what’s in this method RequestHandlerFactory->create In fact, according to the different request mode, find the corresponding processing class

HttpRequestHandler->channelRead0And then, you see, this method, which is the main thing, which is the core of what we’re going to do, we’re going to go down and find an interface with the RequestHandler

Suppose we’re asking Get to find its implementation class

GetRequestHandler-> Handle And this is where the logic really comes in. Let’s go down

Encapsulate request parameters and path parameters

DispatcherMethodMapper->getMethodDetail

MethodDetail->build


This is the core fill method. When the fill is complete, we get an object. This object is quite powerful

Then down

ParameterResolver->resolve is the core method in ParameterResolverFill it in different ways, and then when it’s done


Use reflection to get the current object to be executed, and then use reflection to execute the corresponding method, so that all the FLOW of MVC is finished

Let’s look at the result of the request

RestController to execute

Small request http://localhost:8081/user/ write MVC


At the end

Well, we finished the MVC small process, the code has also uploaded, I hope we study hard, refueling together, behind will be rui IOC and AOP. Take the code down and run through it. It’s not more than a dozen classes, and the average class is about 100 lines.

Daily for praise

Ok, everybody, that’s all for this article, you can see people here, they are real fans.

Creation is not easy, your support and recognition, is the biggest power of my creation, our next article

Six pulse excalibur | article “original” if there are any errors in this blog, please give criticisms, be obliged!