Cute Spring is already more than just a framework of. Today, Spring has become an eco. But in-depth understanding of Spring are few. Here, I brought you a look at how Spring's my handwriting. I will combine research experience of the Spring ten years, with less than 400 lines of code to describe SpringIOC, DI, MVC is the essence of design, and to ensure that the basic functions of integrity.
First, let's introduce the three stages of Spring, the configuration phase, the initialization phase and the operational phase (as shown):
Configuration phase: notably the completion of the configuration and Annotation application.xml configuration.
Initialization phase: mainly load and parse the configuration information, and then, initialization IOC container, the container DI complete operation has been completed HandlerMapping initialization.
Operation phase: mainly scheduled completion of the internal container Spring start after completion of the user request, and returns a response result.
Let's look at our project structure (as shown below)
First, the configuration phase
I used the maven project management. First look at pom.xml configuration file, I just quoted servlet-api dependency.
Then, create GPDispatcherServlet class and inherit HttpServlet, rewrite init (), doGet () and doPost () method.
Configure the following information in the web.xml file:
In <init-param>, we configure a path to the main Spring initialization configuration file loaded in a native frame, we should be configured classpath: application.xml. Here, we have to streamline operations, instead of xml file properties file. The following is the content properties file:
Next, we want to configure annotations. Now, we do not use Spring's stitch, all hand-written notes of all their own.
Creating GPController notes:
Creating GPRequestMapping notes:
Creating GPService notes:
Creating GPAutowired notes:
Creating GPRequestParam Notes:
Use custom annotation to configure:
This, we all handwritten code configuration phase is completed.
Second, the initialization phase
先在GPDispatcherServlet中声明几个成员变量:
当Servlet容器启动时,会调用GPDispatcherServlet的init()方法,从init方法的参数中,我们可以拿到主配置文件的路径,从能够读取到配置文件中的信息。前面我们已经介绍了Spring的三个阶段,现在来完成初始化阶段的代码。在init()方法中,定义好执行步骤,如下:
doLoadConfig()方法的实现,将文件读取到Properties对象中:
doScanner()方法,递归扫描出所有的Class文件
doInstance()方法,初始化所有相关的类,并放入到IOC容器之中。IOC容器的key默认是类名首字母小写,如果是自己设置类名,则优先使用自定义的。因此,要先写一个针对类名首字母处理的工具方法。
然后,再处理相关的类。
doAutowired()方法,将初始化到IOC容器中的类,需要赋值的字段进行赋值
initHandlerMapping()方法,将GPRequestMapping中配置的信息和Method进行关联,并保存这些关系。
到此,初始化阶段的所有代码全部写完。
三、运行阶段
来到运行阶段,当用户发送请求被Servlet接受时,都会统一调用doPost方法,我先在doPost方法中再调用doDispach()方法,代码如下:
doDispatch()方法是这样写的:
到此,我们完成了一个mini版本的Spring,麻雀虽小,五脏俱全。我们把服务发布到web容器中,然后,在浏览器输入:http://localhost:8080/demo/query.json?name=Tom,就会得到下面的结果:
当然,真正的Spring要复杂很多,但核心设计思路基本如此。例如:Spring中真正的HandlerMapping是这样的:
Plus Java architecture architecture circle: 867,857,579 collect information, which will be free to share some video recordings senior architect: There are Spring, MyBatis, Netty source code analysis, high concurrency, high performance, distributed micro-services architecture principles, JVM performance optimization these become the architect of essential information