Reflections的使用

knight2023-09-04 17:10java/libraryjava

仓库地址

  • reflections: https://github.com/ronmamo/reflections

        <dependency>
          <groupId>org.reflections</groupId>
          <artifactId>reflections</artifactId>
        </dependency>
    
如何扫描指定包下包含指定注解的类?

假设扫描@Mapper

Reflections reflections = new Reflections(
    new ConfigurationBuilder().setParallel(true).forPackages("packageName")
        .setScanners(Scanners.TypesAnnotated));
Set<Class<?>> classes = reflections.getTypesAnnotatedWith(Mapper.class);
如何扫描指定包下包含指定注解的方法?

假设扫描@Transaction

Reflections reflections = new Reflections(
    new ConfigurationBuilder().setParallel(true).forPackages("packageName")
        .setScanners(Scanners.MethodsAnnotated));
Set<Method> methods = reflections.getMethodsAnnotatedWith(Transaction.class);
如何扫描指定包下继承/实现某个类的子类?

假设扫描 ServiceImpl 子类

Reflections reflections = new Reflections(
    new ConfigurationBuilder().setParallel(true).forPackages("packageName")
        .setScanners(Scanners.SubTypes));
Set<Class<? extends ServiceImpl>> classes = reflections.getSubTypesOf(ServiceImpl.class);
Last Updated 8/16/2024, 6:38:40 AM