续接前文

手写SpringMVC框架(二)结构开发设计

本节我们来开始具体方法的代码实现。

doLoadConfig()方法的开发

思路:我们需要将contextConfigLocation路径读取过来的配置文件springmvc.properties加载到内存中来。

实现:使用properties及类加载器。

代码如下:

import java.io.InputStream;
import java.util.Properties; private Properties properties=new Properties(); //加载配置文件
private void doLoadConfig(String contextConfigLocation) {
InputStream resourceAsStream = this.getClass().getClassLoader().getResourceAsStream(contextConfigLocation);
try {
properties.load(resourceAsStream);
} catch (IOException e) {
e.printStackTrace();
}
}

doScan()扫描包方法的实现

代码如下:

List<String> classNames=new ArrayList<>();
//扫描类 磁盘上的文件夹及文件
private void doScan(String scanPackage) { String scanPackagePath = Thread.currentThread().getContextClassLoader().getResource("").getPath(); File pack=new File(scanPackagePath);
File[] files = pack.listFiles();
for (File file : files) {
if (file.isDirectory()) {
//递归
doScan(scanPackage+"."+file.getName()); //比如com.lagou.controller
}else if(file.getName().endsWith(".class")){
String className = scanPackage + "." + file.getName().replaceAll(".class", "");
classNames.add(className);
}
} }

doInstance()方法:IOC实例化容器

代码如下

// ioc容器
private Map<String,Object> ioc = new HashMap<String,Object>(); // ioc容器
// 基于classNames缓存的类的全限定类名,以及反射技术,完成对象创建和管理
private void doInstance() { if(classNames.size() == 0) return; try{ for (int i = 0; i < classNames.size(); i++) {
String className = classNames.get(i); // com.lagou.demo.controller.DemoController // 反射
Class<?> aClass = Class.forName(className);
// 区分controller,区分service'
if(aClass.isAnnotationPresent(LagouController.class)) {
// controller的id此处不做过多处理,不取value了,就拿类的首字母小写作为id,保存到ioc中
String simpleName = aClass.getSimpleName();// DemoController
String lowerFirstSimpleName = lowerFirst(simpleName); // demoController
Object o = aClass.newInstance();
ioc.put(lowerFirstSimpleName,o);
}else if(aClass.isAnnotationPresent(LagouService.class)) {
LagouService annotation = aClass.getAnnotation(LagouService.class);
//获取注解value值
String beanName = annotation.value(); // 如果指定了id,就以指定的为准
if(!"".equals(beanName.trim())) {
ioc.put(beanName,aClass.newInstance());
}else{
// 如果没有指定,就以类名首字母小写
beanName = lowerFirst(aClass.getSimpleName());
ioc.put(beanName,aClass.newInstance());
} // service层往往是有接口的,面向接口开发,此时再以接口名为id,放入一份对象到ioc中,便于后期根据接口类型注入
Class<?>[] interfaces = aClass.getInterfaces();
for (int j = 0; j < interfaces.length; j++) {
Class<?> anInterface = interfaces[j];
// 以接口的全限定类名作为id放入
ioc.put(anInterface.getName(),aClass.newInstance());
}
}else{
continue;
} }
}catch (Exception e) {
e.printStackTrace();
}
} // 首字母小写方法
public String lowerFirst(String str) {
char[] chars = str.toCharArray();
if('A' <= chars[0] && chars[0] <= 'Z') {
chars[0] += 32;
}
return String.valueOf(chars);
}

doAutoWired()依赖注入:

