手写模拟SpringMvc源码

简介: 手写模拟SpringMvc源码

MVC框架

MVC是一种设计模式(设计模式就是日常开发中编写代码的一种好的方法和经验的总结)。模型(model)-视图(view)-控制器(controller),三层架构的设计模式。用于实现前端页面的展现与后端业务数据处理的分离。


Spring MVC的主要组件

前端控制器 DispatcherServlet

Spring的MVC框架是围绕DispatcherServlet来设计的,它用来处理所有的HTTP请求和响应。此模块不需要程序员开发。

作用:接收请求、响应结果,相当于转发器,有了DispatcherServlet 就减少了其它组件之间的耦合度。

处理器映射器HandlerMapping

此功能不需要程序员开发。

作用:根据请求的URL来查找Handler

处理器适配器HandlerAdapter

作用:进行视图的解析,根据视图逻辑名解析成真正的视图(view)

视图View

需要程序员开发jsp。View是一个接口, 它的实现类支持不同的视图类型(jsp,freemarker,pdf等等)

fe910a073f8282fabb5b0cf274539cb5.png

用户发送请求至前端控制器DispatcherServlet;

DispatcherServlet收到请求后,调用HandlerMapping处理器映射器,请求获取Handle;

处理器映射器根据请求url找到具体的处理器,生成处理器对象及处理器拦截器(如果有则生成)一并返回给DispatcherServlet;

DispatcherServlet 调用 HandlerAdapter处理器适配器;

HandlerAdapter 经过适配调用 具体处理器(Handler,也叫后端控制器);

Handler执行完成返回ModelAndView;

HandlerAdapter将Handler执行结果ModelAndView返回给DispatcherServlet;

DispatcherServlet将ModelAndView传给ViewResolver视图解析器进行解析;

ViewResolver解析后返回具体View;

DispatcherServlet对View进行渲染视图(即将模型数据填充至视图中)

DispatcherServlet响应用户。

手写模拟SpringMvc源码

spring mvc调用到Controller执行的原理

通过加载配置文件web.xml进而加载spring-mvc.xml。

根据配置文件给定的目录来扫描整个项目。

扫描所有加了@Controller注解的类。

当扫描到加了@Controller注解的类之后遍历里面所有的方法。

拿到方法对象之后 解析方法上面是否加了@RequestMapping注解。

定义一个Map集合把@RequstMapping的value 与方法对象绑定起来,即Map<String,Method>。

定义一个Map把声名该方法的类的对象绑定起来,即Map<String,Object>。

拦截到请求之后拿到请求的URI。

处理请求,例如执行指定方法,返回字符串或跳转到相应视图。

目录

2def37b06d8c4020252a13c04b352ce3.png

导入依赖

 <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>javax.servlet-api</artifactId>
      <version>3.1.0</version>
    </dependency>
    <!--       解析xml文件-->
    <dependency>
      <groupId>dom4j</groupId>
      <artifactId>dom4j</artifactId>
      <version>1.6.1</version>
    </dependency>
    <dependency>
      <groupId>org.apache.commons</groupId>
      <artifactId>commons-lang3</artifactId>
      <version>3.9</version>
    </dependency>

代码

DispatcherServlet类

