一、手写MVC框架

mimi版 - 图1

1 前置准

创建mvc模块是作为自定义的springmvc框架,不选用maven骨架。

2 配置pom.xml

  1. <properties>
  2. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  3. <maven.compiler.source>11</maven.compiler.source>
  4. <maven.compiler.target>11</maven.compiler.target>
  5. </properties>
  6. <dependencies>
  7. <dependency>
  8. <groupId>junit</groupId>
  9. <artifactId>junit</artifactId>
  10. <version>4.12</version>
  11. <scope>test</scope>
  12. </dependency>
  13. <dependency>
  14. <groupId>javax.servlet</groupId>
  15. <artifactId>javax.servlet-api</artifactId>
  16. <version>3.1.0</version>
  17. <scope>provided</scope>
  18. </dependency>
  19. <dependency>
  20. <groupId>org.apache.commons</groupId>
  21. <artifactId>commons-lang3</artifactId>
  22. <version>3.9</version>
  23. </dependency>
  24. </dependencies>

3 创建自定义注解

3.1 HuaAutowired注解

package com.hua.mvc.annotation;

import java.lang.annotation.*;

@Documented
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface HuaAutowired {
    String value() default "";
}

3.2 HuaController注解

import java.lang.annotation.*;

@Documented
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface HuaController {
    String value() default "";
}

3.3 HuaRequestMapping注解

import java.lang.annotation.*;