//  实现依赖注入
private void doAutoWired() {
if(ioc.isEmpty()) {return;} // 有对象,再进行依赖注入处理 // 遍历ioc中所有对象,查看对象中的字段,是否有@LagouAutowired注解,如果有需要维护依赖注入关系
for(Map.Entry<String,Object> entry: ioc.entrySet()) {
// 获取bean对象中的字段信息
Field[] declaredFields = entry.getValue().getClass().getDeclaredFields();
// 遍历判断处理
for (int i = 0; i < declaredFields.length; i++) {
Field declaredField = declaredFields[i]; // @LagouAutowired private IDemoService demoService;
if(!declaredField.isAnnotationPresent(LagouAutowired.class)) {
continue;
} // 有该注解
LagouAutowired annotation = declaredField.getAnnotation(LagouAutowired.class);
String beanName = annotation.value(); // 需要注入的bean的id
if("".equals(beanName.trim())) {
// 没有配置具体的bean id,那就需要根据当前字段类型注入(接口注入) IDemoService
beanName = declaredField.getType().getName();
} // 开启赋值
declaredField.setAccessible(true); try {
declaredField.set(entry.getValue(),ioc.get(beanName));
} catch (IllegalAccessException e) {
e.printStackTrace();
} } } }

相关pojo实体类

package com.lagou.edu.mvcframework.pojo;

import javax.sound.midi.MetaEventListener;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Pattern; /**
* 封装handler方法相关的信息
*/
public class Handler { private Object controller; // method.invoke(obj,) private Method method; private Pattern pattern; // spring中url是支持正则的 private Map<String,Integer> paramIndexMapping; // 参数顺序,是为了进行参数绑定,key是参数名,value代表是第几个参数 <name,2> public Handler(Object controller, Method method, Pattern pattern) {
this.controller = controller;
this.method = method;
this.pattern = pattern;
this.paramIndexMapping = new HashMap<>();
} public Object getController() {
return controller;
} public void setController(Object controller) {
this.controller = controller;
} public Method getMethod() {
return method;
} public void setMethod(Method method) {
this.method = method;
} public Pattern getPattern() {
return pattern;
} public void setPattern(Pattern pattern) {
this.pattern = pattern;
} public Map<String, Integer> getParamIndexMapping() {
return paramIndexMapping;
} public void setParamIndexMapping(Map<String, Integer> paramIndexMapping) {
this.paramIndexMapping = paramIndexMapping;
}
}

LgDispatcherServlet里面的initHandleMapping()方法:

   private List<Handler> handlerMapping = new ArrayList<>();
/*
构造一个HandlerMapping处理器映射器
最关键的环节
目的:将url和method建立关联
*/
private void initHandlerMapping() {
if(ioc.isEmpty()) {return;} for(Map.Entry<String,Object> entry: ioc.entrySet()) {
// 获取ioc中当前遍历的对象的class类型
Class<?> aClass = entry.getValue().getClass(); if(!aClass.isAnnotationPresent(LagouController.class)) {continue;} String baseUrl = "";
if(aClass.isAnnotationPresent(LagouRequestMapping.class)) {
LagouRequestMapping annotation = aClass.getAnnotation(LagouRequestMapping.class);
baseUrl = annotation.value(); // 等同于/demo
} // 获取方法
Method[] methods = aClass.getMethods();
for (int i = 0; i < methods.length; i++) {
Method method = methods[i]; // 方法没有标识LagouRequestMapping,就不处理
if(!method.isAnnotationPresent(LagouRequestMapping.class)) {continue;} // 如果标识,就处理
LagouRequestMapping annotation = method.getAnnotation(LagouRequestMapping.class);
String methodUrl = annotation.value(); // /query
String url = baseUrl + methodUrl; // 计算出来的url /demo/query // 把method所有信息及url封装为一个Handler
Handler handler = new Handler(entry.getValue(),method, Pattern.compile(url)); // 计算方法的参数位置信息 // query(HttpServletRequest request, HttpServletResponse response,String name)
Parameter[] parameters = method.getParameters();
for (int j = 0; j < parameters.length; j++) {
Parameter parameter = parameters[j]; if(parameter.getType() == HttpServletRequest.class || parameter.getType() == HttpServletResponse.class) {
// 如果是request和response对象,那么参数名称写HttpServletRequest和HttpServletResponse
handler.getParamIndexMapping().put(parameter.getType().getSimpleName(),j);
}else{
handler.getParamIndexMapping().put(parameter.getName(),j); // <name,2>
} } // 建立url和method之间的映射关系(map缓存起来)
handlerMapping.add(handler); } } }

