[Spring] 30個類手寫 Spring Mini 版本系列(一)

[Spring] 30個類手寫 Spring Mini 版本系列(一)

簡介java

爲了更深刻的瞭解 Spring 的實現原理和設計思想,一直打算出個系列文章,從零開始從新學習 Spring。有興趣的小夥伴能夠持續關注更新。ios



手機用戶請橫屏獲取最佳閱讀體驗,REFERENCES中是本文參考的連接,如須要連接和更多資源,能夠關注其餘博客發佈地址。git

平臺 地址
CSDN https://blog.csdn.net/sinat_28690417
簡書 https://www.jianshu.com/u/3032cc862300
我的博客 https://yiyuer.github.io/NoteBooks/

正文github


基本思路web

  • 配置階段spring

  • 配置 web.xml編程

    • DispatchSevletapi

  • 設定 init-param瀏覽器

    • contextConfigLocation = classpath:application.xml緩存

  • 設定url-pattern

    • /*

  • 定義Annotation

    • @Controller

    • @Service

    • @Autowried

    • @RequestMapping

  • 初始化階段

  • 調用 init() 方法

    • 加載配置文件

  • IOC容器初始化

    • Map

  • 掃描相關的類

    • Scan-package="com.yido"

  • 建立實例化並保存至容器

    • 經過反射機制將類實例化放入 IOC 容器

  • 進行 DI

    • 掃描 IOC 容器中的實例,給沒有賦值的屬性自動賦值

  • 初始化 HandlerMapping

    • 將 URL 和 Method 創建一對一的映射關係

  • 運行階段

  • 調用 doPost() / doGet()

    • Web 容器調用 doPost() / doGet() ,得到 request / response 對象

  • 匹配 HandlerMapping

    • 從 request 對象中獲取用戶輸入的 url , 找到對應的 Method

  • 反射調用 method.invoke()

    • 利用反射調用方法並返回結果

  • 返回結果

    • 利用 response.getWriter().write(), 將返回結果輸出到瀏覽器


V1 版本

準備工做

pom.xml

  • sevlet-api 依賴

  • jetty 插件

V1.0.0 版本

註解定義

x

定義 Controller 和 Service

x

定義配置

  • src/main/resources/application.properties

scanPackage=com.yido.demo
  • 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/j2ee" xmlns:javaee="http://java.sun.com/xml/ns/javaee"
         xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
         xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
         version="2.4">

    <display-name>Sun Web Application</display-name>

    <!--定義核心調度 servlet -->
    <servlet>
        <servlet-name>mvc-servlet</servlet-name>
        <servlet-class>com.yido.mvcframework.v1.servlet.XDispatchServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>application.properties</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>mvc-servlet</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>

    <!--直接經過 servlet api 使用-->
    <servlet>
        <servlet-name>sun-servlet</servlet-name>
        <servlet-class>com.yido.simple.HelloServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>sun-servlet</servlet-name>
        <url-pattern>/hello</url-pattern>
    </servlet-mapping>
</web-app>

定義 DispatchServlet

/*
 * @ProjectName: 編程學習
 * @Copyright:   2019 HangZhou Ashe Dev, Ltd. All Right Reserved.
 * @address:     https://yiyuery.github.io/NoteBooks/
 * @date:        2020/4/12 5:30 下午
 * @description: 本內容僅限於編程技術學習使用,轉發請註明出處.
 */

package com.yido.mvcframework.v1.servlet;

import com.yido.mvcframework.annotation.XAutowired;
import com.yido.mvcframework.annotation.XController;
import com.yido.mvcframework.annotation.XRequestMapping;
import com.yido.mvcframework.annotation.XService;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.URL;
import java.util.*;

/**
 * <p>
 * 手寫一個 請求轉發器 DispatchServlet
 * </p>
 *
 * @author Helios
 * @date 2020/4/12 5:30 下午
 */

public class XDispatchServlet extends HttpServlet {

    /**
     * key: 請求路由
     * value:
     * - 對應 Controller 實例
     * - Method 方法
     */

    private Map<String, Object> mapping = new HashMap<String, Object>();

    /**
     * Get 請求處理轉發
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    /**
     * Post請求處理轉發
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        try {
            doDispatch(req, resp);
        } catch (Exception e) {
            //出現異常,返回堆棧信息
            resp.getWriter().write("500 Exception " + Arrays.toString(e.getStackTrace()));
        }
    }

    /**
     * 統一轉發處理全部請求數據
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */

    private void doDispatch(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException, InvocationTargetException, IllegalAccessException {
        //1. 獲取參數和請求路徑
        String url = req.getRequestURI();
        String contextPath = req.getContextPath();
        //替換請求上下文
        url = url.replace(contextPath, "")
                //替換多餘 '/'
                .replaceAll("/+""/");
        // 2. 獲取請求處理器
        /**
         * {@link XDispatchServlet#init(ServletConfig)}
         * 從緩存的 requestMapping 中加載指定包路徑下的請求路徑對應的 Controller 處理器
         * - 配置 web.xml
         * - 初始化時 加載配置文件
         */

        if (!this.mapping.containsKey(url)) {
            resp.getWriter().write("404 Not Found!");
            return;
        }
        Method method = (Method) this.mapping.get(url);
        // 3. 解析參數並經過反射執行方法
        Map<String, String[]> parameterMap = req.getParameterMap();
        Object controller = this.mapping.get(method.getDeclaringClass().getName());
        method.invoke(controller, new Object[]{req, resp, parameterMap.get("name")[0]});
    }

    /**
     * 加載配置並緩存 Controller 實例和 初始化請求對應的 Method映射
     *
     * @param config
     * @throws ServletException
     */

    @Override
    public void init(ServletConfig config) throws ServletException {
        InputStream is = null;
        try {
            //1. 讀取參數
            Properties configContext = new Properties();
            is = this.getClass().getClassLoader().getResourceAsStream(config.getInitParameter("contextConfigLocation"));
            configContext.load(is);
            //2. IOC 構建容器, 掃描全部實例和請求方法映射
            doIoc(configContext.getProperty("scanPackage"));
            //3. DI 依賴注入
            doInjection();
        } catch (Exception e){
            e.printStackTrace();
        } finally {
            if (is != null) {
                try{
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        System.out.println("XSpring MVC Framework has been initialed");
    }

    /**
     * 依賴注入
     */

    private void doInjection() {
        Collection<Object> values = mapping.values();
        for (Object value : values) {
            if (null == value) {
                continue;
            }
            Class clazz = value.getClass();
            if (clazz.isAnnotationPresent(XController.class)) {
                Field[] fields = clazz.getDeclaredFields();
                for (Field field : fields) {
                    if (!field.isAnnotationPresent(XAutowired.class)) {
                        continue;
                    }
                    XAutowired autowired = field.getAnnotation(XAutowired.class);
                    String beanName = autowired.value();
                    if ("".equals(beanName)) {
                        beanName = field.getType().getName();
                    }
                    //注入依賴實例
                    field.setAccessible(true);
                    try {
                        field.set(value, mapping.get(beanName));
                    } catch (IllegalAccessException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }

    /**
     * IOC 加載全部請求處理方法映射和實例
     */

    private void doIoc(String scanPackage) throws ClassNotFoundException, IllegalAccessException, InstantiationException {
        doScanTask(scanPackage);
        Map<String, Object> cacheMap = new HashMap<String, Object>();
        for (String clazzName : mapping.keySet()) {
            if (!clazzName.contains(".")) {
                continue;
            }
            Class<?> clazz = Class.forName(clazzName);
            // 1. 處理 XController
            String baseUrl = "";
            if (clazz.isAnnotationPresent(XController.class)) {
                cacheMap.put(clazzName, clazz.newInstance());
                // 1.1 解析請求路徑前綴
                if (clazz.isAnnotationPresent(XRequestMapping.class)) {
                    XRequestMapping requestMapping = clazz.getAnnotation(XRequestMapping.class);
                    baseUrl = requestMapping.value();
                }
                // 1.2 解析方法
                Method[] methods = clazz.getDeclaredMethods();
                for (Method method : methods) {
                    if (method.isAnnotationPresent(XRequestMapping.class)) {
                        XRequestMapping annotation = method.getAnnotation(XRequestMapping.class);
                        String url = baseUrl + annotation.value();
                        cacheMap.put(url, method);
                        System.out.println("> Mapped--------->url: " + url + "," + method.getName());
                    }
                }
                // 2. 處理 XService
            } else if (clazz.isAnnotationPresent(XService.class)) {
                XService service = clazz.getAnnotation(XService.class);
                String beanName = service.value();
                if ("".equals(beanName)) {
                    beanName = clazzName.getClass().getName();
                }
                Object instance = clazz.newInstance();
                cacheMap.put(beanName, instance);
                for (Class<?> i : clazz.getInterfaces()) {
                    cacheMap.put(i.getName(),instance);
                }
            }
        }
        if (!cacheMap.isEmpty()) {
            this.mapping.putAll(cacheMap);
        }
    }

    /**
     * 掃描指定包路徑下全部需實例類
     * @param scanPackage
     */

    private void doScanTask(String scanPackage) {
        URL url = this.getClass().getClassLoader().getResource("/" + scanPackage.replaceAll("\\.""/"));
        File rootDir = new File(url.getFile());
        for (File file : rootDir.listFiles()) {
            if (file.isDirectory()) {
                doScanTask(scanPackage+"."+file.getName());
            }else{
                if (!file.getName().endsWith(".class")) {
                    continue;
                }
                String clazzName = scanPackage + "." + file.getName().replace(".class""");
                mapping.put(clazzName, null);
            }
        }
    }
}

效果演示

/**
     * 返回歡迎信息
     * support:
     *  spring-v1
     * @param name
     */

    @XRequestMapping("/v1/welcome")
    public void welcome(HttpServletRequest req, HttpServletResponse resp, @XRequestParam(value = "name") String name) {
        String result = helloService.welcome(name);
        try {
            resp.getWriter().write(result);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
.

小結

  • XDispatchServlet 職責不夠單一

  • 流程方法混亂,不清晰

  • XDispatchServlet 須要進行重構

  • 沒有自動解析注入 Request 、Response 對象

  • Spring 思想沒有體現,沒有 ApplicationContext、BeanDefinition、BeanDefinitionReader,沒有解決循環依賴問題

  • 沒有 Aop 邏輯

  • To do Continue…


更多

掃碼關注架構探險之道,回覆『源碼』,獲取本文相關源碼和資源連接

.

知識星球(掃碼加入獲取歷史源碼和文章資源連接)

.


本文分享自微信公衆號 - 架構探險之道(zacsnz1314)。
若有侵權,請聯繫 support@oschina.cn 刪除。
本文參與「OSC源創計劃」,歡迎正在閱讀的你也加入,一塊兒分享。