public class DispatcherServlet extends HttpServlet {
    private ApplicationContext applicationContext;
    private List<MyHandle> myHandleList=new ArrayList<>();
    @Override
    public void init() throws ServletException {
        String contextConfigLocation = this.getServletConfig().getInitParameter("contextConfigLocation");
        System.out.println(contextConfigLocation);
        applicationContext = new ApplicationContext(contextConfigLocation);
        applicationContext.refresh();
        initHandleMappinng(applicationContext);
    }
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        excuteDispatch(req,resp);
    }
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    }
    public void initHandleMappinng(ApplicationContext applicationContext){
        if(applicationContext.beanDefinitionConcurrentHashMap.size()==0){
            throw new RuntimeException("Spring容器为空");
        }
        for (Map.Entry<String, BeanDefinition> stringBeanDefinitionEntry : applicationContext.beanDefinitionConcurrentHashMap.entrySet()) {
            Class clazz = stringBeanDefinitionEntry.getValue().getClazz();
            for (Method declaredMethod : clazz.getDeclaredMethods()) {
                boolean annotationPresent = declaredMethod.isAnnotationPresent(RequestMapping.class);
                if(annotationPresent==true){
                    String value = declaredMethod.getAnnotation(RequestMapping.class).value();
                    MyHandle myHandle=new MyHandle(value,declaredMethod,clazz);
                    myHandleList.add(myHandle);
                }
            }
        }
    }
    public void excuteDispatch(HttpServletRequest request,HttpServletResponse response){
        MyHandle handle = getHandle(request);
        if(handle==null){
            try {
                response.getWriter().print("404");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        else {
            Method method = handle.getMethod();
            Class<?>[] parameterTypes = method.getParameterTypes();
            Object[] params=new Object[parameterTypes.length];
            Map<String, String[]> parameterMap = request.getParameterMap();
            for (Map.Entry<String, String[]> stringEntry : parameterMap.entrySet()) {
                String key = stringEntry.getKey();
                String value = stringEntry.getValue()[0];
                int i = GetRequestParams(method, key);
                if(i>=0)
                    params[i]=value;
                else {
                    //反射获取的是arg0,官方这里用的不是反射机制
                }
            }
            try {
                Object invoke = method.invoke(handle.getClazz().newInstance(), params);
                PrintWriter writer = response.getWriter();
                writer.print(invoke);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            } catch (InstantiationException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    public MyHandle getHandle(HttpServletRequest request){
        String requestURI = request.getRequestURI();
        for (MyHandle myHandle : myHandleList) {
            if(myHandle.getUrl().equals(requestURI))
                return myHandle;
        }
        return null;
    }
    public int GetRequestParams(Method method,String name){
        Parameter[] parameters = method.getParameters();
        for (int i=0;i<parameters.length;i++) {
            boolean annotationPresent = parameters[i].isAnnotationPresent(RequestParam.class);
            if(annotationPresent){
                String value = parameters[i].getAnnotation(RequestParam.class).value();
                if(value.equals(name))
                    return i;
            }
        }
        return -1;
    }
}

XmlPaser类

public class XmlPaser {
    public static String getbasePackage(String xml){
        SAXReader saxReader=new SAXReader();
        InputStream inputStream = XmlPaser.class.getClassLoader().getResourceAsStream(xml);
        try {
            Document document = saxReader.read(inputStream);
            Element rootElement = document.getRootElement();
            Element componentScan = rootElement.element("component-scan");
            Attribute attribute = componentScan.attribute("base-package");
            String text = attribute.getText();
            return text;
        } catch (DocumentException e) {
            e.printStackTrace();
        }
        return "";
    }
}

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">
    <display-name>Application</display-name>
    <servlet>
        <servlet-name>DispatcherServlet</servlet-name>
        <servlet-class>com.example.demo.springmvc.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:springmvc.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>DispatcherServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

代码下载地址:https://download.csdn.net/download/qq_43649937/87558006

相关文章
|
设计模式 消息中间件 存储
【设计模式系列笔记】观察者模式
观察者模式是一种设计模式,它允许一个对象(主题)维护一组依赖于它的对象(观察者)并在状态发生变化时通知它们。这种模式属于行为型模式。在观察者模式中,主题是被观察的对象,而观察者是依赖于主题的对象,当主题的状态发生变化时,所有注册的观察者都会得到通知并自动更新。
349 0
|
1天前
|
监控 网络协议 算法
2026年起SSL证书有效期逐年缩短:企业如何避免“续期风暴”!
SSL证书有效期将从2026年起分三阶段逐步缩短至47天,人工续期难以为继。阿里云“WoSign SSL证书+自动化服务”,支持域名监控、自动托管、云产品/服务器一键部署及智能提醒,助力企业高效应对高频续期挑战。
8059 2
|
10天前
|
域名解析 人工智能 Linux
|
20天前
|
Linux Go 开发工具
手机上跑 DeepSeek 智能体:开源安卓客户端 DSH Go 安装与使用指南
DSH Go 是一款开源安卓客户端,将 DeepSeek Harness 智能体引擎装进手机:免 Root、免 Termux,一键安装即用;支持本机运行(内置 Ubuntu+Node.js)或远程接管电脑会话;具备插话打断、过程折叠、屏幕操控、端到端加密分享等特色功能,真正实现智能体“口袋化”。
18324 63
|
18天前
|
人工智能
Google 提示「所在地区不可用」?进不去 Antigravity 或 Gemini,先查账号这个设置
进不去 Antigravity 或 Gemini,提示所在地区不可用?三步查清 Google 账号关联地区、官方更正表单理由选法与改区注意事项。
Google 提示「所在地区不可用」?进不去 Antigravity 或 Gemini,先查账号这个设置
|
4天前
|
人工智能 安全 网络安全
|
2天前
|
人工智能 弹性计算 安全
阿里云 码上2027 AI coding 嘉年华
阿里云“码上2027”AI Coding嘉年华于2026年12月2—3日在上海西岸艺术中心举行,聚焦AI Coding从单点提效迈向企业级研发体系升级。发布Qwen4-max模型、QBot智能协作、AgentCore平台等重磅成果,并推出ECS云服务器特惠(200Mbps流量月付40元起)。
778 0
|
8天前
|
云安全 数据采集 人工智能
|
9天前
|
Linux API iOS开发
【DSH】DeepSeek Harness新手保姆教程(下载+安装+配置+使用,2026最新实测)
DeepSeek Harness(dsh)是 DeepSeek AI 开源的智能体框架,秉持“一切皆插件”理念,支持读文件、改代码、执行命令、处理表格等自动化任务;免费开源,兼容 Win/macOS,需配 API 密钥使用。(239字)
1479 5
|
15天前
|
人工智能 开发工具 开发者
2026最新codex下载安装保姆级使用教程(下载+安装+登录+使用,含所有历史版本)
Codex 是 OpenAI 推出的开源 AI 编程智能体,跑在本地终端和桌面客户端里,能读懂整个代码仓库,一句自然语言就能让它改代码、执行命令、跑测试、修 Bug,支持 Windows、macOS、Linux 三大平台。本文把 Codex下载、安装、登录、汉化、基础使用一次讲全,并整理了国内用户最常踩的坑,跟着做 10 分钟就能跑起来。
2608 8