项目实战:组件扫描实现(1)-扫描类路径所有文件

1、ComponentScan 组件扫描类

  • 一下知识本人都是在Maven工程下总结的,所以目录结构会不一样
  • 这个类的作用是扫描所有的classes目录下的所有的字节码文件,找到相应的类,然后找到相应类上的注解
java 复制代码
package com.csdn.mymvc.core;
import java.io.File;
import java.util.HashMap;
import java.util.Map;
public class ComponentScan {
    public static Map<String, Object> beanFactory = new HashMap<>();
    static String path = null;
    static {
        //分析文件夹
        path = ComponentScan.class.getClassLoader().getResource("").getPath();
        //  /F:/IdeaProjects/workspace/review/pro13-fruit-DispatcherServlet/target/pro13-fruit-DispatcherServlet-1.0-SNAPSHOT/WEB-INF/classes/
        //计算机的硬盘根目录是 / ,不论是什么操作系统。只是微软人为的分出盘符的概念
        //System.out.println(path);
        path = path.substring(1);
        //   F:/IdeaProjects/workspace/review/pro13-fruit-DispatcherServlet/target/pro13-fruit-DispatcherServlet-1.0-SNAPSHOT/WEB-INF/classes/
        File rootDir = new File(path);
        //开始解析文件夹 - 组件扫描工作开始
        parseFile(rootDir);
    }

    private static void parseFile(File file) {
        if (file.exists()) {
            if (file.isDirectory()) {
                //获取所有的子目录
                File[] childFiles = file.listFiles();
                for (File childFile : childFiles) {
                      parseFile(childFile);
                }
            } else {
                String absPath = file.getAbsolutePath();
                System.out.println(absPath);
            }
        }
    }
}
  • 以下是我们扫描classes文件夹下的所有字节码文件
  • F:\IdeaProjects\workspace\review\pro13-fruit-DispatcherServlet\target\pro13-fruit-DispatcherServlet-1.0-SNAPSHOT\WEB-INF\classes\com\csdn\fruit\dao\FruitDao.class
  • F:\IdeaProjects\workspace\review\pro13-fruit-DispatcherServlet\target\pro13-fruit-DispatcherServlet-1.0-SNAPSHOT\WEB-INF\classes\com\csdn\fruit\dao\impl\FruitDaoImpl.class
  • F:\IdeaProjects\workspace\review\pro13-fruit-DispatcherServlet\target\pro13-fruit-DispatcherServlet-1.0-SNAPSHOT\WEB-INF\classes\com\csdn\fruit\dto\PageInfo.class
  • F:\IdeaProjects\workspace\review\pro13-fruit-DispatcherServlet\target\pro13-fruit-DispatcherServlet-1.0-SNAPSHOT\WEB-INF\classes\com\csdn\fruit\dto\PageQueryParam.class
  • F:\IdeaProjects\workspace\review\pro13-fruit-DispatcherServlet\target\pro13-fruit-DispatcherServlet-1.0-SNAPSHOT\WEB-INF\classes\com\csdn\fruit\dto\Result.class
  • F:\IdeaProjects\workspace\review\pro13-fruit-DispatcherServlet\target\pro13-fruit-DispatcherServlet-1.0-SNAPSHOT\WEB-INF\classes\com\csdn\fruit\service\FruitService.class
  • F:\IdeaProjects\workspace\review\pro13-fruit-DispatcherServlet\target\pro13-fruit-DispatcherServlet-1.0-SNAPSHOT\WEB-INF\classes\com\csdn\fruit\service\impl\FruitServiceImpl.class
  • F:\IdeaProjects\workspace\review\pro13-fruit-DispatcherServlet\target\pro13-fruit-DispatcherServlet-1.0-SNAPSHOT\WEB-INF\classes\com\csdn\fruit\servlet\FruitController.class
  • F:\IdeaProjects\workspace\review\pro13-fruit-DispatcherServlet\target\pro13-fruit-DispatcherServlet-1.0-SNAPSHOT\WEB-INF\classes\com\csdn\fruit\util\GsonUtil.class
  • F:\IdeaProjects\workspace\review\pro13-fruit-DispatcherServlet\target\pro13-fruit-DispatcherServlet-1.0-SNAPSHOT\WEB-INF\classes\com\csdn\fruit\util\RequestUtil.class
  • F:\IdeaProjects\workspace\review\pro13-fruit-DispatcherServlet\target\pro13-fruit-DispatcherServlet-1.0-SNAPSHOT\WEB-INF\classes\com\csdn\fruit\util\ResponseUtil.class
  • F:\IdeaProjects\workspace\review\pro13-fruit-DispatcherServlet\target\pro13-fruit-DispatcherServlet-1.0-SNAPSHOT\WEB-INF\classes\com\csdn\mymvc\annotation\Autowire.class
  • F:\IdeaProjects\workspace\review\pro13-fruit-DispatcherServlet\target\pro13-fruit-DispatcherServlet-1.0-SNAPSHOT\WEB-INF\classes\com\csdn\mymvc\annotation\Controller.class
  • F:\IdeaProjects\workspace\review\pro13-fruit-DispatcherServlet\target\pro13-fruit-DispatcherServlet-1.0-SNAPSHOT\WEB-INF\classes\com\csdn\mymvc\annotation\GetMapping.class
  • F:\IdeaProjects\workspace\review\pro13-fruit-DispatcherServlet\target\pro13-fruit-DispatcherServlet-1.0-SNAPSHOT\WEB-INF\classes\com\csdn\mymvc\annotation\PostMapping.class
  • F:\IdeaProjects\workspace\review\pro13-fruit-DispatcherServlet\target\pro13-fruit-DispatcherServlet-1.0-SNAPSHOT\WEB-INF\classes\com\csdn\mymvc\annotation\Repository.class
  • F:\IdeaProjects\workspace\review\pro13-fruit-DispatcherServlet\target\pro13-fruit-DispatcherServlet-1.0-SNAPSHOT\WEB-INF\classes\com\csdn\mymvc\annotation\RequestMapping.class
  • F:\IdeaProjects\workspace\review\pro13-fruit-DispatcherServlet\target\pro13-fruit-DispatcherServlet-1.0-SNAPSHOT\WEB-INF\classes\com\csdn\mymvc\annotation\Service.class
  • F:\IdeaProjects\workspace\review\pro13-fruit-DispatcherServlet\target\pro13-fruit-DispatcherServlet-1.0-SNAPSHOT\WEB-INF\classes\com\csdn\mymvc\core\ComponentScan.class
  • F:\IdeaProjects\workspace\review\pro13-fruit-DispatcherServlet\target\pro13-fruit-DispatcherServlet-1.0-SNAPSHOT\WEB-INF\classes\com\csdn\mymvc\core\DispatcherServlet.class
  • F:\IdeaProjects\workspace\review\pro13-fruit-DispatcherServlet\target\pro13-fruit-DispatcherServlet-1.0-SNAPSHOT\WEB-INF\classes\com\csdn\mymvc\listener\ContextLoaderListener.class

