本文主要是介绍Reflections类实现接口和注解的扫描,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
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]
*/
这篇关于Reflections类实现接口和注解的扫描的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!