手写SpringMvc框架

RestController接口类
package org.springmvc;

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)
public @interface RestController {
}
RequestMapping接口类
package org.springmvc;

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 {
	String value();
}
ControllerDefinition类(定义url,类名,方法名)
package org.springmvc;

public class ControllerDefinition {
	String url;
	String className;
	String methodName;
	public ControllerDefinition(String url, String className, String methodName) {
		super();
		this.url = url;
		this.className = className;
		this.methodName = methodName;
	}
	public String getUrl() {
		return url;
	}
	public void setUrl(String url) {
		this.url = url;
	}
	public String getClassName() {
		return className;
	}
	public void setClassName(String className) {
		this.className = className;
	}
	public String getMethodName() {
		return methodName;
	}
	public void setMethodName(String methodName) {
		this.methodName = methodName;
	}
	
}
UserController类(加RestController)
package com.company;

import org.springmvc.RequestMapping;
import org.springmvc.RestController;

@RestController
public class UserController {
	@RequestMapping(value = "/register") //value 可以不写
	public String register() {
		return "注册成功";
	}
	@RequestMapping("login")
	public String login() {
		return "注册成功";
	}
}

AppMain类(SpringMvc框架启动类)
package org.springmvc;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.HashMap;

public class AppMain {

	static HashMap<String, ControllerDefinition> urlMapping = new HashMap<>();

	public static void main(String[] args) throws Throwable {
		//加载类
		loadObject();
		//处理请求
		requestProcess("/register");
		requestProcess("/login");
	}

	private static void requestProcess(String url) throws Throwable{
		// TODO Auto-generated method stub
		//urlMapping找到ControllerDefinition 
		ControllerDefinition definition = urlMapping.get(url);
		//得到类名方法名
		String className = definition.getClassName();
		String methodName = definition.getMethodName();
		//根据类名,创建类对象
		Class clazz = Class.forName(className);
		//通过类对象创建对象
		Object object = clazz.newInstance();
		//根据方法名,得到一个方法
		Method method = clazz.getDeclaredMethod(methodName);
		//调用这个方法
		Object result = method.invoke(object);
		System.out.println("url = "+url+",结果"+result);
	}

	private static void loadObject() throws Throwable {
		// TODO Auto-generated method stub
		String className = "com.tedu.UserController";
		//得到类对象
		Class clazz =Class.forName(className);
		//判断有没有加@RestController
		RestController restController = (RestController) clazz.getAnnotation(RestController.class);
		if(restController!=null) {
		//得到所有方法
			Method[] methods = clazz.getDeclaredMethods();
			//遍历所有方法
			for(Method method : methods) {
			//判断有没有加@RequsetMapping
				RequestMapping requestMapping = method.getAnnotation(RequestMapping.class);
				if(requestMapping!=null) {
				//得到Url
					String url = requestMapping.value();
					String methodName = method.getName();
					//创建ControllerDefinition 
					ControllerDefinition definition = new ControllerDefinition(url,className,methodName);
					将url和ControllerDefinition 加入HashMap中
					urlMapping.put(url, definition);
				}
			}


		}
	}
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值