2、获取URL路径: getResource()

java 复制代码
URL resource = ComponentScan.class.getClassLoader().getResource("");
System.out.println(resource);
file:/F:/IdeaProjects/workspace/review/pro13-fruit-DispatcherServlet/target/pro13-fruit-DispatcherServlet-1.0-SNAPSHOT/WEB-INF/classes/
  • file: /F:/IdeaProjects/workspace/review/pro13-fruit-DispatcherServlet/target/pro13-fruit-DispatcherServlet-1.0-SNAPSHOT/WEB-INF/classes/
  • 可以发现以上路径不是我们想要的,需要去掉 file:

3、去掉 file: getPath()

java 复制代码
String path = ComponentScan.class.getClassLoader().getResource("").getPath();
System.out.println(path);
/F:/IdeaProjects/workspace/review/pro13-fruit-DispatcherServlet/target/pro13-fruit-DispatcherServlet-1.0-SNAPSHOT/WEB-INF/classes/
  • / F:/IdeaProjects/workspace/review/pro13-fruit-DispatcherServlet/target/pro13-fruit-DispatcherServlet-1.0-SNAPSHOT/WEB-INF/classes/
  • 我们发现还是多了一个 / 斜杠,需要把 / 斜杠去掉
复制代码
计算机的硬盘根目录是 / ,不论是什么操作系统。只是微软人为的分出盘符的概念

4、去掉斜杠 / : substring(1)

java 复制代码
String path = ComponentScan.class.getClassLoader().getResource("").getPath();
path = path.substring(1);
System.out.println(path)
F:/IdeaProjects/workspace/review/pro13-fruit-DispatcherServlet/target/pro13-fruit-DispatcherServlet-1.0-SNAPSHOT/WEB-INF/classes/
  • F:/IdeaProjects/workspace/review/pro13-fruit-DispatcherServlet/target/pro13-fruit-DispatcherServlet-1.0-SNAPSHOT/WEB-INF/classes/
相关推荐
晨曦_子画4 分钟前
编程语言之战:AI 之后的 Kotlin 与 Java
android·java·开发语言·人工智能·kotlin
南宫生27 分钟前
贪心算法习题其三【力扣】【算法学习day.20】
java·数据结构·学习·算法·leetcode·贪心算法
Heavydrink40 分钟前
HTTP动词与状态码
java
ktkiko1143 分钟前
Java中的远程方法调用——RPC详解
java·开发语言·rpc
计算机-秋大田1 小时前
基于Spring Boot的船舶监造系统的设计与实现,LW+源码+讲解
java·论文阅读·spring boot·后端·vue
神里大人1 小时前
idea、pycharm等软件的文件名红色怎么变绿色
java·pycharm·intellij-idea
小冉在学习1 小时前
day53 图论章节刷题Part05(并查集理论基础、寻找存在的路径)
java·算法·图论
代码之光_19802 小时前
保障性住房管理:SpringBoot技术优势分析
java·spring boot·后端
ajsbxi2 小时前
苍穹外卖学习记录
java·笔记·后端·学习·nginx·spring·servlet
StayInLove2 小时前
G1垃圾回收器日志详解
java·开发语言