正式调用请求的doPost方法:

@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//真正处理请求 //根据uri获取到能处理当前请求的handler(从handlerMapping(List)中获取)
Handler handler=getHandler(req);
if(handler==null) {
resp.getWriter().write("404 not found");
return ;
} //参数绑定,获取所有参数类型数组,这个数组长度就是我们最后要传入的args数组的长度
Class<?>[] parameterTypes = handler.getMethod().getParameterTypes();
//根据上述数组长度创建一个新的数组(参数数组,使要传入反射调用的)
Object[] paraValues = new Object[parameterTypes.length];
//以下是为了向参数数组中塞值,而且还得保证参数的顺序和方法中的形参顺序一致 Map<String, String[]> parameterMap = req.getParameterMap();
//遍历request中所有参数
for (Map.Entry<String, String[]> param : parameterMap.entrySet()) {
//name=1&name=2 name[1,2]
String value = StringUtils.join(param.getValue(), ","); //如同1,2
//如果参数和方法中的参数匹配上了,则填充数据 if(!handler.getParamIndexMapping().containsKey(param.getKey())){continue;}
Integer index = handler.getParamIndexMapping().get(param.getKey()); //name 在第二个位置 paraValues[index] = value; //把前台传过来的参数值填充到对应的位置去 } Integer requestIndex = handler.getParamIndexMapping().get(HttpServletRequest.class.getSimpleName()); //0
paraValues[requestIndex]=req; Integer responseIndex = handler.getParamIndexMapping().get(HttpServletResponse.class.getSimpleName()); //1
paraValues[responseIndex]=resp; //最终调用handler的method属性
try {
handler.getMethod().invoke(handler.getController(),paraValues);
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} } private Handler getHandler(HttpServletRequest req) {
if(handlerMapping.isEmpty()){return null;}
String url=req.getRequestURI();
for (Handler handler : handlerMapping) {
Matcher matcher = handler.getPattern().matcher(url);
if(matcher.matches()){continue;}
return handler;
}
return null; }

pom.xml

需要定义编译器的编译细节,为了让编译器编译的时候能够记住形参的名字,而不是args1...等等。

<?xml version="1.0" encoding="UTF-8"?>

<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.0</modelVersion> <groupId>com.lagou.edu</groupId>
<artifactId>mvc</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging> <name>mvc Maven Webapp</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties> <dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.5</version>
</dependency>
</dependencies> <build>
<plugins> <!--编译插件定义编译细节-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>11</source>
<target>11</target>
<encoding>utf-8</encoding>
<!--告诉编译器,编译的时候记录下形参的真实名称-->
<compilerArgs>
<arg>-parameters</arg>
</compilerArgs>
</configuration>
</plugin> <plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<port>8080</port>
<path>/</path>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>8</source>
<target>8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>

至此,我们手写SpringMVC框架已经基本完成。

欢迎访问:

微信公众号(程序员资料站):code_data

手写SpringMVC框架(三)-------具体方法的实现的更多相关文章

  1. (二)springMvc原理和手写springMvc框架

    我们从两个方面了解springmvc执行原理,首先我们去熟悉springmvc执行的过程,然后知道原理后通过手写springmvc去深入了解代码中执行过程. (一)SpringMVC流程图 (二)Sp ...

  2. 手写SpringMVC 框架

    手写SpringMVC框架 细嗅蔷薇 心有猛虎 背景:Spring 想必大家都听说过,可能现在更多流行的是Spring Boot 和Spring Cloud 框架:但是SpringMVC 作为一款实现 ...

  3. 手写SpringMVC框架(二)-------结构开发设计

    续接前文, 手写SpringMVC框架(一)项目搭建 本节我们来开始手写SpringMVC框架的第二阶段:结构开发设计. 新建一个空的springmvc.properties, 里面写我们要扫描的包名 ...

  4. 手写SpringMVC框架(一)-------项目搭建

    SpringMVC处理请求的大致流程: 我们来开始着手手写一个SpringMVC框架. 新建一个springMVC项目,流程参见 SpringMVC框架搭建流程 引入servlet相关的jar包: & ...

  5. 五,手写SpringMVC框架,过滤器的使用

    8. 过滤器 8.1 编写字符过滤器 CharacterEncodingFilter 复制项目mymvc4,新建项目mymvc5 package com.hy.filter; import java. ...

  6. 纯手写SpringMVC框架,用注解实现springmvc过程

    闲话不多说,直接上代码! 1.第一步,首先搭建如下架构,其中,annotation中放置自己编写的注解,主要包括service controller qualifier RequestMapping ...

  7. 二. 手写SpringMVC框架

    1.1 新建DispatcherServlet 1.2 在src目录下,新建applicationContext.xml <?xml version="1.0" encodi ...

  8. 深度解析SpringMvc实现原理手写SpringMvc框架

    http://www.toutiao.com/a6340568603607171329/?tt_from=mobile_qq&utm_campaign=client_share&app ...

  9. 《四 spring源码》手写springmvc

    手写SpringMVC思路 1.web.xml加载  为了读取web.xml中的配置,我们用到ServletConfig这个类,它代表当前Servlet在web.xml中的配置信息.通过web.xml ...

