1、ComponentScan 组件扫描类
- 一下知识本人都是在Maven工程下总结的,所以目录结构会不一样
- 这个类的作用是扫描所有的classes目录下的所有的字节码文件,找到相应的类,然后找到相应类上的注解
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()
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()
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/
4、去掉斜杠 / : substring(1)
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/