Java反射框架Reflections示例詳解
MAVEN 坐標(biāo)
<dependency> <groupId>org.reflections</groupId> <artifactId>reflections</artifactId> <version>0.9.10</version> </dependency>
Reflections 的作用
Reflections通過(guò)掃描classpath,索引元數(shù)據(jù),并且允許在運(yùn)行時(shí)查詢這些元數(shù)據(jù)。
- 獲取某個(gè)類型的所有子類;比如,有一個(gè)父類是TestInterface,可以獲取到TestInterface的所有子類。
- 獲取某個(gè)注解的所有類型/字段變量,支持注解參數(shù)匹配。
- 使用正則表達(dá)式獲取所有匹配的資源文件
- 獲取特定簽名方法。
使用Reflections可以很輕松的獲取以下元數(shù)據(jù)信息:
項(xiàng)目中使用:
public class ReflectionTest {
public static void main(String[] args) {
// 掃包
Reflections reflections = new Reflections(new ConfigurationBuilder()
.forPackages("com.boothsun.reflections") // 指定路徑URL
.addScanners(new SubTypesScanner()) // 添加子類掃描工具
.addScanners(new FieldAnnotationsScanner()) // 添加 屬性注解掃描工具
.addScanners(new MethodAnnotationsScanner() ) // 添加 方法注解掃描工具
.addScanners(new MethodParameterScanner() ) // 添加方法參數(shù)掃描工具
);
// 反射出子類
Set<Class<? extends ISayHello>> set = reflections.getSubTypesOf( ISayHello.class ) ;
System.out.println("getSubTypesOf:" + set);
// 反射出帶有指定注解的類
Set<Class<?>> ss = reflections.getTypesAnnotatedWith( MyAnnotation.class );
System.out.println("getTypesAnnotatedWith:" + ss);
// 獲取帶有特定注解對(duì)應(yīng)的方法
Set<Method> methods = reflections.getMethodsAnnotatedWith( MyMethodAnnotation.class ) ;
System.out.println("getMethodsAnnotatedWith:" + methods);
// 獲取帶有特定注解對(duì)應(yīng)的字段
Set<Field> fields = reflections.getFieldsAnnotatedWith( Autowired.class ) ;
System.out.println("getFieldsAnnotatedWith:" + fields);
// 獲取特定參數(shù)對(duì)應(yīng)的方法
Set<Method> someMethods = reflections.getMethodsMatchParams(long.class, int.class);
System.out.println("getMethodsMatchParams:" + someMethods);
Set<Method> voidMethods = reflections.getMethodsReturn(void.class);
System.out.println( "getMethodsReturn:" + voidMethods);
Set<Method> pathParamMethods =reflections.getMethodsWithAnyParamAnnotated( PathParam.class);
System.out.println("getMethodsWithAnyParamAnnotated:" + pathParamMethods);
}
}
具體也可以參見官方文檔:官方API
到此這篇關(guān)于Java反射框架Reflections示例詳解的文章就介紹到這了,更多相關(guān)Java反射框架Reflections內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
springboot+vue2+elementui實(shí)現(xiàn)時(shí)間段查詢方法
這篇文章主要介紹了springboot+vue2+elementui實(shí)現(xiàn)時(shí)間段查詢方法,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),感興趣的朋友跟隨小編一起看看吧2024-05-05
在webservice里調(diào)用耗時(shí)方法出錯(cuò)的解決方案
這篇文章主要介紹了在webservice里調(diào)用耗時(shí)方法出錯(cuò)的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-07-07
詳解Maven profile配置管理及激活profile的幾種方式
這篇文章主要介紹了詳解Maven profile配置管理及激活profile的幾種方式,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01
MyBatis動(dòng)態(tài)SQL標(biāo)簽用法實(shí)例詳解
本文通過(guò)實(shí)例代碼給大家介紹了MyBatis動(dòng)態(tài)SQL標(biāo)簽用法,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下吧2017-07-07
Java中的線程安全集合CopyOnWriteArrayList解析
這篇文章主要介紹了Java中的線程安全CopyOnWriteArrayList解析,CopyOnWriteArrayList是ArrayList的線程安全版本,從他的名字可以推測(cè),CopyOnWriteArrayList是在有寫操作的時(shí)候會(huì)copy一份數(shù)據(jù),然后寫完再設(shè)置成新的數(shù)據(jù),需要的朋友可以參考下2023-12-12

