一、仿写流程
1、初始化
- 扫描某路径下的文件夹和文件 通过字符串处理得到全限定名
传入指定包的地址
输出 List 里边的所有java文件(”.java") - 反射创建对象
url->对象 Map<String,Object> - 识别是否有我们所需的注解,并获取注解内的值
识别类上的注解Controller
识别类上的注解RequestMapping,并获取value值
识别该类中方法上的注解RequestMapping,并获取value值
需要构建 每个类每个方法对应url的映射结构
2、测试
- 编写自定义注解和测试类
编写测试main方法,传入请求访问路径和方法,执行
二、代码实现
1、自定义注解Controller和RequestMapping
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface Controller {
String value() default "user";
}
@Target(value ={ElementType.METHOD,ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface RequestMapping {
String value() default "index";
}
2、扫描本项目下Controller下所有的java文件
public class GetAllFiles {
static int count = 0;
public static List<String> getAllFiles(String path) {
System.out.println(path);
List<String> qualifiedPaths = new ArrayList<String>();
String fileType = ".java";
File file = new File(path);
long start = System.currentTimeMillis();
listAllFiles(file, fileType, qualifiedPaths);
long end = System.currentTimeMillis();
System.out.println("共花费:" + (end - start) + "毫秒");
System.out.println("有" + count + "个文件");
return qualifiedPaths;
}
public static void listAllFiles(File file, String fileType, List<String> qualifiedPaths){
if (file.isFile()){
count++;
System.out.println(file);
String path = file.getPath().replace('\\', '.');
int begin = path.indexOf("Controller");
int end = path.indexOf(".java");
String qualifiedPath = path.substring(begin, end);
qualifiedPaths.add(qualifiedPath);
System.out.println(qualifiedPath);
} else {
if (file.exists() && file.isDirectory()){
File[] files = file.listFiles();
for (File file1 : files){
listAllFiles(file1, fileType,qualifiedPaths);
}
}
}
}
}
3、识别注解,完成映射
public class SpringMVC {
private static Map<String, Object> mapobj = new HashMap<String, Object>();
private static Map<String, HashMap<String, Method>> map = new HashMap<>();
public static void exec(String classPath,String methodPath){
if(mapobj.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(mapobj.get(classPath));
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
}
}
public static void springmvc(String path) throws ClassNotFoundException, InstantiationException, IllegalAccessException {
path = "D:\\appdata\\ssm-workspace\\Spring\\src\\Controller";
List<String> classNames = GetAllFiles.getAllFiles(path);
for (String className:classNames) {
Class cl = Class.forName(className);
Annotation[] annotations = cl.getAnnotations();
for (Annotation annotation : annotations) {
System.out.println(annotation);
}
Controller controller = (Controller) cl.getAnnotation(Controller.class);
if (controller!=null) {
RequestMapping mapping = (RequestMapping) cl.getAnnotation(RequestMapping.class);
mapobj.put(mapping.value(), cl.newInstance());
map.put(mapping.value(), new HashMap<String, Method>());
Method[] methods = cl.getDeclaredMethods();
for (Method m:methods) {
Annotation[] annotations_m = m.getAnnotations();
for (Annotation annotation : annotations_m) {
RequestMapping requestMapping = (RequestMapping) m.getAnnotation(RequestMapping.class);
map.get(mapping.value()).put(requestMapping.value(), m);
}
}
}
}
System.out.println("映射结果:");
for(String key:map.keySet()){
System.out.println(key+" "+map.get(key));
}
}
}
4、编写Controller,准备测试
@Controller
@RequestMapping("test")
public class TestController {
@RequestMapping
public String index(){
System.out.println("test->index");
return "";
}
@RequestMapping("index1")
public String index1(){
System.out.println("test->index1");
return "";
}
}
@Controller
@RequestMapping("test2")
public class TestController2 {
@RequestMapping("index2")
public void index2() {
System.out.println("test2->index2");
}
}
5、初始化并编写main方法,开始测试
public class Main {
static {
String path = Main.class.getResource("").getPath();
String packageName = Main.class.getPackage().getName();
String scannerPath = path.replaceAll(packageName, "Controller");
try {
SpringMVC.springmvc(scannerPath);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
System.out.println("测试结果:");
SpringMVC.exec("test", "index");
SpringMVC.exec("test", "index1");
SpringMVC.exec("test2", "index2");
}
}
6、执行,测试结果