Project practice: component scanning implementation (1) – scanning all files on the class path

1. ComponentScan component scanning class

  • I have summarized the following knowledge under the Maven project, so the directory structure will be different
  • The function of this class is to scan all bytecode files in all classes directories, find the corresponding class, and then find the annotations on the corresponding class.
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 {
        //Analyze folder
        path = ComponentScan.class.getClassLoader().getResource("").getPath();
        // /F:/IdeaProjects/workspace/review/pro13-fruit-DispatcherServlet/target/pro13-fruit-DispatcherServlet-1.0-SNAPSHOT/WEB-INF/classes/
        //The root directory of the computer's hard disk is /, no matter what operating system it is. It's just Microsoft's artificial concept of dividing drive letters.
        //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);
        //Start parsing the folder - component scanning begins
        parseFile(rootDir);
    }

    private static void parseFile(File file) {
        if (file.exists()) {
            if (file.isDirectory()) {
                //Get all subdirectories
                File[] childFiles = file.listFiles();
                for (File childFile : childFiles) {
                      parseFile(childFile);
                }
            } else {
                String absPath = file.getAbsolutePath();
                System.out.println(absPath);
            }
        }
    }
}
  • The following are all the bytecode files we scanned in the classes folder
  • 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. Get the URL path: 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/
  • It can be found that the above path is not what we want, and file needs to be removed:

3. Remove 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/
  • /F:/IdeaProjects/workspace/review/pro13-fruit-DispatcherServlet/target/pro13-fruit-DispatcherServlet-1.0-SNAPSHOT/WEB-INF/classes/
  • We found that there is still an extra / slash and we need to remove the / slash
  • <strong>The root directory of your computer's hard drive is / , regardless of the operating system. It's just Microsoft's artificial concept of dividing drive letters</strong>

4. Remove the slash / : 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/

The knowledge points of the article match the official knowledge files, and you can further learn related knowledge. Java Skill TreeHomepageOverview 139025 people are learning the system