基于Servelet手写实现Spring MVC V1.0

1.自定义注解:

package com.yzf.demo.mvcframework.annotation;

import java.lang.annotation.*;

/**
 * Created by 于占峰 on 2020/3/30/030.
 * 自动注入注解
 */
@Target({ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface YZFAutowired {
    String value() default "";
}



package com.yzf.demo.mvcframework.annotation;

import java.lang.annotation.*;

/**
 * Created by 于占峰 on 2020/3/30/030.
 * 控制层注解
 */
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface YZFController {
    String value() default "";
}


package com.yzf.demo.mvcframework.annotation;

import java.lang.annotation.*;

/**
 * Created by 于占峰 on 2020/3/30/030.
 * 请求注解
 */
@Target({ElementType.TYPE,ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface YZFRequestMapping {
    String value() default "";
}


package com.yzf.demo.mvcframework.annotation;

import java.lang.annotation.*;

/**
 * Created by 于占峰 on 2020/3/30/030.
 * 参数注解
 */
@Target({ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface YZFRequestParam {
    String value() default "";
}


package com.yzf.demo.mvcframework.annotation;

import java.lang.annotation.*;

/**
 * Created by 于占峰 on 2020/3/30/030.
 * service层注解
 */
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface YZFService {
    String value() default "";
}

2.Bean

package com.yzf.demo.mvcframework.controller;

import com.yzf.demo.mvcframework.annotation.YZFAutowired;
import com.yzf.demo.mvcframework.annotation.YZFController;
import com.yzf.demo.mvcframework.annotation.YZFRequestMapping;
import com.yzf.demo.mvcframework.annotation.YZFRequestParam;
import com.yzf.demo.mvcframework.service.IDemoService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Created by 于占峰 on 2020/3/30/030.
 */
@YZFController
@YZFRequestMapping("/demo")
public class DemoController {

    @YZFAutowired
    private IDemoService demoService;

    @YZFRequestMapping("/select")
    public void query(HttpServletRequest req, HttpServletResponse resp,@YZFRequestParam("key")String key){
        try {
            resp.getWriter().write(demoService.query(key));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    @YZFRequestMapping("/add")
    public void add(HttpServletRequest req, HttpServletResponse resp,@YZFRequestParam("key")String key){
        try {
            resp.getWriter().write(demoService.add(key));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    @YZFRequestMapping("/update")
    public void update(HttpServletRequest req, HttpServletResponse resp,@YZFRequestParam("key")String key){
        try {
            resp.getWriter().write(demoService.update(key));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    @YZFRequestMapping("/remove")
    public void remove(HttpServletRequest req, HttpServletResponse resp,@YZFRequestParam("key")String key){
        try {
            resp.getWriter().write(demoService.remove(key));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}



/**
 * Created by 于占峰 on 2020/3/30/030.
 */
@YZFService
public class DeserviceImpl implements IDemoService {

    @Override
    public String query(String value) {
        return "SUCCESS:"+value;
    }

    @Override
    public String add(String value) {
        return "SUCCESS:"+value;
    }

    @Override
    public String update(String value) {
        return "SUCCESS:"+value;
    }

    @Override
    public String remove(String value) {
        return "SUCCESS:"+value;
    }
}



/**
 * Created by 于占峰 on 2020/3/30/030.
 */

public interface IDemoService {

    public String query(String value);
    public String add(String value);
    public String update(String value);
    public String remove(String value);

}

3.核心功能实现代码

package com.yzf.demo.mvcframework.v1.servlet;

import com.yzf.demo.mvcframework.annotation.*;

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.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.net.URL;
import java.util.*;

/**
 * Created by 于占峰 on 2020/3/30/030.
 * 手写实现Spring MVC 流程 V1.0
 */
public class YZFDispatchServlet extends HttpServlet {
    //Properties文件对象
    private Properties contextConfig = new Properties();


    //用于装入所有类名
    private List<String> classNames = new ArrayList<String>();

    //IoC容器
    private Map<String, Object> ioc = new HashMap<String, Object>();

    //存储URL和方法的handlerMapping
    private Map<String, Method> handlerMapping = new HashMap<String, Method>();

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

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        try {
            // 根据URL寻找对应的Method 并通过HttpServletResponse返回结果
            doDispath(req, resp);
        } catch (Exception e) {
            e.printStackTrace();
            resp.getWriter().write("500 服务器发生异常" + Arrays.toString(e.getStackTrace()));
        }
    }

    private void doDispath(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        String url = req.getRequestURI();
        String contextPath = req.getContextPath();
        url = url.replace(contextPath, "").replace("/+", "/");

        //如果不存在该请求路径则抛出页面找不到异常
        if (!this.handlerMapping.containsKey(url)) {
            resp.getWriter().write("404 Not Found!!!");
        }

        Map<String, String[]> params = req.getParameterMap();

        Method method = this.handlerMapping.get(url);

        //处理形参
        //方法的形参
        System.out.println(url + "-------------------------------------------");
        System.out.println(method + "--------------------------------------------");
        Class<?>[] parameterTypes = method.getParameterTypes();
        Object[] paramValues = new Object[parameterTypes.length];

        for (int i = 0; i < parameterTypes.length; i++) {
            Class paramterType = parameterTypes[i];
            if (paramterType == HttpServletRequest.class) {
                paramValues[i] = req;
            } else if (paramterType == HttpServletResponse.class) {
                paramValues[i] = resp;
            } else if (paramterType == String.class) {
                Annotation[][] pa = method.getParameterAnnotations();
                for (int j = 0; j < pa.length; j++) {
                    for (Annotation annotations : pa[i]) {
                        if (annotations instanceof YZFRequestParam) {
                            String paramName = ((YZFRequestParam) annotations).value();
                            if (!"".equals(paramName.trim())) {
                                String value = Arrays.toString(params.get(paramName))
                                        .replace("\\[|\\]", "")
                                        .replace("\\s+", ",");
                                paramValues[i] = value;

                            }
                        }
                    }
                }
            }
        }
        String beanName = toLowerFirstCase(method.getDeclaringClass().getSimpleName());
        for (Map.Entry<String, Object> entry : ioc.entrySet()) {
            System.out.println(entry.getKey() + "++++++++++++++++++++++++++++++++++++");
            System.out.println(entry.getValue());
        }
        System.err.println(beanName + "----------------------");
        System.err.println(ioc.get(beanName) + "----------------------");
        System.err.println(paramValues + "----------------------");
        method.invoke(ioc.get(beanName), paramValues);
    }


    @Override
    public void init(ServletConfig config) throws ServletException {

        //1.加载配置文件
        doLoadConfig(config.getInitParameter("contextConfigLocation"));

        //2.扫描相关类
        doScanner(contextConfig.getProperty("scanPackage"));

        //3.初始化IoC容器,将扫描到的相关类实例化,装入IoC
        doInstance();

        //4.完成依赖注入
        doAutowired();

        //5.初始化HandlerMapping
        doInitHandlerMapping();

        System.err.println("初始化启动完成:"
                + ioc.size()
                + handlerMapping.size()
        );
    }

    private void doInitHandlerMapping() {
        //判断IoC内是否存在Bean
        if (ioc.isEmpty()) {
            return;
        }

        for (Map.Entry<String, Object> entry : ioc.entrySet()) {
            Class<?> clazz = entry.getValue().getClass();
            //如果是控制层则跳过
            if (!clazz.isAnnotationPresent(YZFController.class)) {
                continue;
            }

            //获得可能是Controller类YZFRequestMapping注解上的URL
            String baseUrl = "";
            if (clazz.isAnnotationPresent(YZFRequestMapping.class)) {
                YZFRequestMapping requestMapping = clazz.getAnnotation(YZFRequestMapping.class);
                baseUrl = requestMapping.value();
            }

            //反射获得所有的public方法
            for (Method method : clazz.getMethods()) {
                //如果没有requestMapping注解则跳过
                if (!method.isAnnotationPresent(YZFRequestMapping.class)) {
                    continue;
                }
                YZFRequestMapping requestMapping = method.getAnnotation(YZFRequestMapping.class);
                //拼接完整的请求URL
                String url = (baseUrl.replace("/+", "/") + requestMapping.value().replace("/+", "/"));
                handlerMapping.put(url, method);
                System.err.println("Mapping:" + url + "---" + method + "========");
            }
        }
    }

    private void doAutowired() {
        //判断IoC内是否存在Bean
        if (ioc.isEmpty()) {
            return;
        }

        //迭代所有Bean
        for (Map.Entry<String, Object> stringObjectEntry : ioc.entrySet()) {
            //取出所有修饰字段 包括私有的
            for (Field field : stringObjectEntry.getValue().getClass().getDeclaredFields()) {
                //如果当前字段没有存在AutoWired注解则跳过
                if (!field.isAnnotationPresent(YZFAutowired.class)) {
                    continue;
                }

                //获得注解对象
                YZFAutowired yzfAutowired = field.getAnnotation(YZFAutowired.class);

                //如果用户没有自定义BeanName,就默认根据类型注入
                String beanName = yzfAutowired.value().trim();
                if ("".equals(beanName)) {
                    beanName = field.getType().getName();
                }
                //设置字段暴力访问
                field.setAccessible(true);

                //通过接口的全名拿到接口的实现类实例
                try {
                    field.set(stringObjectEntry.getValue(), ioc.get(beanName));
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    private void doInstance() {
        //判断是否取得类名集合
        if (classNames.isEmpty()) {
            return;
        }
        //遍历类名集合
        for (String className : classNames) {
            try {
                //得到class对象
                System.err.println(className + "=================================================");
                Class<?> clazz = Class.forName(className);
                //判断是否存在Controller注解
                if (clazz.isAnnotationPresent(YZFController.class)) {
                    //生成key(小写首字母简单类名) value 实例化Bean
                    String beanName = toLowerFirstCase(clazz.getSimpleName());
                    Object instance = null;
                    try {
                        instance = clazz.newInstance();
                    } catch (InstantiationException e) {
                        e.printStackTrace();
                    } catch (IllegalAccessException e) {
                        e.printStackTrace();
                    }
                    ioc.put(beanName, instance);
                } else if (clazz.isAnnotationPresent(YZFService.class)) {
                    //1. 在多个包下存在相同的类名,只能自己进行全局唯一命名
                    String beanName = clazz.getAnnotation(YZFService.class).value();
                    if ("".equals(beanName.trim())) {
                        beanName = toLowerFirstCase(clazz.getSimpleName());
                    }

                    //2.默认的类名首字母小写
                    Object istance = null;
                    try {
                        istance = clazz.newInstance();
                    } catch (InstantiationException e) {
                        e.printStackTrace();
                    } catch (IllegalAccessException e) {
                        e.printStackTrace();
                    }
                    ioc.put(beanName, istance);

                    //3.如果是接口
                    //判断有多少个实现类,如果只有一个就默认选择这个实现类 否则抛出异常
                    for (Class<?> i : clazz.getInterfaces()) {
                        if (ioc.containsKey(i.getName())) {
                            try {
                                throw new Exception(i.getName() + "接口存在多个实现类");
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                        ioc.put(i.getName(), istance);
                    }
                } else {
                    //不存在Controller注解
                    continue;
                }
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
        }
    }


    private void doScanner(String scanPackage) {
        URL url = this.getClass().getClassLoader().getResource("/" + scanPackage.replace(".", "/"));
        File classPath = new File(url.getFile());

        //迭代文件夹
        for (File file : classPath.listFiles()) {
            //短短是否为文件夹 是则继续迭代
            if (file.isDirectory()) {
                doScanner(scanPackage + "." + file.getName());
            } else {
                //判断是否为.class结尾的文件
                if (!file.getName().endsWith(".class")) {
                    continue;
                }
                //取得全类名
                String className = (scanPackage + "." + file.getName().replace(".class", ""));
                classNames.add(className);
            }
        }
    }

    private void doLoadConfig(String contextConfigLocation) {
        //获得流
        InputStream is = this.getClass().getClassLoader().getResourceAsStream(contextConfigLocation);
        try {
            //加载读取配置文件
            contextConfig.load(is);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (null != is) {
                try {
                    //关闭流
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    /**
     * 首字母转小写
     *
     * @param simpleName
     * @return
     */
    private String toLowerFirstCase(String simpleName) {
        char[] chars = simpleName.toCharArray();
        chars[0] += 32;
        return String.valueOf(chars);
    }
}

4.pom依赖

    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>servlet-api</artifactId>
      <version>2.4</version>
      <scope>provided</scope>
    </dependency>