@Documented
@Target({ElementType.TYPE,ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface HuaRequestMapping {
    String value() default "";
}

3.4 HuaService注解

import java.lang.annotation.*;

@Documented
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface HuaService {
    String value() default "";
}

4 创建Handler

package com.hua.mvc.pojo;

import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Pattern;

public class HuaHandler {

    private Object controller; 
    // method.invoke(obj,)

    private Method method;

    private Pattern pattern; 
    // spring中url是支持正则的

    private Map<String,Integer> paramIndexMapping; 
    // 参数顺序,是为了进行参数绑定,key是参数名,value代表是第几个参数 <name,2>


    public HuaHandler(Object controller, Method method, Pattern pattern) {
        this.controller = controller;
        this.method = method;
        this.pattern = pattern;
        this.paramIndexMapping = new HashMap<>();
    }

    public Object getController() {
        return controller;
    }

    public void setController(Object controller) {
        this.controller = controller;
    }

    public Method getMethod() {
        return method;
    }

    public void setMethod(Method method) {
        this.method = method;
    }

    public Pattern getPattern() {
        return pattern;
    }

    public void setPattern(Pattern pattern) {
        this.pattern = pattern;
    }

    public Map<String, Integer> getParamIndexMapping() {
        return paramIndexMapping;
    }

    public void setParamIndexMapping(Map<String, Integer> paramIndexMapping) {
        this.paramIndexMapping = paramIndexMapping;
    }
}

5 创建DispatherServlet

package com.hua.mvc.servlet;

import com.hua.mvc.annotation.HuaAutowired;
import com.hua.mvc.annotation.HuaController;
import com.hua.mvc.annotation.HuaRequestMapping;
import com.hua.mvc.annotation.HuaService;
import com.hua.mvc.pojo.HuaHandler;
import org.apache.commons.lang3.StringUtils;

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.lang.reflect.Parameter;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class HuaDispatcherServlet extends HttpServlet {
    //存放配置信息
    private Properties properties = new Properties();
    // 缓存扫描到的类的全限定类名
    private List<String> classNames = new ArrayList<>();

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


    // handlerMapping
    //private Map<String,Method> handlerMapping = now HashMap<>();
    // 存储url和Method之间的映射关系
    private List<HuaHandler> handlerMapping = new ArrayList<>();

    @Override
    public void init(ServletConfig config) throws ServletException {
        // 1 加载配置文件 springmvc.properties
        String contextConfigLocation = config.getInitParameter("contextConfigLocation");
        doLoadConfig(contextConfigLocation);

        // 2 扫描相关的类,扫描注解
        doScan(properties.getProperty("scanPackage"));

        // 3 初始化bean对象(实现ioc容器,基于注解)
        doInstance();

        // 4 实现依赖注入
        doAutoWired();

        // 5 构造一个HandlerMapping处理器映射器,将配置好的url和Method建立映射关系
        initHandlerMapping();

        System.out.println("lagou mvc 初始化完成....");

        // 等待请求进入,处理请求
    }

    /*
        构造一个HandlerMapping处理器映射器
        最关键的环节
        目的:将url和method建立关联
     */
    private void initHandlerMapping() {
        if(ioc.isEmpty()) {return;}

        for(Map.Entry<String,Object> entry: ioc.entrySet()) {
            // 获取ioc中当前遍历的对象的class类型
            Class<?> aClass = entry.getValue().getClass();

            if(!aClass.isAnnotationPresent(HuaController.class)) {continue;}


            String baseUrl = "";
            if(aClass.isAnnotationPresent(HuaRequestMapping.class)) {
                HuaRequestMapping annotation = aClass.getAnnotation(HuaRequestMapping.class);
                baseUrl = annotation.value(); // 等同于/demo
            }

            // 获取方法
            Method[] methods = aClass.getMethods();
            for (int i = 0; i < methods.length; i++) {
                Method method = methods[i];

                //  方法没有标识HuaRequestMapping,就不处理
                if(!method.isAnnotationPresent(HuaRequestMapping.class)) {continue;}

                // 如果标识,就处理
                HuaRequestMapping annotation = method.getAnnotation(HuaRequestMapping.class);
                String methodUrl = annotation.value();  // /query
                String url = baseUrl + methodUrl;    // 计算出来的url /demo/query

                // 把method所有信息及url封装为一个Handler
                HuaHandler handler = new HuaHandler(entry.getValue(),method, Pattern.compile(url));


                // 计算方法的参数位置信息
                // query(HttpServletRequest request, HttpServletResponse response,String name)
                Parameter[] parameters = method.getParameters();
                for (int j = 0; j < parameters.length; j++) {
                    Parameter parameter = parameters[j];

                    if(parameter.getType() == HttpServletRequest.class
                            || parameter.getType() == HttpServletResponse.class) {
                        // 如果是request和response对象,那么参数名称写HttpServletRequest和HttpServletResponse
                        handler.getParamIndexMapping().put(parameter.getType().getSimpleName(),j);
                    }else{
                        handler.getParamIndexMapping().put(parameter.getName(),j);  // <name,2>
                    }

                }

                // 建立url和method之间的映射关系(map缓存起来)
                handlerMapping.add(handler);
            }
        }

    }

    //  实现依赖注入
    private void doAutoWired() {
        if(ioc.isEmpty()) {return;}

        // 有对象,再进行依赖注入处理

        // 遍历ioc中所有对象,查看对象中的字段,是否有@LagouAutowired注解,如果有需要维护依赖注入关系
        for(Map.Entry<String,Object> entry: ioc.entrySet()) {
            // 获取bean对象中的字段信息
            Field[] declaredFields = entry.getValue().getClass().getDeclaredFields();
            // 遍历判断处理
            for (int i = 0; i < declaredFields.length; i++) {
                Field declaredField = declaredFields[i];
                //  @HuaAutowired  private IDemoService demoService;
                if(!declaredField.isAnnotationPresent(HuaAutowired.class)) {
                    continue;
                }

                // 有该注解
                HuaAutowired annotation = declaredField.getAnnotation(HuaAutowired.class);
                String beanName = annotation.value();  // 需要注入的bean的id
                if("".equals(beanName.trim())) {
                    // 没有配置具体的bean id,那就需要根据当前字段类型注入(接口注入)  IDemoService
                    beanName = declaredField.getType().getName();
                }

                // 开启赋值
                declaredField.setAccessible(true);

                try {
                    declaredField.set(entry.getValue(),ioc.get(beanName));
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            }
        }
    }


    // ioc容器
    // 基于classNames缓存的类的全限定类名,以及反射技术,完成对象创建和管理
    private void doInstance()  {

        if(classNames.size() == 0) return;

        try{

            for (int i = 0; i < classNames.size(); i++) {
                String className =  classNames.get(i);
                // com.lagou.demo.controller.DemoController

                // 反射
                Class<?> aClass = Class.forName(className);
                // 区分controller,区分service'
                if(aClass.isAnnotationPresent(HuaController.class)) {
                    // controller的id此处不做过多处理,不取value了,就拿类的首字母小写作为id,保存到ioc中
                    String simpleName = aClass.getSimpleName();// DemoController
                    String lowerFirstSimpleName = lowerFirst(simpleName); // demoController
                    Object o = aClass.newInstance();
                    ioc.put(lowerFirstSimpleName,o);
                }else if(aClass.isAnnotationPresent(HuaService.class)) {
                    HuaService annotation = aClass.getAnnotation(HuaService.class);
                    //获取注解value值
                    String beanName = annotation.value();

                    // 如果指定了id,就以指定的为准
                    if(!"".equals(beanName.trim())) {
                        ioc.put(beanName,aClass.newInstance());
                    }else{
                        // 如果没有指定,就以类名首字母小写
                        beanName = lowerFirst(aClass.getSimpleName());
                        ioc.put(beanName,aClass.newInstance());
                    }


                    // service层往往是有接口的,面向接口开发,此时再以接口名为id,
                    // 放入一份对象到ioc中,便于后期根据接口类型注入
                    Class<?>[] interfaces = aClass.getInterfaces();
                    for (int j = 0; j < interfaces.length; j++) {
                        Class<?> anInterface = interfaces[j];
                        // 以接口的全限定类名作为id放入
                        ioc.put(anInterface.getName(),aClass.newInstance());
                    }
                }else{
                    continue;
                }

            }
        }catch (Exception e) {
            e.printStackTrace();
        }
    }


    // 首字母小写方法
    public String lowerFirst(String str) {
        char[] chars = str.toCharArray();
        if('A' <= chars[0] && chars[0] <= 'Z') {
            chars[0] += 32;
        }
        return String.valueOf(chars);
    }




    // 扫描类
    // scanPackage: com.lagou.demo  package---->  磁盘上的文件夹(File)  com/lagou/demo
    private void doScan(String scanPackage) {
        String scanPackagePath = Thread.currentThread().getContextClassLoader()
                .getResource("").getPath()
                + scanPackage.replaceAll("\\.", "/");
        File pack = new File(scanPackagePath);

        File[] files = pack.listFiles();

        for(File file: files) {
            if(file.isDirectory()) { // 子package
                // 递归
                doScan(scanPackage + "." + file.getName());
                // com.lagou.demo.controller
            }else if(file.getName().endsWith(".class")) {
                String className = scanPackage + "."
                        + file.getName().replaceAll(".class", "");
                classNames.add(className);
            }

        }
    }

    // 加载配置文件
    private void doLoadConfig(String contextConfigLocation) {

        InputStream resourceAsStream = this.getClass().getClassLoader()
                .getResourceAsStream(contextConfigLocation);

        try {
            properties.load(resourceAsStream);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }


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


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

        // 根据uri获取到能够处理当前请求的hanlder(从handlermapping中(list))
        HuaHandler handler = getHandler(req);

        if(handler == null) {
            resp.getWriter().write("404 not found");
            return;
        }

        // 参数绑定
        // 获取所有参数类型数组,这个数组的长度就是我们最后要传入的args数组的长度
        Class<?>[] parameterTypes = handler.getMethod().getParameterTypes();


        // 根据上述数组长度创建一个新的数组(参数数组,是要传入反射调用的)
        Object[] paraValues = new Object[parameterTypes.length];

        // 以下就是为了向参数数组中塞值,而且还得保证参数的顺序和方法中形参顺序一致

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

        // 遍历request中所有参数  (填充除了request,response之外的参数)
        for(Map.Entry<String,String[]> param: parameterMap.entrySet()) {
            // name=1&name=2   name [1,2]
            String value = StringUtils.join(param.getValue(), ",");  // 如同 1,2

            // 如果参数和方法中的参数匹配上了,填充数据
            if(!handler.getParamIndexMapping().containsKey(param.getKey())) {continue;}

            // 方法形参确实有该参数,找到它的索引位置,对应的把参数值放入paraValues
            Integer index = handler.getParamIndexMapping().get(param.getKey());//name在第 2 个位置

            paraValues[index] = value;  // 把前台传递过来的参数值填充到对应的位置去

        }


        int requestIndex = handler.getParamIndexMapping()
                .get(HttpServletRequest.class.getSimpleName()); // 0
        paraValues[requestIndex] = req;


        int responseIndex = handler.getParamIndexMapping()
                .get(HttpServletResponse.class.getSimpleName()); // 1
        paraValues[responseIndex] = resp;


        // 最终调用handler的method属性
        try {
            handler.getMethod().invoke(handler.getController(),paraValues);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }


    }

    private HuaHandler getHandler(HttpServletRequest req) {
        if(handlerMapping.isEmpty()){return null;}

        String url = req.getRequestURI();

        for(HuaHandler handler: handlerMapping) {
            Matcher matcher = handler.getPattern().matcher(url);
            if(!matcher.matches()){continue;}
            return handler;
        }

        return null;

    }

}

二、创建demo模块

1 前置准备

  • mvc-demo 是测试demo,使用maven骨架:org.apache.maven.archetypes:maven-archetype-webapp
  • 将 mvn模块 执行maven install 命令打包到本地仓库
  • 导入mvn模块

    2 配置pom.xml

    ```xml UTF-8 11 11
com.hua mvc 1.0-SNAPSHOT javax.servlet javax.servlet-api 3.1.0 compile

<!--编译插件定义编译细节-->
<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-compiler-plugin</artifactId>
  <version>3.1</version>
  <configuration>
    <source>11</source>
    <target>11</target>
    <encoding>utf-8</encoding>
    <!--告诉编译器,编译的时候记录下形参的真实名称-->
    <compilerArgs>
      <arg>-parameters</arg>
    </compilerArgs>
  </configuration>
</plugin>

<plugin>
  <groupId>org.apache.tomcat.maven</groupId>
  <artifactId>tomcat7-maven-plugin</artifactId>
  <version>2.2</version>
  <configuration>
    <port>8083</port>
    <path>/</path>
  </configuration>
</plugin>

<a name="BWw8W"></a>
## 3 配置web.xml
```xml
<servlet>
  <servlet-name>huamvc</servlet-name>
  <servlet-class>com.hua.mvc.servlet.HuaDispatcherServlet</servlet-class>
  <init-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>springmvc.properties</param-value>
  </init-param>
</servlet>

<servlet-mapping>
  <servlet-name>huamvc</servlet-name>
  <url-pattern>/*</url-pattern>
</servlet-mapping>

4 配置springmvc.properties

scanPackage=com.hua.demo

5 创建Demo类

5.1 DemoController

package com.hua.demo.controller;

import com.hua.demo.service.IDemoService;
import com.hua.mvc.annotation.HuaAutowired;
import com.hua.mvc.annotation.HuaController;
import com.hua.mvc.annotation.HuaRequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@HuaController
@HuaRequestMapping("/demo")
public class DemoController {


    @HuaAutowired
    private IDemoService demoService;


    /**
     * URL: /demo/query?name=lisi
     * @param request
     * @param response
     * @param name
     * @return
     */
    @HuaRequestMapping("/query")
    public String query(HttpServletRequest request, 
                        HttpServletResponse response,String name) {
        return demoService.get(name);
    }
}

5.2 IDemoService接口

public interface IDemoService {

    String get(String name);
}

5.3 IDemoService实现类

import com.hua.demo.service.IDemoService;
import com.hua.mvc.annotation.HuaService;

@HuaService("demoService")
public class DemoServiceImpl implements IDemoService {
    @Override
    public String get(String name) {
        System.out.println("service 实现类中的name参数:" + name) ;
        return name;
    }
}

6 测试

http://localhost:8083/demo/query?name=123
image.png