1、
<dependency>
<groupId>org.reflections</groupId>
<artifactId>reflections</artifactId>
<version>0.9.12</version>
</dependency>
2、
package com.example.learn.demo.reflect;
import com.example.learn.demo.DemoApplication;
import org.reflections.Reflections;
import org.springframework.boot.SpringApplication;
import java.util.Set;
public class TestOne {
public static void main(String[] args) {
// 实例化Reflections,并指定要扫描的包名
Reflections reflections = new Reflections("com.example.learn.demo");
// 获取某个类的所有子类
Set<Class<? extends SomeType>> subTypes = reflections.getSubTypesOf(SomeType.class);
subTypes.forEach(i-> System.out.println(i.getSimpleName()));
// 获取包含某个注解的所有类
Set<Class<?>> annotated = reflections.getTypesAnnotatedWith(SomeAnnotation.class);
annotated.forEach(i-> System.out.println(i.getSimpleName()));
}
}
3、
package com.example.learn.demo.reflect;
public class SomeType {
}
4、
package com.example.learn.demo.reflect;
@SomeAnnotation
public class SomeTypeOne extends SomeType {
}
5、
package com.example.learn.demo.reflect;
public @interface SomeAnnotation {
}