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. LeetCode 785. Is Graph Bipartite?

    原题链接在这里:https://leetcode.com/problems/is-graph-bipartite/ 题目: Given an undirected graph, return true ...

  2. delphi调用https接口

    delphi调用http接口直接使用idhttp就可以了,但是调用https接口的时候就需要和IdSSLIOHandlerSocket1控件一起使用. 截图中是两个控件的具体配置,需要注意的是IdSS ...

  3. Uncaught ReferenceError: Invalid left-hand side in assignment

    Uncaught ReferenceError: Invalid left-hand side in assignment 今天在对个人资料页面增加当浏览别的页面之后第二次访问当前页面,之前填写的内容 ...

  4. 计蒜客——Reversion Count

    Reversion Count 解析:题目数字的长度最大为 99,因此使用字符串处理,那么必然这些数存在某些规律.对于数 a (XYZW) 和数 b (WZYX),原式 = (1000X + 100Y ...

  5. 树莓派基于scratch2控制GPIO

    本文通过MetaWeblog自动发布,原文及更新链接:https://extendswind.top/posts/technical/raspberry_scratch2_gpio_control.m ...

  6. linux常用命名汇总:自用,持续更新

    1.查看磁盘空间大小 df -hl 查看磁盘剩余空间 df -h 查看每个根路径的分区大小 du -sh [目录名] 返回该目录的大小 du -sm [文件夹] 返回该文件夹总M数 du -h [目录 ...

  7. 【解决方案】Chrome崩溃问题解决

    问题描述 出现异常之前做的操作就是,因为换工位的需要,所以关闭电脑,修改网络配置. 问题分析 Firefox和其他应用网络正常 Chrome设置.帮助等选项均打不开 分析,很可能是电脑重启后,Wind ...

  8. [C++] const和mutable关键字使用方法

    const 修饰的变量为常成员变量,表示此变量不能被修改赋值,并且构造函数中只能用初始化列表的方式初始化,其他初始化方式都是错误的 const 修饰的函数为常成员函数,表示此函数中只能读取成员变量,不 ...

  9. USB:USB通信中的端点(endpoint)和四种传输模式

    USB的传输模式有4种,分别是控制传输(Control Transfer).中断传输(Interrupt Transfer).批量传输或叫块传输(Bulk Transfer).实时传输或叫同步传输(I ...

  10. 泡泡一分钟:Tightly-Coupled Aided Inertial Navigation with Point and Plane Features

    Tightly-Coupled Aided Inertial Navigation with Point and Plane Features 具有点和平面特征的紧密耦合辅助惯性导航 Yulin Ya ...