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")
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{
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 {
String className = "com.tedu.UserController";
Class clazz =Class.forName(className);
RestController restController = (RestController) clazz.getAnnotation(RestController.class);
if(restController!=null) {
Method[] methods = clazz.getDeclaredMethods();
for(Method method : methods) {
RequestMapping requestMapping = method.getAnnotation(RequestMapping.class);
if(requestMapping!=null) {
String url = requestMapping.value();
String methodName = method.getName();
ControllerDefinition definition = new ControllerDefinition(url,className,methodName);
将url和ControllerDefinition 加入HashMap中
urlMapping.put(url, definition);
}
}
}
}
}
}