SpringMVC手写-核心逻辑


从网上看到了SpringMVC实现的最简单版本,大致体现了核心逻辑。
1、实现HttpServlet实现web请求访问
2、在实际处理方法post中对注解进行解析。
3、将访问URL上的地址和参数转到实际执行的Controller方法上。

注解的核心
1、Class<?> clazz = Class.forName(classUrl);
2、 if(clazz.isAnnotationPresent(MyDefineController.class)) 判断注解

核心技巧:将对象信息放在hashmap中;作为一个简单的IOC容器;

下面附上代码,包名做了修改,只是将核心DispatcherServlet 逻辑保存下来:

注解解析过程DispatcherServlet

package com.web.copy.servlet;

import com.web.copy.annotation.*;
import com.web.copy.controller.JamesController;

import java.io.File;
import java.io.IOException;
import java.lang.annotation.Annotation;
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.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class DispatcherServlet extends HttpServlet {

    List<String> classUrls = new ArrayList<String>();//保存待实例化所有类的路径
    Map<String,Object> ioc = new HashMap<String, Object>();//ioc容器
    Map<String,Object> urlHandlers = new HashMap<String, Object>();//地址映射


    //tomcat在启动的过程中---springmvc初始化要执行的
    public void init(ServletConfig config) throws ServletException {
        doScanPackage("com.enjoy");//扫描
        doInstance();//把找到特殊注解的类实例化对象, 保存到map  classUrls过滤 ioc
        doAutowired();//处理依赖关系
        doUrlMapping();//处理路径映射
    }
    //File file = new File("E:/SXX/com/enjoy")

    public void doScanPackage(String basePackage){//"com.enjoy"
        URL url = this.getClass().getClassLoader().getResource("/"+basePackage.replaceAll("\\.","/"));
        String fileStr = url.getFile();
        File file = new File(fileStr);
        String[] filesStr = file.list();
        for(String path : filesStr){
            File filePath = new File(fileStr+path);
            if(filePath.isDirectory()){
                doScanPackage(basePackage+"."+path);
            }else{
                classUrls.add(basePackage+"."+filePath.getName().replace(".class",""));
        }
        }
    }

    public void doInstance(){
        for(String classUrl:classUrls){
            try {
                Class<?> clazz = Class.forName(classUrl);
                if(clazz.isAnnotationPresent(EnjoyController.class)){
                    Object instance = clazz.newInstance();
                    EnjoyRequestMapping map1 = clazz.getAnnotation(EnjoyRequestMapping.class);
                    String key = map1.value();
                    ioc.put(key,instance);
                }else if(clazz.isAnnotationPresent(EnjoyService.class)){
                    Object instance1 = clazz.newInstance();
                    EnjoyService es = clazz.getAnnotation(EnjoyService.class);
                    String key = es.value();
                    ioc.put(key,instance1);
                }else {
                    continue;
                }


            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InstantiationException e) {
                e.printStackTrace();
            }
        }

    }

    public void  doAutowired(){
        for(Map.Entry<String,Object> entry:ioc.entrySet()){
            Object instance = entry.getValue();
            Class<?> clazz = instance.getClass();
            if(clazz.isAnnotationPresent(EnjoyController.class)){
                Field[] fields = clazz.getDeclaredFields();
                for(Field field:fields){
                    if(field.isAnnotationPresent(EnjoyAutowired.class)){
                        EnjoyAutowired ea= field.getAnnotation(EnjoyAutowired.class);

                        String key =ea.value();
                        Object ins = ioc.get(key);

                        field.setAccessible(true);
                        try {
                            field.set(instance, ins);
                        } catch (IllegalAccessException e) {
                            e.printStackTrace();
                        }

                    }
                }
            }
        }

    }

    public void  doUrlMapping(){
        for(Map.Entry<String,Object> entry:ioc.entrySet()) {
            Object instance = entry.getValue();
            Class<?> clazz = instance.getClass();
            if (clazz.isAnnotationPresent(EnjoyController.class)) {
                EnjoyRequestMapping erm = clazz.getAnnotation(EnjoyRequestMapping.class);
                String classPath = erm.value();

                Method[] methods = clazz.getMethods();
                for(Method method : methods){
                    if(method.isAnnotationPresent(EnjoyRequestMapping.class)){
                        EnjoyRequestMapping er = method.getAnnotation(EnjoyRequestMapping.class);
                        String methodPath = er.value();
                        urlHandlers.put(classPath+methodPath, method );
                    }
                }
            }
        }
    }

    //HTTP---doPost--
    //http://127.0.0.1:8080/enjoymvc/james/query?name=james&age=23

    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String uri = request.getRequestURI();
        String context = request.getContextPath();
        String path = uri.replace(context,"");

        Method method = (Method)urlHandlers.get(path);

        JamesController instance = (JamesController)ioc.get("/"+path.split("/")[1]);

        Object[] args = hand(request,response,method);
        try {
            method.invoke(instance,args);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    }


    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        this.doPost(request, response);
    }

    // 这里不用策略模式
    private static Object[] hand(HttpServletRequest request, HttpServletResponse response, Method method) {
        // 拿到当前待执行的方法有哪些参数
        Class<?>[] paramClazzs = method.getParameterTypes();
        // 根据参数的个数,new 一个参数的数组,将方法里的所有参数赋值到args来
        Object[] args = new Object[paramClazzs.length];

        int args_i = 0;
        int index = 0;
        for (Class<?> paramClazz : paramClazzs) {
            if (ServletRequest.class.isAssignableFrom(paramClazz)) {
                args[args_i++] = request;
            }
            if (ServletResponse.class.isAssignableFrom(paramClazz)) {
                args[args_i++] = response;
            }
            // 从0-3判断有没有RequestParam注解,很明显paramClazz为0和1时,不是,
            // 当为2和3时为@RequestParam,需要解析
            // [@com.enjoy.james.annotation.EnjoyRequestParam(value=name)]
            Annotation[] paramAns = method.getParameterAnnotations()[index];
            if (paramAns.length > 0) {
                for (Annotation paramAn : paramAns) {
                    if (EnjoyRequestParam.class.isAssignableFrom(paramAn.getClass())) {
                        EnjoyRequestParam rp = (EnjoyRequestParam) paramAn;
                        // 找到注解里的name和age
                        args[args_i++] = request.getParameter(rp.value());
                    }
                }
            }
            index++;
        }
        return args;
    }
}

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"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
	version="2.5">
	<display-name>enjoymvc</display-name>
	<servlet>
		<servlet-name>DispatcherServlet</servlet-name>
		<servlet-class>com.web.copy.servlet.DispatcherServlet</servlet-class>
		<load-on-startup>0</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>DispatcherServlet</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>

