自己手写一个 springmvc 框架 -欧洲杯足彩官网

4顶
6踩

自己手写一个 springmvc 框架

2018-03-12 10:44 by 副主编 jihong10102006 评论(2) 有10663人浏览

前端框架很多,但没有一个框架称霸,后端框架现在spring已经完成大一统.所以学习spring是java程序员的必修课.
spring 框架对于 java 后端程序员来说再熟悉不过了,以前只知道它用的反射实现的,但了解之后才知道有很多巧妙的设计在里面。如果不看 spring 的源码,你将会失去一次和大师学习的机会:它的代码规范,设计思想很值得学习。我们程序员大部分人都是野路子,不懂什么叫代码规范。写了一个月的代码,最后还得其他老司机花3天时间重构,相信大部分老司机都很头疼看新手的代码。
废话不多说,我们进入今天的正题,在web应用程序设计中,mvc模式已经被广泛使用。springmvc以dispatcherservlet为核心,负责协调和组织不同组件以完成请求处理并返回响应的工作,实现了mvc模式。想要实现自己的springmvc框架,需要从以下几点入手:
一、了解 springmvc 运行流程及九大组件
二、自己实现 springmvc 的功能分析
三、手写 springmvc 框架

一、了解springmvc运行流程及九大组件

1、springmvc 的运行流程

· 用户发送请求至前端控制器dispatcherservlet
· dispatcherservlet收到请求调用handlermapping处理器映射器。
· 处理器映射器根据请求url找到具体的处理器,生成处理器对象及处理器拦截器(如果有则生成)一并返回给dispatcherservlet。
· dispatcherservlet通过handleradapter处理器适配器调用处理器
· 执行处理器(controller,也叫后端控制器)。
· controller执行完成返回modelandview
· handleradapter将controller执行结果● modelandview返回给dispatcherservlet
· dispatcherservlet将modelandview传给● viewreslover视图解析器
· viewreslover解析后返回具体view
· dispatcherservlet对view进行渲染视图(即将模型数据填充至视图中)。
· dispatcherservlet响应用户。
从上面可以看出,dispatcherservlet有接受请求,响应结果,转发等作用。有了dispatcherservlet之后,可以减少组件之间的耦合度。

2、springmvc 的九大组件
protected void initstrategies(applicationcontext context) {
//用于处理上传请求。处理方法是将普通的request包装成multiparthttpservletrequest,后者可以直接调用getfile方法获取file.
initmultipartresolver(context);
//springmvc主要有两个地方用到了locale:一是viewresolver视图解析的时候;二是用到国际化资源或者主题的时候。
initlocaleresolver(context); 
//用于解析主题。springmvc中一个主题对应一个properties文件,里面存放着跟当前主题相关的所有资源、
//如图片、css样式等。springmvc的主题也支持国际化, 
initthemeresolver(context);
//用来查找handler的。
inithandlermappings(context);
//从名字上看,它就是一个适配器。servlet需要的处理方法的结构却是固定的,都是以request和response为参数的方法。
//如何让固定的servlet处理方法调用灵活的handler来进行处理呢?这就是handleradapter要做的事情
inithandleradapters(context);
//其它组件都是用来干活的。在干活的过程中难免会出现问题,出问题后怎么办呢?
//这就需要有一个专门的角色对异常情况进行处理,在springmvc中就是handlerexceptionresolver。
inithandlerexceptionresolvers(context);
//有的handler处理完后并没有设置view也没有设置viewname,这时就需要从request获取viewname了,
//如何从request中获取viewname就是requesttoviewnametranslator要做的事情了。
initrequesttoviewnametranslator(context);
//viewresolver用来将string类型的视图名和locale解析为view类型的视图。
//view是用来渲染页面的,也就是将程序返回的参数填入模板里,生成html(也可能是其它类型)文件。
initviewresolvers(context);
//用来管理flashmap的,flashmap主要用在redirect重定向中传递参数。
initflashmapmanager(context); 
}

