简单的讲,大致的过程就是扫描controller,然后把controller中的RequestMapping给存起来
首先把Controller和RequestMapping两个注解写一下
/**
* controller声明
*/
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @ interface Controller {
}
/**
* RequestMapping声明
*
* @author heaboy
* @date 2023/03/06
*/
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE,ElementType.METHOD})
public @interface RequestMapping {
/**
*
* @return
*/
String value() default "";
}
然后写mvc的过程
public class HeaboyMvc {
private static HashMap<String, Map<String,Method>> map=new HashMap<>();
private static HashMap<String, Object> objMap=new HashMap<>();
public static void exec(String classPath,String methodPath){
if(objMap.get(classPath)==null){
System.out.println("没有这个类 404");
}else {
if(map.get(classPath).get(methodPath)==null){
System.out.println("没有这个方法 404");
}else {
try {
map.get(classPath).get(methodPath).invoke(objMap.get(classPath));
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
}
}
public static void scanner(String path,String packageName){
List<String> paths = traverseFolder2(path);
for (String p : paths) {
p=p.substring(path.length()-1);
try {
String className=packageName+"."+p.replaceAll( Matcher.quoteReplacement(File.separator),".");
String replace = className.replace(".class", "");
Class<?> cl = ClassLoader.getSystemClassLoader().loadClass(replace);
if(isController(cl)){
if(isRequestMapping(cl)){
RequestMapping requestMapping = getRequestMapping(cl);
if(map.containsKey(requestMapping.value())){
throw new RuntimeException("类多注解值:"+requestMapping.value());
}else {
map.put(requestMapping.value(),new HashMap<>());
objMap.put(requestMapping.value(),cl.newInstance());
}
Method[] declaredMethods = cl.getDeclaredMethods();
for (Method declaredMethod : declaredMethods) {
if(isRequestMapping(declaredMethod)){
RequestMapping mapping = getRequestMapping(declaredMethod);
if(map.get(requestMapping.value()).containsKey(mapping.value())){
throw new RuntimeException("方法多注解值:"+requestMapping.value());
}else {
map.get(requestMapping.value()).put(mapping.value(),declaredMethod);
}
}
}
}else {
throw new RuntimeException("类无requestMapping");
}
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
}
}
}
private static boolean isController(Class cl){
Annotation annotation = cl.getAnnotation(Controller.class);
if(annotation!=null){
return true;
}
return false;
}
private static boolean isRequestMapping(Class cl){
Annotation annotation = cl.getAnnotation(RequestMapping.class);
if(annotation!=null){
return true;
}
return false;
}
private static boolean isRequestMapping(Method method){
Annotation annotation = method.getAnnotation(RequestMapping.class);
if(annotation!=null){
return true;
}
return false;
}
private static RequestMapping getRequestMapping(Class cl){
Annotation annotation = cl.getAnnotation(RequestMapping.class);
if(annotation instanceof RequestMapping){
return (RequestMapping) annotation;
}
return null;
}
private static RequestMapping getRequestMapping(Method method){
Annotation annotation = method.getAnnotation(RequestMapping.class);
if(annotation instanceof RequestMapping){
return (RequestMapping) annotation;
}
return null;
}
private static List<String> traverseFolder2(String path) {
File file = new File(path);
List<String> classFiles=new ArrayList<>();
if (file.exists()) {
LinkedList<File> list = new LinkedList<File>();
File[] files = file.listFiles();
for (File file2 : files) {
if (file2.isDirectory()) {
list.add(file2);
} else {
classFiles.add(file2.getAbsolutePath());
}
}
File temp_file;
while (!list.isEmpty()) {
temp_file = list.removeFirst();
files = temp_file.listFiles();
for (File file2 : files) {
if (file2.isDirectory()) {
list.add(file2);
} else {
classFiles.add(file2.getAbsolutePath());
}
}
}
} else {
}
return classFiles;
}
}
最后是调用
public class Main {
static {
String path = Main.class.getResource("").getPath();
String packageName = Main.class.getPackage().getName();
HeaboyMvc.scanner(path,packageName);
}
public static void main(String[] args) {
HeaboyMvc.exec("","");
HeaboyMvc.exec("test","index1");
HeaboyMvc.exec("test","");
System.out.println("Hello World!");
}
}
简单的描述一下过程,就是把路径下的文件,拿到类信息,先判断类上有没有Controller,然后再看类上有没有RequestMapping,有的话就再objMap中存一个实例,然后map中存一个空HashMap,用来存方法。然后遍历这个类的方法,如果有@RequestMapping,就存进去,如果有相同的会抛异常