项目实战:组件扫描实现(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/
相关推荐
钱多多_qdd6 分钟前
spring cache源码解析(四)——从@EnableCaching开始来阅读源码
java·spring boot·spring
waicsdn_haha8 分钟前
Java/JDK下载、安装及环境配置超详细教程【Windows10、macOS和Linux图文详解】
java·运维·服务器·开发语言·windows·后端·jdk
Q_192849990618 分钟前
基于Spring Boot的摄影器材租赁回收系统
java·spring boot·后端
Code_流苏20 分钟前
VSCode搭建Java开发环境 2024保姆级安装教程(Java环境搭建+VSCode安装+运行测试+背景图设置)
java·ide·vscode·搭建·java开发环境
禁默1 小时前
深入浅出:AWT的基本组件及其应用
java·开发语言·界面编程
Cachel wood1 小时前
python round四舍五入和decimal库精确四舍五入
java·linux·前端·数据库·vue.js·python·前端框架
Code哈哈笑1 小时前
【Java 学习】深度剖析Java多态:从向上转型到向下转型,解锁动态绑定的奥秘,让代码更优雅灵活
java·开发语言·学习
gb42152871 小时前
springboot中Jackson库和jsonpath库的区别和联系。
java·spring boot·后端
程序猿进阶1 小时前
深入解析 Spring WebFlux:原理与应用
java·开发语言·后端·spring·面试·架构·springboot