自己手写SpringMVC框架

在我们平常开发过程中一定使用过springmvc框架,可有些朋友并没有察觉到该框架底层是如何实现的,接下来我带大家手写一个简单版springmvc框架,以便大家能够更好的理解自己正在使用的框架的内部原理

项目结构

首先定义spring框架中常用的注解:Controller、RequestMapping、Service、Quatifier(也就是Autowired)

 Controller注解

Controller注解用于标注作为handler处理器的controller类

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 Controller {
    String value() default "";
}

RequestMapping注解

RequestMapping用于标注controller中被映射的方法路径名

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.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface RequestMapping {

	String value() default "";
}

Service注解

Service注解用于标注业务层类

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 Service {
    String value() default "";
}

Quatifier注解

Quatifier用于标识哪些类需要被注入

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.FIELD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Quatifier {
	String value() default "";
}

DispatcherServlet类

SpringMVC的核心类,主要用于处理请求、映射访问路径、依赖注入

import com.frame.annotation.*;
import com.frame.controller.SpringmvcController;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class DispatcherServlet extends HttpServlet{
    private static final long serialVersionUID = 1L;
    List<String> packageNames = new ArrayList<String>();
    // 所有类的实例,key是注解的value,value是所有类的实例
    Map<String, Object> instanceMap = new HashMap<String, Object>();
    Map<String, Object> handerMap = new HashMap<String, Object>();
    public DispatcherServlet() {
        super();
    }

    //初始化方法
    public void init(ServletConfig config) throws ServletException {
	    super.init(config); 
        // 包扫描,获取包中的文件
        scanPackage("com.frame");
        try {
            filterAndInstance();
        } catch (Exception e) {
            e.printStackTrace();
        }
        // 建立映射关系
        handerMap();
        // 实现注入
        ioc();
    }

    //根据不同的注解来获取不同类的实例
    private void filterAndInstance() throws Exception {
        if (packageNames.size() <= 0) {
            return;
        }
        for (String className : packageNames) {
            Class<?> cName = Class.forName(className.replace(".class", "").trim());
            if (cName.isAnnotationPresent(Controller.class)) {
                Object instance = cName.newInstance();
                Controller controller = (Controller) cName.getAnnotation(Controller.class);
                String key = controller.value();
                instanceMap.put(key, instance);
            } else if (cName.isAnnotationPresent(Service.class)) {
                Object instance = cName.newInstance();
                Service service = (Service) cName.getAnnotation(Service.class);
                String key = service.value();
                instanceMap.put(key, instance);
            } else {
                continue;
            }
        }
    }

    //将被Quatifier注解休斯的类的实例注入到对应的类中
    private void ioc() {

        if (instanceMap.isEmpty())
            return;
        for (Map.Entry<String, Object> entry : instanceMap.entrySet()) {
            // 拿到里面的所有属性
            Field fields[] = entry.getValue().getClass().getDeclaredFields();
            for (Field field : fields) {
                field.setAccessible(true);// 可访问私有属性
                if (field.isAnnotationPresent(Quatifier.class));
                Quatifier quatifier = field.getAnnotation(Quatifier.class);
                String value = quatifier.value();
                field.setAccessible(true);
                try {
                    field.set(entry.getValue(), instanceMap.get(value));
                } catch (IllegalArgumentException e) {
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            }
        }
        SpringmvcController wuqi = (SpringmvcController) instanceMap.get("daidai");
        System.out.print(wuqi);
    }

    /**
     * 扫描包下的所有文件
     *
     * @param Package
     */
    private void scanPackage(String Package) {
        URL url = this.getClass().getClassLoader().getResource("/" + replaceTo(Package));// 将所有的.转义获取对应的路径
        String pathFile = url.getFile();
        File file = new File(pathFile);
        String fileList[] = file.list();
        for (String path : fileList) {
            File eachFile = new File(pathFile + path);
            if (eachFile.isDirectory()) {
                scanPackage(Package + "." + eachFile.getName());
            } else {
                packageNames.add(Package + "." + eachFile.getName());
            }
        }
    }

    /**
     * 建立映射关系
     */
    private void handerMap() {
        if (instanceMap.size() <= 0)
            return;
        for (Map.Entry<String, Object> entry : instanceMap.entrySet()) {
            if (entry.getValue().getClass().isAnnotationPresent(Controller.class)) {
                Controller controller = (Controller) entry.getValue().getClass().getAnnotation(Controller.class);
                String ctvalue = controller.value();
                Method[] methods = entry.getValue().getClass().getMethods();
                for (Method method : methods) {
                    if (method.isAnnotationPresent(RequestMapping.class)) {
                        RequestMapping rm = (RequestMapping) method.getAnnotation(RequestMapping.class);
                        String rmvalue = rm.value();
                        handerMap.put("/" + ctvalue + "/" + rmvalue, method);
                    } else {
                        continue;
                    }
                }
            } else {
                continue;
            }

        }
    }

    private String replaceTo(String path) {
        return path.replaceAll("\\.", "/");
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    /**
     * 用于处理请求,根据请求路径调用不同的controller
     */
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String url = req.getRequestURI();
        String context = req.getContextPath();
        String path = url.replace(context, "");
        Method method = (Method) handerMap.get(path);
        SpringmvcController controller = (SpringmvcController) instanceMap.get(path.split("/")[1]);
        try {
        	method.invoke(controller, new Object[] { req, resp, null }); 
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    }
}

MyService接口

import java.util.Map;

public interface MyService {

	 int insert(Map map);

     int delete(Map map);

	 int update(Map map);

	 int select(Map map);
}

MyServiceImpl实现类

import java.util.Map;
import com.frame.annotation.Service;
import com.frame.service.MyService;

@Service("MyServiceImpl")
public class MyServiceImpl implements MyService{

    public int insert(Map map) {
        System.out.println("MyServiceImpl:" + "insert");
        return 0;
    }

    public int delete(Map map) {
        System.out.println("MyServiceImpl:" + "delete");
        return 0;
    }

    public int update(Map map) {
        System.out.println("MyServiceImpl:" + "update");
        return 0;
    }

    public int select(Map map) {
        System.out.println("MyServiceImpl:" + "select");
        return 0;
    }
	
}

SpringmvcController类

@Controller("daidai")
public class SpringmvcController {

	@Quatifier("MyServiceImpl")
	MyService myService;
	
	@RequestMapping("insert")
	public String insert(HttpServletRequest request, HttpServletResponse response, String param){
		myService.insert(null);
		return "新增";
	}
	
	@RequestMapping("delete")
	public String delete(HttpServletRequest request,HttpServletResponse response,String param){
		myService.delete(null);
		return "删除";
	}
	
	@RequestMapping("update")
	public String update(HttpServletRequest request,HttpServletResponse response,String param){
		myService.update(null);
		return "更新";
	}
	
	@RequestMapping("select")
	public String select(HttpServletRequest request,HttpServletResponse response, String param){
		myService.select(null);
		return "选择";
	}
	
}

web.xml

需要在web.xml里配置DispatcherServlet,以便能够接收到请求

<?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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
  <display-name>Archetype Created Web Application</display-name>
  <servlet>
    <servlet-name>Servlet</servlet-name>
    <servlet-class>com.frame.servlet.DispatcherServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>Servlet</servlet-name>
    <url-pattern>/*</url-pattern>
  </servlet-mapping>
</web-app>

最后运行,测试

在浏览器地址栏输入请求地址,localhost:8080/frame/daidai/insert

然后查看控制台可以看到访问到了serviceImpl中的insert方法

以上便是手写SpringMVC的全部内容了,还请多多指教!

猜你喜欢

转载自blog.csdn.net/w8827130/article/details/81587129