需求:扫描指定注解标记的类,进行输出等处理
引入依赖
<dependency>
<groupId>org.reflections</groupId>
<artifactId>reflections</artifactId>
<version>0.9.11</version>
</dependency>扫描指定注解ProcesserAnnotation标记的类
String packageName="org.test";
//反射
Reflections ref = new Reflections(packageName);
// 获取扫描到的标记注解的集合
Set<Class<?>> set = ref.getTypesAnnotatedWith(ProcesserAnnotation.class);
System.out.println("--------start-------------");
List<MutablePair<Processer,ProcesserAnnotation>> pairList=new ArrayList<>();
for (Class<?> c : set) {
// 循环获取标记的注解
ProcesserAnnotation annotation = c.getAnnotation(ProcesserAnnotation.class);
if(annotation!=null){
// 打印注解中的内容
System.out.println(String.format("处理器名:%s,分组:%s",annotation.value(),annotation.group()));
try {
pairList.add(new MutablePair<>((Processer) c.newInstance(),annotation));
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}

