二、自己实现 springmvc 功能分析

本篇文章只实现 springmvc 的配置加载、实例化扫描的包、handlermapping 的 url 映射到对应的controller 的 method 上、异常的拦截和动态调用后返回结果输出给浏览器的功能。其余 springmvc 功能读者可以尝试自己实现。

1、读取配置

从图中可以看出,springmvc本质上是一个servlet,这个 servlet 继承自 httpservlet。
frameworkservlet负责初始化springmvc的容器,并将spring容器设置为父容器。因为本文只是实现springmvc,对于spring容器不做过多讲解(有兴趣同学可以看看博主另一篇文章:向spring大佬低头--大量源码流出解析)。
为了读取web.xml中的配置,我们用到servletconfig这个类,它代表当前servlet在web.xml中的配置信息。通过web.xml中加载我们自己写的mydispatcherservlet和读取配置文件。

2、初始化阶段
在上文中,我们知道了dispatcherservlet的initstrategies方法会初始化9大组件,但是本文将实现一些springmvc的最基本的组件而不是全部,按顺序包括:

• 加载配置文件
• 扫描用户配置包下面所有的类
• 拿到扫描到的类,通过反射机制,实例化。并且放到ioc容器中(map的键值对  beanname-bean) beanname默认是首字母小写
• 初始化handlermapping,这里其实就是把url和method对应起来放在一个k-v的map中,在运行阶段取出

3、运行阶段
每一次请求将会调用doget或dopost方法,所以统一运行阶段都放在dodispatch方法里处理,它会根据url请求去handlermapping中匹配到对应的method,然后利用反射机制调用controller中的url对应的方法,并得到结果返回。按顺序包括以下功能:
• 异常的拦截
• 获取请求传入的参数并处理参数
• 通过初始化好的handlermapping中拿出url对应的方法名,反射调用

三、手写 springmvc 框架

工程文件及目录:

首先,新建一个maven项目,在pom.xml中导入以下依赖。为了方便,博主直接导入了springboot的web包,里面有我们需要的所有web开发的东西:

  4.0.0
  com.liugh
  liughmvc
  0.0.1-snapshot
  war
  
 

    
        org.springframework.boot
        spring-boot-dependencies
        1.4.3.release
        pom
        import
    



    utf-8
    1.8
    1.8
    1.8


  
            org.springframework.boot
            spring-boot-starter-web
        
    

接着,我们在web-inf下创建一个web.xml,如下配置:



    myspringmvc
    com.liugh.servlet.mydispatcherservlet
    
    contextconfiglocation
    application.properties
    
    1


    myspringmvc
    /*


application.properties文件中只是配置要扫描的包到springmvc容器中。
scanpackage=com.liugh.core

创建自己的controller注解,它只能标注在类上面:
package com.liugh.annotation;
import java.lang.annotation.documented;
import java.lang.annotation.elementtype;
import java.lang.annotation.retention;
import java.lang.annotation.retentionpolicy;
import java.lang.annotation.target;
@target(elementtype.type)
@retention(retentionpolicy.runtime)
@documented
public @interface mycontroller {
/**
     * 表示给controller注册别名
     * @return
     */
    string value() default "";
}

requestmapping注解,可以在类和方法上:
package com.liugh.annotation;
import java.lang.annotation.documented;
import java.lang.annotation.elementtype;
import java.lang.annotation.retention;
import java.lang.annotation.retentionpolicy;
import java.lang.annotation.target;
@target({elementtype.type,elementtype.method})
@retention(retentionpolicy.runtime)
@documented
public @interface myrequestmapping {
/**
     * 表示访问该方法的url
     * @return
     */
    string value() default "";
}

