欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

模拟SpringMVC简单实现注解@RequestMapping

程序员文章站 2022-04-29 23:53:21
...

2最近一直在研究SpringMVC,根据自己的理解,模拟SpringMVC,简单实现注解@RequestMapping

RequestMapping.java注解类

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)
public @interface RequestMapping {

    public String value() default "";

}

Controller控制层

public class Controller {

    @RequestMapping(value="getinfo")
    public String getInfo(String username, String password) {
        System.out.println("execute getInfo");
        System.out.println("username = " + username);
        System.out.println("password = " + password);
        return "";
    }

    @RequestMapping(value="getinfo2")
    public String getInfo2(String username, String randomArg) {
        System.out.println("execute getInfo2");
        System.out.println("username = " + username);
        System.out.println("randomArg = " + randomArg);
        return "";
    }

    public String getInfo3() {
        System.out.println("execute getInfo3");
        return "";
    }

}

SpringRequestMapping.java模拟实现类

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Set;
import org.springframework.core.LocalVariableTableParameterNameDiscoverer;

public class SpringRequestMapping {

    private static HashMap<String, Method> uriMethodMap = new HashMap<String, Method>();
    /**
     * 扫描含有RequestMapping注解的方法,并将uri和对应的方法存储起来
     * @param clazz
     */
    private static void scanRequestMappings(Class<?> clazz) {

        Method[] methods = clazz.getDeclaredMethods();

        for(Method method : methods) {
            if (method.isAnnotationPresent(RequestMapping.class)) {
                RequestMapping rm = method.getAnnotation(RequestMapping.class);
                uriMethodMap.put(rm.value(), method);
            }
        }
    }

    /**
     * Spring利用类LocalVariableTableParameterNameDiscoverer得到方法参数名,并将相应的参数值注入到RequestMapping的方法中
     * @param method
     * @param formParams
     * @return 
     */
    public static Object[] getParamsToBePassed(Method method, HashMap<String, Object> formParams) {

        LocalVariableTableParameterNameDiscoverer discover = new LocalVariableTableParameterNameDiscoverer();

        String[] methodParams = discover.getParameterNames(method);

        ArrayList<Object> paramsToBePassed = new ArrayList<Object>();

        for (String methodParam : methodParams) {
            Set<String> formParamKeys = formParams.keySet();

            //和表单里匹配的参数则相应设置值,不匹配的参数将默认设置为空值
            if(formParamKeys.contains(methodParam)) {
                paramsToBePassed.add(formParams.get(methodParam));
            }else {
                paramsToBePassed.add(null);
            }
        }
        return paramsToBePassed.toArray();
    }


    /**
     * ,将Form表单的参数注入方法中。根据uri执行对应的处理方法
     * @param clazz
     * @param uri
     * @param formParams
     */
    public static void executeRequest(Class<?> clazz, String uri, HashMap<String, Object> formParams) {

        Method method = uriMethodMap.get(uri);

        Object[] paramsToBePassed = getParamsToBePassed(method, formParams);

        try {
            method.invoke(clazz.newInstance(), paramsToBePassed);
        } catch (Exception e) {
        }
    }

}

测试类

import java.util.HashMap;

public class SpringRequestMappingTest {

    public static void main(String[] args) {

        SpringRequestMapping srm = new SpringRequestMapping();

        srm.scanRequestMappings(Controller.class);
        //模拟前端表单的参数
        HashMap<String, Object> formParams = new HashMap<String, Object>();
        formParams.put("username", "admin");
        formParams.put("password", "123456");

        srm.executeRequest(Controller.class, "getinfo", formParams);
        srm.executeRequest(Controller.class, "getinfo2", formParams);
    }
}

控制台输出结果

execute getInfo
username = admin
password = 123456
execute getInfo2
username = admin
password = null(不匹配的参数将默认设置为空值