随机推荐

  1. Java实现 蓝桥杯VIP 算法提高 连接乘积

    算法提高 连接乘积 时间限制:1.0s 内存限制:256.0MB 问题描述 192这个数很厉害,用它分别乘以1.2.3,会得到: 192 x 1 = 192 192 x 2 = 384 192 x 3 ...

  2. Java实现 蓝桥杯 历届试题 邮局

    问题描述 C村住着n户村民,由于交通闭塞,C村的村民只能通过信件与外界交流.为了方便村民们发信,C村打算在C村建设k个邮局,这样每户村民可以去离自己家最近的邮局发信. 现在给出了m个备选的邮局,请从中 ...

  3. 逐点分析,这样做Web端性能测试

    前言: 71%用户希望在手机上打开网页能跟电脑一样快: 5秒钟被认为是用户能忍受的最长响应时间,如果响应时间超过5秒,50%的移动用户会放弃: 33%失望的用户会使用竞品替代: 用户尝试三次出现同样性 ...

  4. (转)Zookeeper全解析——Paxos作为灵魂

    原计划在介绍完ZK Client之后就着手ZK Server的介绍,但是发现ZK Server所包含的内容实在太多,并不是简简单单一篇Blog就能搞定的.于是决定从基础搞起比较好. 那么ZK Serv ...

  5. sed中使用shell变量

    假设希望在 file_to_modified 文件最后新增一行以下信息:传入 shell 脚本文件的第一个参数,以及当前时间(YYYY-MM-DD HH:MMS) date "+%Y-%m- ...

  6. TensorFlow从0到1之TensorFlow多层感知机实现MINIST分类(22)

    TensorFlow 支持自动求导,可以使用 TensorFlow 优化器来计算和使用梯度.它使用梯度自动更新用变量定义的张量.本节将使用 TensorFlow 优化器来训练网络. 前面章节中,我们定 ...

  7. spark源码解析大全

      第1章 Spark 整体概述 1.1 整体概念   Apache Spark 是一个开源的通用集群计算系统,它提供了 High-level 编程 API,支持 Scala.Java 和 Pytho ...

  8. Asp.Net Core入门之自定义中间件

    什么是中间件? 这里引用官方解释: 中间件是用于组成应用程序管道来处理请求和响应的组件.管道内的每一个组件都可以选择是否将请求交给下一个组件.并在管道中调用下一个组件之前和之后执行某些操作.请求委托被 ...

  9. Vue中hash模式和history模式的区别

    vue-router 中hash模式和history模式. 在vue的路由配置中有mode选项,最直观的区别就是在hash模式下的地址栏里的URL夹杂着‘#’号 ,而history模式下没有.vue默 ...

  10. cb32a_c++_STL_算法_查找算法_(5)adjacent_find

    cb32a_c++_STL_算法_查找算法_(5)adjacent_findadjacent_find(b,e),b,begin(),e,end()adjacent_find(b,e,p),p-par ...