requestparam注解,只能注解在参数上
package com.liugh.annotation;
import java.lang.annotation.documented;
import java.lang.annotation.elementtype;
import java.lang.annotation.retention;
import java.lang.annotation.retentionpolicy;
import java.lang.annotation.target;
@target(elementtype.parameter)
@retention(retentionpolicy.runtime)
@documented
public @interface myrequestparam {
/**
     * 表示参数的别名,必填
     * @return
     */
    string value();
}

然后创建mydispatcherservlet这个类,去继承httpservlet,重写init方法、doget、dopost方法,以及加上我们第二步分析时要实现的功能:
package com.liugh.servlet;
import java.io.file;
import java.io.ioexception;
import java.io.inputstream;
import java.lang.reflect.method;
import java.net.url;
import java.util.arraylist;
import java.util.arrays;
import java.util.hashmap;
import java.util.list;
import java.util.map;
import java.util.map.entry;
import java.util.properties;
import javax.servlet.servletconfig;
import javax.servlet.servletexception;
import javax.servlet.http.httpservlet;
import javax.servlet.http.httpservletrequest;
import javax.servlet.http.httpservletresponse;
import com.liugh.annotation.mycontroller;
import com.liugh.annotation.myrequestmapping;
public class mydispatcherservlet extends httpservlet{
private properties properties = new properties();
private list classnames = new arraylist<>();
private map ioc = new hashmap<>();
private map handlermapping = new  hashmap<>();
private map controllermap  =new hashmap<>();
@override
public void init(servletconfig config) throws servletexception {
    //1.加载配置文件
    doloadconfig(config.getinitparameter("contextconfiglocation"));
    //2.初始化所有相关联的类,扫描用户设定的包下面所有的类
    doscanner(properties.getproperty("scanpackage"));
    //3.拿到扫描到的类,通过反射机制,实例化,并且放到ioc容器中(k-v  beanname-bean) beanname默认是首字母小写
    doinstance();
    //4.初始化handlermapping(将url和method对应上)
    inithandlermapping();
}
@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 {
        //处理请求
        dodispatch(req,resp);
    } catch (exception e) {
        resp.getwriter().write("500!! server exception");
}
}
private void dodispatch(httpservletrequest req, httpservletresponse resp) throws exception {
    if(handlermapping.isempty()){
        return;
    }
    string url =req.getrequesturi();
    string contextpath = req.getcontextpath();
    url=url.replace(contextpath, "").replaceall("/ ", "/");
    if(!this.handlermapping.containskey(url)){
    resp.getwriter().write("404 not found!");
    return;
}
method method =this.handlermapping.get(url);
//获取方法的参数列表
class[] parametertypes = method.getparametertypes();
//获取请求的参数
map parametermap = req.getparametermap();
//保存参数值
object [] paramvalues= new object[parametertypes.length];
//方法的参数列表
        for (int i = 0; i param : parametermap.entryset()) {
         string value =arrays.tostring(param.getvalue()).replaceall("\[|\]", "").replaceall(",\s", ",");
         paramvalues[i]=value;
         }
            }
        }  
