简体   繁体   English

如何通过Java中的Reflection API加速类扫描?

[英]How to speed up the class scan via Reflection API in Java?

I use org.reflections library to scan ClassPath and get classes. 我使用org.reflections库扫描ClassPath并获取类。 Here is my code: 这是我的代码:

Reflections ref = new Reflections();
Set<Class<? extends Service>> classes = new HashSet<>();
for (Class<? extends Service> subType : ref.getSubTypesOf(Service.class)) {
    if (!Modifier.isAbstract(subType.getModifiers())) {
        classes.add(subType);
    }
}

But I faced a problem. 但是我遇到了一个问题。 It takes too much time. 这需要太多时间。 At the same time I can not set a package 同时我无法设置包裹

new Reflections("my.pack");

because I want to save an ability to add Service classes in the future. 因为我想保留将来添加Service类的功能。 How can I accelerate this process? 我如何加快这个过程? Is it possible to exclude rt.jar packs? 是否可以排除rt.jar包?

Use FilterBuilder to exclude java root package at least. 使用FilterBuilder至少排除java根包。

And it may help to specify SubTypesScanner as that's what you're doing. 这可能有助于指定SubTypesScanner因为这就是您正在执行的操作。

Reflections ref = new Reflections(new SubTypesScanner(),
                                  new FilterBuilder().excludePackage("java"));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM