Dra-M Dra-M
首页
技术
冥思
哲学
收藏
  • 分类
  • 标签
  • 归档
GitHub (opens new window)

莫小龙

保持理智,相信未来。
首页
技术
冥思
哲学
收藏
  • 分类
  • 标签
  • 归档
GitHub (opens new window)
  • Java

    • JUC

    • Spring

      • 彻底透析SpringBoot jar可执行原理
      • 使用Hibernate-validator的Notnull等注解校验参数并使用全局异常处理
      • 简陋的注解扫描实现
      • SpringBoot+Swagger,快速构建REST API文档
      • Spring&kotlin 异常 NoClassDefFoundError
      • PageHelper不生效问题
      • SpringDataJpa一些注意事项
      • SpringDataJpa的动态条件查询和分页查询
    • Gradle

    • 面向对象

    • Stream流式编程思想及常用API介绍
    • JAVA快速导出Excel
  • Golang

  • 编程思想

  • 微服务

  • 中间件

  • Python

  • 运维

  • 技术
  • Java
  • Spring
莫小龙
2020-03-07

简陋的注解扫描实现

意识流简陋的实现了下注解扫描。 思路: 拿到当前包下所有类文件,反射成类对象, 判断是否包含@Component注解。

@Component注解

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface Component {
    
}
1
2
3
4
5

Main方法

public class Main {
    public static void main(String[] args) throws IOException, ClassNotFoundException {

        Class<Main> mainClass = Main.class;
        //拿到当前类所在的路径
        //file:/D:/IdeaProjects/myspring/out/production/myspring/
        String s = mainClass.getResource("/").toString();

        //拿到当前类所在包名
        //com.dram
        String packageName = mainClass.getPackage().getName();
        String replace = packageName.replace('.', '/');
        String path = s + replace;
        System.out.println("要扫描的路径 = " + path);

        //加载文件
        ClassLoader classLoader = mainClass.getClassLoader();
        File[] files = new File(path.substring(6)).listFiles();
        //存放类文件名
        ArrayList<String> classes = new ArrayList<>();
        assert files != null;
        for (File file : files) {
            //把.class结尾的文件名放到classes
            if (file.getName().endsWith(".class")) {
                if (file.getName().equals(Main.class.getSimpleName() + ".class")) {
                    continue;
                }
                classes.add(file.getName());
            }
        }

        //存放类对象
        ArrayList<Class> classArrayList = new ArrayList<>();

        //遍历文件名
        for (String aClass : classes) {
            //拼接成完整包名,加载成类对象
            String s1 = aClass.split("\\.")[0];
            classArrayList.add(Class.forName(packageName + "." + s1, false, classLoader));
        }
        System.out.println("所有的类对象" + classArrayList);

        //存放所有带有component注解的类对象
        ArrayList<Class> componentClasses = new ArrayList<>();
        //遍历加载好的类对象
        for (Class aClass : classArrayList) {
            //拿到单个类的注解集合
            Annotation[] annotations = aClass.getAnnotations();
            if (annotations.length == 0) {
                //没有注解
                continue;
            } else {
                //遍历注解集合
                for (Annotation annotation : annotations) {
                    //判断单个注解是否是我们定义的Component注解
                    Class<? extends Annotation> x = annotation.annotationType();
                    if (x.equals(Component.class)) {
                        componentClasses.add(aClass);
                    }
                }
            }
        }

        System.out.println("包含Component注解的类:" + componentClasses);

        //TODO 把对象实例化到IOC容器中

    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69

运行结果:

要扫描的路径 = file:/D:/IdeaProjects/myspring/out/production/myspring/com/dram 所有的类对象[interface com.dram.Component, class com.dram.UserController, class com.dram.UserService] 包含Component注解的类:[class com.dram.UserController, class com.dram.UserService]


#注解扫描#代码片段
上次更新: 10/23/2024
使用Hibernate-validator的Notnull等注解校验参数并使用全局异常处理
SpringBoot+Swagger,快速构建REST API文档

← 使用Hibernate-validator的Notnull等注解校验参数并使用全局异常处理 SpringBoot+Swagger,快速构建REST API文档→

最近更新
01
mosquito配置ws协议
10-23
02
Pip包的离线下载和安装
10-23
03
stable diffusion 相关收藏
02-24
更多文章>
Theme by Vdoing | Copyright © 2019-2024 Dra-M
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式