台湾国

First, the overall overview

Spring is now more than just a framework. Spring has become an ecosystem. But very few people know much about Spring. Here, I will show you how I wrote Spring by hand. I will use less than 400 lines of code to describe the essence of Spring IOC, DI, and MVC, combined with more than a decade of experience in Spring research, and keep the basic functionality intact.

First, let’s take a look at the three phases of Spring, configuration, initialization, and run (as shown below) : 

Configuration phase: Mainly completes the application.xml configuration and Annotation configuration.

Initialization phase: mainly load and parse configuration information, then initialize IOC container, complete DI operation of container, and complete initialization of HandlerMapping.

Run stage: mainly after the completion of the Spring container startup, the completion of the internal scheduling of user requests, and return the response results.

Let’s take a look at our project structure (as shown below)

Second, the configuration stage

I use Maven to manage projects. Looking at the configuration in the POM.xml file, I only refer to the servlet-API dependencies.

Then, create the GPDispatcherServlet class and inherit HttpServlet, overriding the init(), doGet(), and doPost() methods.

 

Configure the following information in the web.xml file:

In the initialization we configured with a load of the Spring configuration file path, in the frame of the native, we should configuration is the classpath: application. XML. Here, we use the Properties file instead of the XML file for simplicity. Here’s what’s in the Properties file:

Create a GPService annotation:

Create GPAutowired annotations:

Create a GPRequestParam comment:

Configure with custom annotations:

Add @gpService annotation:

At this point, we complete the configuration phase of the code by hand.

3. Initialization phase

Start by declaring a few member variables in the GPDispatcherServlet:

When the Servlet container is started, the init() method of GPDispatcherServlet is called. From the parameter of init method, we can get the path to the main configuration file and read the information in the configuration file. Now that we’ve covered the three phases of Spring, it’s time to complete the initialization phase of the code. In the init() method, the execution steps are defined as follows:

An implementation of the doLoadConfig() method that reads a file into a Properties object:

The doScanner() method recursively scans all Class files

The doInstance() method initializes all related classes and puts them into the IOC container. The default key of the IOC container is the first letter of the class name. If you set the class name yourself, the custom key is preferred. Therefore, write a utility method for handling the first letter of a class name.

Then, you can process the related classes.

The doAutowired() method, which initializes classes in the IOC container, assigns values to fields that need to be assigned

InitHandlerMapping () associates information configured in GPRequestMapping with Method and saves these relationships.

At this point, all the code for the initialization phase has been written.

4. Operation stage

In the run phase, when the Servlet accepts the request sent by the user, the doPost method is always called. I first call doDispach() from the doPost method and the code is as follows:

The doDispatch() method is written like this:

5. Effect demonstration

At this point, we have completed a mini version of Spring that is small but has all the essentials. We publish the service to the web container, then input the browser: http://localhost:8080/demo/query.json? Name =Tom, you get the following result:

True Spring is a lot more complicated, of course, but the core design idea is basically the same.

台湾国

OK, here, a mini version of Spring MVC development is complete, before, I used to use Spring MVC, never thought I could develop one by myself, this is my first time, my energy is beyond my imagination, haha ~

** More attention to the public account: JAVA architecture advanced road, reply ‘ali’ to obtain Ali interviewer manual **