</web-app>

Controller

@MyDefineController
@MyDefineRequestMapping("/test")
public class TestController {

    @MyDefineAutowired("TestServiceImpl")//TestServiceImpl==KEY .get
    private TestService testService;

    private int i = 0;

    @MyDefineRequestMapping("/query")
    public void query(HttpServletRequest request, HttpServletResponse response,
                      @MyDefineRequestParam("name") String name,
                      @MyDefineRequestParam("age") String age) {

        try {
            PrintWriter pw = response.getWriter();
            String result = testService.query(name,age);
            pw.write(result);
        }
        catch (IOException e) {
            e.printStackTrace();
        }
    }

    public int div(){
        return  0;
    }

}

自定义注解

@Target(ElementType.FIELD)  //只能在类的成员变量上使用
@Retention(RetentionPolicy.RUNTIME) //表示在运行时可以通过反射获取  载体
@Documented  //javadoc   载体
public @interface MyDefineAutowired {
	String value() default "";
}



@Target(java.lang.annotation.ElementType.TYPE)  //只能在类上使用
@Retention(RetentionPolicy.RUNTIME) //表示在运行时可以通过反射获取  载体
@Documented  //javadoc   载体
public @interface MyDefineController {
	String value() default "";
}

@Target({java.lang.annotation.ElementType.TYPE, ElementType.METHOD})  //只能在类上使用
@Retention(RetentionPolicy.RUNTIME) //表示在运行时可以通过反射获取  载体
@Documented  //javadoc   载体
public @interface MyDefineRequestMapping {
	String value() default "";
}


@Target(ElementType.PARAMETER)  //只能在类的方法的参数上使用
@Retention(RetentionPolicy.RUNTIME) //表示在运行时可以通过反射获取  载体
@Documented  //javadoc   载体
public @interface MyDefineRequestParam {
	String value() default "";
}

@Target(java.lang.annotation.ElementType.TYPE)  //只能在类上使用
@Retention(RetentionPolicy.RUNTIME) //表示在运行时可以通过反射获取  载体
@Documented  //javadoc   载体
public @interface MyDefineService {
	String value() default "";
}
@EnjoyService("JamesServiceImpl")
public class JamesServiceImpl implements JamesService {

    public String query(String name, String age)
    {
        return "{name="+name+",age="+age+"}";
    }
}

public interface JamesService {
    String query(String name, String age);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值