SpringMVC的主要作用是:从http请求中得到一个url字符串和对应的请求参数,根据该字符串找到Controller中的一个方法,利用反射执行该方法,将结果返回给前端

1,初始化

  将url请求路径和controller中的方法一一对应

  将url请求路径和object一一对应, 反射调用方法时要传入object和方法入参

2,执行请求

  获取http请求路径和参数,找到对应的方法,执行该方法后返回给前端

================================================================================================================================

包扫描是怎么实现的

递归指定路径的目录(class文件在项目中也是放在某个目录当中的),找出所有的全局限定类名,利用反射生成该类的实例,判断该类是否有特定的注解修饰,比如说@MyController,有注解修饰的就将实例放入Map中

一,web.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
<servlet>
<servlet-name>MVC</servlet-name>
<servlet-class>com.irish.servlet.MyDispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>application.properties</param-value>
</init-param>
<load-on-startup></load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>MVC</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>

二,DispathcherServlet

public class MyDispatcherServlet extends HttpServlet{

    private static final long serialVersionUID = 1L;

    private Properties properties = new Properties();

    private List<String> classNames = new ArrayList<>();

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

    private Map<String, Method> handlerMapping = new  HashMap<>();

    private Map<String, Object> controllerMap  =new HashMap<>();

    @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();
System.out.println("requestUri:"+url);
System.out.println("contextPath:"+contextPath);
url=url.replace(contextPath, "").replaceAll("/+", "/");
System.out.println("url:"+url);
if(!this.handlerMapping.containsKey(url)){
resp.getWriter().write("404 NOT FOUND!");
return;
} Method method =this.handlerMapping.get(url); //获取方法的参数列表
Class<?>[] parameterTypes = method.getParameterTypes();
//获取方法的参数列表的注解值,顺序和长度与parameterTypes相同,
String [] paramNames = getMethodParameterNamesByAnnotation(method); //获取请求的参数
Map<String, String[]> parameterMap = req.getParameterMap(); //保存参数值
Object [] paramValues= new Object[parameterTypes.length]; //方法的参数列表
for (int i = ; i<parameterTypes.length; i++){
//根据参数名称,做某些处理
String requestParam = parameterTypes[i].getSimpleName();
if (requestParam.equals("HttpServletRequest")){
//参数类型已明确,这边强转类型
paramValues[i]=req;
continue;
}
if (requestParam.equals("HttpServletResponse")){
paramValues[i]=resp;
continue;
}
if(requestParam.equals("String")){
String theParamName = paramNames[i];
for (Entry<String, String[]> param : parameterMap.entrySet()) {
if(param.getKey().equals(theParamName)) {
String value =Arrays.toString(param.getValue()).replaceAll("\\[|\\]", "").replaceAll(",\\s", ",");
//[abc, ed] 替换为 abc,ed
paramValues[i]=value;
System.out.println(theParamName + " : "+value);
}
}
}
}
//利用反射机制来调用
try {
method.invoke(this.controllerMap.get(url), paramValues);
} catch (Exception e) {
e.printStackTrace();
} } /**
*
* 按照方法参数的顺序获取注解的值,没有注解的参数对应的位置为空
*/
public static String[] getMethodParameterNamesByAnnotation(Method method) {
Annotation[][] parameterAnnotations = method.getParameterAnnotations();
if (parameterAnnotations == null || parameterAnnotations.length == ) {
return null;
}
String[] parameterNames = new String[parameterAnnotations.length];
int i = ;
for (Annotation[] parameterAnnotation : parameterAnnotations) {
if(parameterAnnotation.length == ) {
parameterNames[i++] = null;
}
for (Annotation annotation : parameterAnnotation) {
if (annotation instanceof MyRequestParam) {
MyRequestParam param = (MyRequestParam) annotation;
parameterNames[i++] = param.value();
}
}
}
return parameterNames;
} @Override
public void init(ServletConfig config) throws ServletException { //1.加载配置文件,将classpath路径下的application.properties文件加载到内存
//config.getInitParameter("contextConfigLocation") 获取的是Servlet的配置参数
doLoadConfig(config.getInitParameter("contextConfigLocation")); //2.将指定路径下的全局限定类名添加到classNames集合当中
doScanner(properties.getProperty("scanPackage")); //3.通过反射实例化标注了MyController注解的类,并且放到ioc容器中
doInstance(); //4.初始化url和Method的对应关系,url和Object的对应关系
initHandlerMapping(); } 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<String, Object> entry: ioc.entrySet()) {
Class<? extends Object> 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();
} } /**
* 字符串首字母小写
*/
private String toLowerFirstWord(String name){
char[] charArray = name.toCharArray();
charArray[] += ;
return String.valueOf(charArray);
} }