//利用反射机制来调用
    try {
       method.invoke(this.controllermap.get(url), paramvalues);//obj是method所对应的实例 在ioc容器中
    } catch (exception e) {
        e.printstacktrace();
    }
}
private void  doloadconfig(string location){
//把web.xml中的contextconfiglocation对应value值的文件加载到留里面
inputstream resourceasstream = this.getclass().getclassloader().getresourceasstream(location);
try {
//用properties文件加载文件里的内容
properties.load(resourceasstream);
} catch (ioexception e) {
e.printstacktrace();
}finally {
//关流
if(null!=resourceasstream){
try {
resourceasstream.close();
} catch (ioexception e) {
e.printstacktrace();
}
}
}
}
private void doscanner(string packagename) {
//把所有的.替换成/
url url  =this.getclass().getclassloader().getresource("/" packagename.replaceall("\.", "/"));
file dir = new file(url.getfile());
for (file file : dir.listfiles()) {
if(file.isdirectory()){
//递归读取包
doscanner(packagename "." file.getname());
}else{
string classname =packagename  "."  file.getname().replace(".class", "");
classnames.add(classname);
}
}
}
private void doinstance() {
if (classnames.isempty()) {
return;
}
for (string classname : classnames) {
try {
//把类搞出来,反射来实例化(只有加@mycontroller需要实例化)
class clazz =class.forname(classname);
   if(clazz.isannotationpresent(mycontroller.class)){
ioc.put(tolowerfirstword(clazz.getsimplename()),clazz.newinstance());
}else{
continue;
}
} catch (exception e) {
e.printstacktrace();
continue;
}
}
}
private void inithandlermapping(){
if(ioc.isempty()){
return;
}
try {
for (entry entry: ioc.entryset()) {
class clazz = entry.getvalue().getclass();
if(!clazz.isannotationpresent(mycontroller.class)){
continue;
}
//拼url时,是controller头的url拼上方法上的url
string baseurl ="";
if(clazz.isannotationpresent(myrequestmapping.class)){
myrequestmapping annotation = clazz.getannotation(myrequestmapping.class);
baseurl=annotation.value();
}
method[] methods = clazz.getmethods();
for (method method : methods) {
if(!method.isannotationpresent(myrequestmapping.class)){
continue;
}
myrequestmapping annotation = method.getannotation(myrequestmapping.class);
string url = annotation.value();
url =(baseurl "/" url).replaceall("/ ", "/");
handlermapping.put(url,method);
controllermap.put(url,clazz.newinstance());
system.out.println(url "," method);
}
}
} catch (exception e) {
e.printstacktrace();
}
}
/**
* 把字符串的首字母小写
* @param name
* @return
*/
private string tolowerfirstword(string name){
char[] chararray = name.tochararray();
chararray[0]  = 32;
return string.valueof(chararray);
}
}

这里我们就开发完了自己的springmvc,现在我们测试一下:
package com.liugh.core.controller;
import java.io.ioexception;
import javax.servlet.http.httpservletrequest;
import javax.servlet.http.httpservletresponse;
import com.liugh.annotation.mycontroller;
import com.liugh.annotation.myrequestmapping;
import com.liugh.annotation.myrequestparam;
@mycontroller
@myrequestmapping("/test")
public class testcontroller {
@myrequestmapping("/dotest")
    public void test1(httpservletrequest request, httpservletresponse response,
    @myrequestparam("param") string param){
 system.out.println(param);
    try {
            response.getwriter().write( "dotest method success! param:" param);
        } catch (ioexception e) {
            e.printstacktrace();
        }
    }
 
 
@myrequestmapping("/dotest2")
    public void test2(httpservletrequest request, httpservletresponse response){
        try {
            response.getwriter().println("dotest2 method success!");
        } catch (ioexception e) {
            e.printstacktrace();
        }
    }
}

访问http://localhost:8080/liughmvc/test/dotest?param=liugh如下:

访问一个不存在的试试:

到这里我们就大功告成了!
  • 大小: 229.7 kb
  • 大小: 39.3 kb
  • 大小: 41.1 kb
  • 大小: 22.8 kb
  • 大小: 10.7 kb
  • 大小: 11.1 kb
来自:
4
6
评论 共 2 条 请登录后发表评论
2 楼 2018-03-28 16:56
不是原创。
1 楼 2018-03-12 16:42
原来是标题党

发表评论

您还没有登录,请您登录后再发表评论

