Reflections反射包在springboot jar环境下扫描不到class排查过程
创始人
2024-05-24 00:00:05
0

需求:
要实现指定pkg(如com.qiqitrue.test.pojo)扫描包下所有class类信息:使用代码如下
pkg扫描包:如com.qiqitrue.test.pojo
使用的版本:0.10.2(截至目前是最新版)发现只能在idea编译期间可以获取得到(也就是在开发阶段)。当打成了jar包之后,获取不到。
先找到了
https://github.com/CESNET/perun/pull/3836
https://github.com/ronmamo/reflections
这个。改成了如下:

Reflections reflections = new Reflections(new ConfigurationBuilder().forPackage("com.my.project")// 先注释这个,一会说原因//.filterInputsBy(new FilterBuilder().includePackage("com.my.project")));

改了这个之后虽然能获取到。但实际上,所谓定义package,只是为classloader找路径而已。并没有指定传得包,也就是说,虽然我指定了package,但是,实际扫描得路径是target\classs(那范围可大了)。打成jar包之后获取,也是可以。貌似解决了。但如果我程序中有多次扫描不同的包下类上标注相同注解的类,那将会出现重叠的情况。
比如这样:

String pkg = "com.qiqitrue.pojo.a";//@1
Reflections reflections = new Reflections(pkg);
Set> resourceTypeClasses = reflections.getTypesAnnotatedWith(MyAnno.class);
log.info("扫描出类型{}资源类实体数:{}", subPathPackage, resourceTypeClasses.size());String pkg = "com.qiqitrue.pojo.b";//@2package com.qiqitrue.pojo.a;
@MyAnno
class A {
}package com.qiqitrue.pojo.b;
@MyAnno
class B {
}a和b包都有标注相同的注解情况下,那将产生重复。虽然能通过包名进行过滤,但是且不够优雅。

为了需要,就需要把filterInputsBy(new FilterBuilder().includePackage(“com.my.project”))这个也加上,发现就可以解决上面说的问题。

但是…打包jar之后运行,获取不到。最终解决是回退到版本:0.9.11
https://github.com/ronmamo/reflections/issues/373

虽然切换个版本就能解决,但是不知道为何解决了,继续刨根问底看一下,
对比两个版本的代码实现,发现0.10.X版本有了重大更新。跟踪源码
在这里插入图片描述
重点是scan()方法

protected Map>> scan() {long start = System.currentTimeMillis();Map>> collect = configuration.getScanners().stream().map(Scanner::index).distinct().collect(Collectors.toMap(s -> s, s -> Collections.synchronizedSet(new HashSet<>())));Set urls = configuration.getUrls();(configuration.isParallel() ? urls.stream().parallel() : urls.stream()).forEach(url -> {Vfs.Dir dir = null;try {dir = Vfs.fromURL(url);//@1 这是重点,进入这个方法看一下for (Vfs.File file : dir.getFiles()) {if (doFilter(file, configuration.getInputsFilter())) {ClassFile classFile = null;for (Scanner scanner : configuration.getScanners()) {try {if (doFilter(file, scanner::acceptsInput)) {List> entries = scanner.scan(file);if (entries == null) {if (classFile == null) classFile = getClassFile(file);entries = scanner.scan(classFile);}if (entries != null) collect.get(scanner.index()).addAll(entries);}} catch (Exception e) {if (log != null) log.trace("could not scan file {} with scanner {}", file.getRelativePath(), scanner.getClass().getSimpleName(), e);}}}}} catch (Exception e) {if (log != null) log.warn("could not create Vfs.Dir from url. ignoring the exception and continuing", e);} finally {if (dir != null) dir.close();}});// mergeMap>> storeMap =collect.entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey,entry -> entry.getValue().stream().filter(e -> e.getKey() != null).collect(Collectors.groupingBy(Map.Entry::getKey,HashMap::new,Collectors.mapping(Map.Entry::getValue, Collectors.toSet())))));if (log != null) {int keys = 0, values = 0;for (Map> map : storeMap.values()) {keys += map.size();values += map.values().stream().mapToLong(Set::size).sum();}log.info(format("Reflections took %d ms to scan %d urls, producing %d keys and %d values", System.currentTimeMillis() - start, urls.size(), keys, values));}return storeMap;
}

在这里插入图片描述
重点就是这个defaultUrlTypes。
在这里插入图片描述
在这里插入图片描述
主要涉及到了这个正则:
在这里插入图片描述
进行了取反,即不支持springboot打包jar的方式。

再看下0.9.11版本的
在这里插入图片描述
对比之下,就是没有新版取反的条件。

相关内容

热门资讯

【NI Multisim 14...   目录 序言 一、工具栏 🍊1.“标准”工具栏 🍊 2.视图工具...
银河麒麟V10SP1高级服务器... 银河麒麟高级服务器操作系统简介: 银河麒麟高级服务器操作系统V10是针对企业级关键业务...
不能访问光猫的的管理页面 光猫是现代家庭宽带网络的重要组成部分,它可以提供高速稳定的网络连接。但是,有时候我们会遇到不能访问光...
AWSECS:访问外部网络时出... 如果您在AWS ECS中部署了应用程序,并且该应用程序需要访问外部网络,但是无法正常访问,可能是因为...
Android|无法访问或保存... 这个问题可能是由于权限设置不正确导致的。您需要在应用程序清单文件中添加以下代码来请求适当的权限:此外...
北信源内网安全管理卸载 北信源内网安全管理是一款网络安全管理软件,主要用于保护内网安全。在日常使用过程中,卸载该软件是一种常...
AWSElasticBeans... 在Dockerfile中手动配置nginx反向代理。例如,在Dockerfile中添加以下代码:FR...
AsusVivobook无法开... 首先,我们可以尝试重置BIOS(Basic Input/Output System)来解决这个问题。...
ASM贪吃蛇游戏-解决错误的问... 要解决ASM贪吃蛇游戏中的错误问题,你可以按照以下步骤进行:首先,确定错误的具体表现和问题所在。在贪...
月入8000+的steam搬砖... 大家好,我是阿阳 今天要给大家介绍的是 steam 游戏搬砖项目,目前...