当前位置: 首页 > 工具软件 > Reflections > 使用案例 >

Reflections类实现接口和注解的扫描

公冶高峯
2023-12-01

pom.xml

    <dependency>
            <groupId>org.reflections</groupId>
            <artifactId>reflections</artifactId>
            <version>0.9.10</version>
        </dependency>

加了某个注解和实现某类接口的扫描

package org.example;

import org.reflections.Reflections;

import java.util.Set;

/**
 * @author jianan
 * @date 2021/7/2 20:02:19
 */
@TestAnno
public class TestReflections extends Test1{
	public static void main(String[] args) throws Exception {
		Reflections reflections = new Reflections("org");
		//扫描注解
//		Set<Class<?>> subOfObjSet = reflections.getTypesAnnotatedWith(TestAnno.class);

		//扫描子类
		Set<Class<? extends Test1>> subTypesOf = reflections.getSubTypesOf(Test1.class);
		for(Class<? extends Test1> v : subTypesOf){
			Test1 test1 = v.getDeclaredConstructor().newInstance();
		}
		System.out.println(subTypesOf);
	}
}

/*
➜  target java -jar mj.jar
[class org.example.TestReflections]
*/

 类似资料: