1. 学习手写简单的 MVC 框架的实现流程
2. 自定义注解
2.1.@LagouService注解
package com.lagou.edu.mvcframework.annotations;
import java.lang.annotation.*;
@Documented
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface LagouService {
String value() default "";
}
2.2.@LagouRequestMapping注解
package com.lagou.edu.mvcframework.annotations;
import java.lang.annotation.*;
@Documented
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface LagouRequestMapping {
String value() default "";
}
2.3.@LagouAutowired注解
package com.lagou.edu.mvcframework.annotations;
import java.lang.annotation.*;
@Documented
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface LagouAutowired {
String value() default "";
}
2.4.@LagouController
package com.lagou.edu.mvcframework.annotations;
import java.lang.annotation.*;
@Documented
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface LagouService {
String value() default "";
}
3. 运用注解写Demo代码
3.1 Service接口及其实现类(使用自定义注解@LagouService)
(1)IDemoService.java
package com.lagou.edu.demo.service;
public interface IDemoService {
String get(String name);
}
(2)DemoServiceImpl.java
package com.lagou.edu.demo.service.impl;
import com.lagou.edu.demo.service.IDemoService;
import com.lagou.edu.mvcframework.annotations.LagouService;
@LagouService
public class DemoServiceImpl implements IDemoService {
@Override
public String get(String name) {
System.out.println("service 实现类中的name参数:" + name);
return name;
}
}
3.2 Controller类 (使用自定义注解@LagouController,@LagouRequestMapping,@LagouAutowired)
DemoController.java
package com.lagou.edu.demo.controller;
import com.lagou.edu.demo.service.IDemoService;
import com.lagou.edu.mvcframework.annotations.LagouAutowired;
import com.lagou.edu.mvcframework.annotations.LagouController;
import com.lagou.edu.mvcframework.annotations.LagouRequestMapping;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@LagouController
@LagouRequestMapping("/demo")
public class DemoController {
@LagouAutowired
private IDemoService demoService;
@LagouRequestMapping("/query")
public String query(HttpServletRequest request, HttpServletResponse response, String name){
return demoService.get(name);
}
}
4. 实现自定义框架
4.1. src\main\resources\springmvc.properties 扫描包的配置
scanPackage=com.lagou.demo
4.2. src\main\webapp\WEB-INF\web.xml 的配置
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Archetype Created Web Application</display-name>
<!-- 配置DispatcherServlet前端控制器 -->
<servlet>
<servlet-name>lagoumvc</servlet-name>
<servlet-class>com.lagou.edu.mvcframework.servlet.LgDispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>springmvc.properties</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>lagoumvc</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
4.3. 封装handler方法相关的信息
package com.lagou.edu.mvcframework.pojo;
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(object,)
private Method method;
private Pattern pattern; // spring中url是支持正则的
// 参数顺序,为了进行参数绑定,key是参数,value代表是第几个参数<name,2>
private Map<String, Integer> paramIndexMapping;
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;
}
}
4.4. LgDispatcherServlet.java 自定义 mvc 框架核心 java 实现
package com.lagou.edu.mvcframework.servlet;
import com.lagou.edu.demo.service.IDemoService;
import com.lagou.edu.mvcframework.annotations.LagouAutowired;
import com.lagou.edu.mvcframework.annotations.LagouController;
import com.lagou.edu.mvcframework.annotations.LagouRequestMapping;
import com.lagou.edu.mvcframework.annotations.LagouService;
import com.lagou.edu.mvcframework.pojo.Handler;
import org.apache.commons.lang3.StringUtils;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class LgDispatcherServlet extends HttpServlet {
private Properties properties = new Properties();
private List<String> classNames = new ArrayList<>(); // 缓存扫描到的类的全限定类名
// ioc容器
private Map<String, Object> ioc = new HashMap<>();
// handlerMapping
//private Map<String, Method> handlerMapping = new HashMap<>(); // 存贮url和method之间的映射关系
private List<Handler> handlerMapping = new ArrayList<>(); // 存贮url和method之间的映射关系
@Override
public void init(ServletConfig config) throws ServletException {
// 1.加载配置文件 springmvc.properties
String contextConfigLocation = config.getInitParameter("contextConfigLocation");
doLoadCofig(contextConfigLocation);
// 2.扫描相关的类,扫描注解
doScan(properties.getProperty("scanPackage"));
// 3.初始化bean对象(实现ioc容器,基于注解)
doInstance();
// 4.实现依赖注入
doAutoWired();
// 5.构造一个HandlerMapping处理器映射器,将配置好的url和Method建立映射关系
initHandlerMapping();
System.out.println("lagou mv 初始化完成");
}
/**
* 构建一个HandlerMapping处理器映射器
*/
private void initHandlerMapping() {
if(ioc.isEmpty()) {return;}
// 扫描ioc容器中
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 (Method method : methods) {
// 方法没有标识 @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 i = 0; i < parameters.length; i++) {
Parameter parameter = parameters[i];
if(parameter.getType() == HttpServletRequest.class || parameter.getType() == HttpServletResponse.class){
// 如果是request和response对象,那么参数名称写HttpServletRequest和HttpServletResponse
handler.getParamIndexMapping().put(parameter.getType().getSimpleName(),i);
}else{
handler.getParamIndexMapping().put(parameter.getName(),i); // <name, 2>
}
}
//建立url和method之间的映射关系(map缓存起来)
handlerMapping.add(handler);
}
}
}
/**
* 实现依赖注入
*/
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 (Field declaredField : declaredFields) { // @LagouAutowired private IDemoService demoService;
if(!declaredField.isAnnotationPresent(LagouAutowired.class)){
continue;
}else{
LagouAutowired annotation = declaredField.getAnnotation(LagouAutowired.class);
String beanName = annotation.value();
if("".equals(beanName)){
// 如果没有配置具体的bean id,那就需要根据当前字段类型注入(接口注入) IDemoService
beanName = declaredField.getType().getName();
}
// 开启赋值
declaredField.setAccessible(true);
try {
declaredField.set(entry.getValue(), ioc.get(beanName));
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}
}
}
/**
* ioc 容器
* 基于classNames缓存的全限定类名,以及反射技术,完成对象创建和管理
*/
private void doInstance() {
try {
if (classNames.size() == 0) {
return;
}
for (String className : classNames) { // com.lagou.edu.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)){
ioc.put(beanName, aClass.newInstance());
}else{
// 如果没有指定, 就以类名首字母小写
beanName = lowerFirst(aClass.getSimpleName());
ioc.put(beanName,aClass.newInstance());
}
// service层往往是有接口的, 面向接口开发,此时再以接口名为id, 放入一份对象到ioc容器中, 便于后期根据接口类型注入
Class<?>[] interfaces = aClass.getInterfaces();
for (Class<?> anInterface : interfaces) {
// 以接口的全限定类名作为id放入
ioc.put(anInterface.getName(),aClass.newInstance());
}
}else{
continue;
}
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
/**
* 首字母小写方法
*/
public String lowerFirst(String str){
char[] chars = str.toCharArray();
if(chars[0] >= 'A' && chars[0] <= 'Z'){
chars[0] += 32;
}
// return chars.toString();
return String.valueOf(chars);
}
/**
* 扫描类
* scanPackage: com.lagou.demo package----> 磁盘上的文件(file) com/lagou/demo
*/
private void doScan(String scanPackage) {
// 拿到classpath路径
String scanPackagePath = Thread.currentThread().getContextClassLoader().getResource("").getPath() + scanPackage.replaceAll("\\.", "/");
File pack = new File(scanPackagePath);
File[] files = pack.listFiles();
for (File file : files) {
if(file.isDirectory()){ // 子package则递归调用
// 递归
doScan(scanPackage + "." + file.getName());
}else if(file.getName().endsWith(".class")){ // class则添加到缓存中
String className = scanPackage + "." + file.getName().replaceAll(".class", "");
classNames.add(className);
}
}
}
/**
* 加载配置文件
* @param contextConfigLocation
*/
private void doLoadCofig(String contextConfigLocation) {
InputStream resourceAsStream = this.getClass().getClassLoader().getResourceAsStream(contextConfigLocation);
try {
properties.load(resourceAsStream);
} catch (IOException e) {
e.printStackTrace();
}
}
// 接收处理请求
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// 处理请求:根据url,找到对应的Method方法,进行调用
// 获取uri
// String requestURI = req.getRequestURI();
// Method method = handlerMapping.get(requestURI);
// // 反射调用,需要对象,需要传入参数,此处无法完成调用,没有把对象缓存起来,也没有参数!
// method.invoke()
// 根据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中的所有参数 (填充除了request,response之外的参数)
for (Map.Entry<String, String[]> param : parameterMap.entrySet()) {
// name=1&name=2 name [1,2]
String value = StringUtils.join(param.getValue(),",");
// 如果参数和方法中的参数匹配上了,填充数据
if(!handler.getParamIndexMapping().containsKey(param.getKey())){continue;}
// 方法形参确实有参数,找到对应的index,对应的把参数值放入paraValues
Integer index = handler.getParamIndexMapping().get(param.getKey());
paraValues[index] = value;
}
// 放入req
int requestIndex = handler.getParamIndexMapping().get(HttpServletRequest.class.getSimpleName());
paraValues[requestIndex] = req;
// 放入resp
int responseIndex = handler.getParamIndexMapping().get(HttpServletResponse.class.getSimpleName());
paraValues[responseIndex] = resp;
// 最终调用handler的mathod属性
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;
}
}
4.4. pom.xml
<?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>org.example</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>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.11</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<!-- 编译插件定义编译细节 -->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>11</source>
<target>11</target>
<encoding>utf-8</encoding>
<!-- 告诉编译器编译的时候记录一下形参的真实名字 -->
<compilerArgs>
<arg>-parameters</arg>
</compilerArgs>
</configuration>
</plugin>
<!-- tomcat 插件 -->
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<port>8080</port>
<path>/</path>
</configuration>
</plugin>
</plugins>
</build>
</project>
5. 小知识
1.学会自定义注解的定义方法,实际用法,以及其他的一些跟反射相关的技术
// 判断如果类上面没有标注@LagouController就不处理
if(!aClass.isAnnotationPresent(LagouController.class)) {continue;}
// 方法没有标识 @LagouRequestMapping 就不处理
if(!method.isAnnotationPresent(LagouRequestMapping.class)){continue;}
// 字段没有标注@LagouAutowired 就不处理
if(!declaredField.isAnnotationPresent(LagouAutowired.class)){continue;}
// 获取方法的参数
Parameter[] parameters = method.getParameters();
// 获取方法的所有参数类型
Class<?>[] parameterTypes = handler.getMethod().getParameterTypes();
// 获取类的所有接口
Class<?>[] interfaces = aClass.getInterfaces();
// service层往往是有接口的, 面向接口开发,此时再以接口名为id, 放入一份对象到ioc容器中, 便于后期根据接口类型注入
Class<?>[] interfaces = aClass.getInterfaces();
for (Class<?> anInterface : interfaces) {
// 以接口的全限定类名作为id放入
ioc.put(anInterface.getName(),aClass.newInstance());
}
// 获取参数的属性名字
parameter.getType().getSimpleName()
// 获取对象的所有字段
Field[] declaredFields = entry.getValue().getClass().getDeclaredFields();
// 获取自读类型的名字
beanName = declaredField.getType().getName();
// 开启赋值并赋值
declaredField.setAccessible(true);
declaredField.set(entry.getValue(), ioc.get(beanName));
// 从web.xml中获取 <param-name>contextConfigLocation</param-name>对应的<param-value>的值
String contextConfigLocation = config.getInitParameter("contextConfigLocation");
// 拿到classpath路径
String scanPackagePath = Thread.currentThread().getContextClassLoader().getResource("").getPath()
// 通过路径生成一个File对象
File pack = new File(scanPackagePath);
// 获取File对象的文件一览
File[] files = pack.listFiles();
// 获取属性配置文件的输入流,然后加载属性配置文件
InputStream resourceAsStream = this.getClass().getClassLoader().getResourceAsStream(contextConfigLocation);
private Properties properties = new Properties();
properties.load(resourceAsStream);
// 想resp中写入"404 not found")
resp.getWriter().write("404 not found");
2.通过maven-compiler-plugin插件定义编译细节,不定义的话无法保留参数的名字
<!-- 告诉编译器编译的时候记录一下形参的真实名字 -->
<compilerArgs>
<arg>-parameters</arg>
</compilerArgs>
- 通过scope定义provided避免运行时冲突
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>