1、ControllerDefinition
package com.csdn.mymvc.core;
import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;
import java.lang.reflect.Method;
import java.util.HashMap;
public class ControllerDefinition {
private String requestMapping;
private Object controllerBean;
private Map methodMappingMap = new HashMap<>();
2、ComponentScan
package com.csdn.mymvc.core;
import com.csdn.mymvc.annotation.*;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Modifier;
public class ComponentScan {
public static Map beanFactory = new HashMap<>();
public static Map controllerBeanMap = new HashMap<>();
static String path = null;
path = ComponentScan.class.getClassLoader().getResource("").getPath();
path = path.substring(1);
File rootDir = new File(path);
beanFactory.values().forEach(System.out::println);
beanFactory.values().forEach(bean -> {
Field[] fields = bean.getClass().getDeclaredFields();
.filter(field -> field.getDeclaredAnnotation(Autowire.class) != null)
String fieldTypeName = field.getType().getName();
Object filedValue = beanFactory.values().stream().filter(instance -> {
return field.getType().isAssignableFrom(instance.getClass());
}).findFirst().orElseThrow(() -> new RuntimeException(fieldTypeName + "装配失败!"));
field.setAccessible(true);
field.set(bean, filedValue);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
beanFactory.values().stream()
.filter(bean -> bean.getClass().getDeclaredAnnotation(RequestMapping.class) != null)
ControllerDefinition controllerDefinition = new ControllerDefinition();
String requestMapping = bean.getClass().getDeclaredAnnotation(RequestMapping.class).value();
Object controllerBean = bean;
controllerDefinition.setRequestMapping(requestMapping);
controllerDefinition.setControllerBean(controllerBean);
Arrays.stream(bean.getClass().getDeclaredMethods()).forEach(method -> {
GetMapping getMappingAnnotation = method.getDeclaredAnnotation(GetMapping.class);
String methodMapping = null;
if (getMappingAnnotation != null) {
methodMapping = getMappingAnnotation.value();
methodMapping = "get_" + methodMapping;
PostMapping postMappingAnnotation = method.getDeclaredAnnotation(PostMapping.class);
if (postMappingAnnotation != null) {
methodMapping = postMappingAnnotation.value();
methodMapping = "post_" + methodMapping;
if (methodMapping != null) {
controllerDefinition.getMethodMappingMap().put(methodMapping, method);
controllerBeanMap.put(requestMapping, controllerDefinition);
System.out.println(beanFactory);
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
} catch (InvocationTargetException e) {
throw new RuntimeException(e);
} catch (NoSuchMethodException e) {
throw new RuntimeException(e);
} catch (InstantiationException e) {
throw new RuntimeException(e);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
private static void parseFile(File file) throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
if (file.isDirectory()) {
File[] childFiles = file.listFiles();
for (File childFile : childFiles) {
String absPath = file.getAbsolutePath();
String fullClassPath = absPath.substring(path.length());
if (fullClassPath.endsWith(".class")) {
String fullClassPathName = fullClassPath.substring(0, fullClassPath.length() - ".class".length());
String fullClassName = fullClassPathName.replaceAll("\\\\", ".");
Class> clazz = Class.forName(fullClassName);
if (clazz.toString().startsWith("class")) {
if (!Modifier.isAbstract(clazz.getModifiers())) {
Optional
optional = Arrays.stream(clazz.getDeclaredAnnotations()).filter(annotation -> { return (annotation instanceof Controller || annotation instanceof Service || annotation instanceof Repository);
if (!optional.isEmpty()) {
Object bean = clazz.getDeclaredConstructor().newInstance();
beanFactory.put(fullClassName, bean);