相关推荐

  • null 博文链接:https://bewithme.iteye.com/blog/1938178

  • 文章目录功能背景角色介绍服务器端证书获取gateway 信任客户端证书openssl对d证书文件处理证书文件生成jks文件server端证书处理client端证书处理证书文件生成p12文件server端证书处理client端证书处理httpclient双向...

  • tomcat6配置双向认证1、生成服务器端证书java代码keytool -genkey -keyalg rsa -dname "cn=localhost,ou=sango,o=none,l=china,st=beijing,c=...

  • httpclient4实现ssl双向认证的客户端服务器端使用netty实现的http服务器端,只能处理get和post请求。下面是具体的代码:httpdemoserver.javapackagehttps;importio.netty.bootstrap.serverbootstrap;importio.netty....

  • maven dependenceorg.apache.httpcomponentshttpclient4.5.22. 测试类packagecom.iraid.test;importjava.io.bufferedreader;importjava.io.file;importjava.io.fileinputstream;importjava.io.ioexception;import...

  • 说明:本文主要是在平时接口对接开发中遇到的为保证传输安全的情况特要求使用https进行交互的情况下,使用httpclient4.5版本对https的双向验证的 功能的实现  首先,老生常谈,文章将按照哲学三部曲来解答什么是...

  • 由于不能直接将pkcs12格式的证书库导入,我们必须先把客户端证书导出为一个单独的cer文件,使用如下命令,先把客户端证书导出为一个单独的cer文件: java代码 keytool -export -alias custom -file custom....

  • java使用httpclient加载证书实现ssl双向认证(客户端),向服务端发送json数据和上传文件信息,修复sslpeerunverifiedexception异常问题。

  • httpclient工具类代码 /** * description: httpclient工具类 * * @author chin */ @slf4j public class httpclienttools { // 编码格式。发送编码格式统一用utf-8 private static final string encoding =...

  • 原文地址: https://www.cnblogs.com/ywbmaster/p/14684968.html (推荐) https://blog.csdn.net/irokay/article/details/78801307

  • 单向认证 我们在通常访问一个网站,例如...这种时候,服务端并不校验客户端的合法性,来者不拒,绝大部分的网站都是这种类型。 例如查看百度: [root@izbp1g905y8l5pclnbxvfxz ~]# curl https://www.b

  • https://blog.csdn.net/xintonghanchuang/article/details/103298943

  • httpclient 实现https双向认证

  • 我的项目是服务器与服务器之间调接口,调用是需要ssl证书双向认证的。 yml中配置: ## 证书双向认证配置(本系统作为客户端) client: ssl: abs: # jks与pkcs12(即pfx)都可以。type不区分大小写 # path: ...

  • 为了双向认证,我们首先得准备两个crt证书,一个是client.crt,一个是server.crt,有时为了验证是否同一个根证书的验证,这两个证书可以共有一个根证书root.crt。 首先要生成这些证书,这里采用了自签证书方式: ...

  • 双向认证4. 连接池 1. 加载证书单向认证 import org.apache.http.httpentity; import org.apache.http.client.methods.closeablehttpresponse; import org.apache.http.client.methods.httpget; import org.apache...

  • httpclient httpclient = new defaulthttpclient(); httpclient.getparams().setparameter(coreprotocolpnames.http_content_charset, charset.forname("utf-8")); try { keystore keystore = ...

  • python库是一组预先编写的代码模块,旨在帮助开发者实现特定的编程任务,无需从零开始编写代码。这些库可以包括各种功能,如数学运算、文件操作、数据分析和网络编程等。python社区提供了大量的第三方库,如numpy、pandas和requests,极大地丰富了python的应用领域,从数据科学到web开发。python库的丰富性是python成为最受欢迎的编程语言之一的关键原因之一。这些库不仅为初学者提供了快速入门的途径,而且为经验丰富的开发者提供了强大的工具,以高效率、高质量地完成复杂任务。例如,matplotlib和seaborn库在数据可视化领域内非常受欢迎,它们提供了广泛的工具和技术,可以创建高度定制化的图表和图形,帮助数据科学家和分析师在数据探索和结果展示中更有效地传达信息。

  • stm32单片机fpga毕设电路原理论文报告基于ide硬盘的数字图像存储技术研究本资源系百度网盘分享地址

  • 适合rust入门。深入浅出,事无巨细,远胜市面上所有入门书。而且是免费的

global site tag (gtag.js) - google analytics
网站地图