三,自定义注解

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface MyController {
/**
* 表示给controller注册别名
* @return
*/
String value() default ""; }
@Target({ElementType.TYPE,ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface MyRequestMapping {
/**
* 表示访问该方法的url
* @return
*/
String value() default ""; }
@Target(ElementType.PARAMETER)
@Retention(RetentionPolicy.RUNTIME)
public @interface MyRequestParam {
/**
* 表示参数的别名,必填
* @return
*/
String value(); }

四,controller

@MyController
@MyRequestMapping("/test")
public class TestController { @MyRequestMapping("/doTest")
public void test1(HttpServletRequest request, HttpServletResponse response,
@MyRequestParam("param1") String param1 , @MyRequestParam("param2") String param2 ){
System.out.println("param1 = "+ param1);
System.out.println("param2 = "+ param2);
try {
response.getWriter().write( "doTest method success! param1:"+param1 + " , param2:"+ param2);
} 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();
}
}
}

五,application.properties

scanPackage=com.irish.controller

六,pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.</modelVersion>
<groupId>com.irish</groupId>
<artifactId>mySpringMVC</artifactId>
<version>0.0.-SNAPSHOT</version>
<packaging>war</packaging> <properties>
<project.build.sourceEncoding>UTF-</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<java.version>1.8</java.version>
</properties> <dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.</version>
<scope>provided</scope>
</dependency>
</dependencies> </project>

七,测试

项目结构

github地址    https://github.com/jake1263/MySpringMVC

自己实现简单版SpringMVC的更多相关文章

  1. 手动实现一个简易版SpringMvc

    版权声明:本篇博客大部分代码引用于公众号:java团长,我只是在作者基础上稍微修改一些内容,内容仅供学习与参考 前言:目前mvc框架经过大浪淘沙,由最初的struts1到struts2,到目前的主流框 ...

  2. 简单深入SpringMvc

    简单深入SpringMvc 一.如何让一个普通类成为Controller? 方案一:实现接口Controller解析:handleRequest(request,response) 方案二:继承Abs ...

  3. JavaMail简单版实验测试

    前言: 最近由于实现web商城的自动发送邮件功能的需求,故涉猎的邮箱协议的内部原理.现将简单版的Java Mail实例做个代码展示,并附上其中可能出现的bug贴出,方便感兴趣的读者进行测试! 1.载入 ...

  4. 小米抢购(简单版v0.1)-登录并验证抢购权限,以及获取真实抢购地址

    小米(简单版)-登录并验证抢购权限,以及获取真实抢购地址! 并不是复制到浏览器就行了的   还得传递所需要的参数 这里只是前部分  后面的自己发挥了 { "stime": 1389 ...

  5. Java实现简单版SVM

    Java实现简单版SVM 近期的图像分类工作要用到latent svm,为了更加深入了解svm,自己动手实现一个简单版的.         之所以说是简单版,由于没实用到拉格朗日,对偶,核函数等等.而 ...

  6. 创建一个可用的简单的SpringMVC项目,图文并茂

    转载麻烦注明下来源:http://www.cnblogs.com/silentdoer/articles/7134332.html,谢谢. 最近在自学SpringMVC,百度了很多资料都是比较老的,而 ...

  7. 简单实现springmvc框架(servlet+自定义注解)

    个人水平比较菜,没有这么高的实力简单实现springmvc框架,我是看了一个老哥的博客,这老哥才是大神! 原文链接:https://www.cnblogs.com/xdp-gacl/p/4101727 ...

  8. MySQL数据库执行计划(简单版)

    +++++++++++++++++++++++++++++++++++++++++++标题:MySQL数据库执行计划简单版时间:2019年2月25日内容:MySQL数据库执行计划简单版重点:MySQL ...

  9. 红警大战JAVA简单版

    代码结构: 相关源码: 武器类: 属性:武器,攻击力,子弹数量. 方法:给属性赋值(set属性()方法) 获取属性值(get属性()方法) package 红警大战简单版; public class ...

随机推荐

  1. session内置对象

    SimpleDateFormat sdf = new SimpleDateFormat(yyyy年MM月dd日)  //处理日期格式 session.getCreationDate() 是获取sess ...

  2. 【转】分布式事务,EventBus 解决方案:CAP【中文文档】

    [转]分布式事务,EventBus 解决方案:CAP[中文文档] 最新文档地址:https://github.com/dotnetcore/CAP/wiki 前言 很多同学想对CAP的机制以及用法等想 ...

  3. 学习:c++指向指针的指针(多级间接寻址)

    指向指针的指针是一种多级间接寻址的形式,或者说是一个指针链.通常,一个指针包含一个变量的地址.当我们定义一个指向指针的指针时,第一个指针包含了第二个指针的地址,第二个指针指向包含实际值的位置. 当一个 ...

  4. C# 监控网速

    主要有两个类,其一是NetworkAdapter,该类的作用是获取本机网络适配器列表,并且可以通过该类的属性获取当前网速数据:其二是NetworkMonitor,该类是通过.NET的Performan ...

  5. 用Desmos玩极坐标[适配手机端]

    前言 数学给人的印象一直就是算算算,今天我们不算,我们只玩. 必备条件 ①."呆萌"软件--Desmos 网址:https://www.desmos.com/calculator, ...

  6. 拉格朗日插值法(c++)【转载】

    摘自<c++和面向对象数值计算>,代码简洁明快,采用模板函数,通用性增强,对其中代码稍加改动 #include<iostream> #include <vector> ...

  7. rust控制流

    fn main() { let number = 6; if number % 4 == 0 { println!("number is divisible by 4"); } e ...

  8. 使用IDEA查看变量调用链

    在开发中,我们有时需要查看某个变量是怎么来的,从哪个类的某个方法调用后进入另一个类的某个方法. 如果只有一两层的调用,那么还能直接通过方法跳转来观察. 但是,如果有七八层的调用链呢,在各个方法之间跳来 ...

  9. golang 文件传输小demo(转载)

    转载地址:https://www.cnblogs.com/qq702368956/p/10195497.html 获取文件信息需要用到os. Stat接口,发送文件前开启接收者(服务端),启动客户端先 ...

  10. slot 插槽子组件向父组件传值

    slot 插槽要实现子组件向父组件传值,则需要运用 作用域插槽 1.父组件中用 标签加上 slot-scoped 的属性,属性值随性.(旧版本是scope,vue新版本必须用slot-scope) 2 ...