前端框架很多,但没有一个框架称霸,后端框架现在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如下:
访问一个不存在的试试:
到这里我们就大功告成了!
2 楼 2018-03-28 16:56
1 楼 2018-03-12 16:42