我有一个使用的类 org.reflections.Reflections
在类路径中的类中获取注释。
当我在同一个项目中使用它时一切正常,但是当我将它导出为 JAR 并在不同的类中引用它时,我得到以下执行:
Exception in thread "main" org.reflections.ReflectionsException: Scanner TypeAnnotationsScanner was not configured
at org.reflections.Store.get(Store.java:39)
at org.reflections.Store.get(Store.java:61)
at org.reflections.Store.get(Store.java:46)
at org.reflections.Reflections.getTypesAnnotatedWith(Reflections.java:429)
Run Code Online (Sandbox Code Playgroud)
这是代码片段:
System.out.println("Package to Scan: "+getPackageName());
final Reflections reflections = new Reflections(getPackageName(), new TypeAnnotationsScanner());
final Set<Class<?>> handlerClasses = reflections.getTypesAnnotatedWith(HandlerMapping.class,true);
Run Code Online (Sandbox Code Playgroud)
我确实提供了一个TypeAnnotationsScanner
对象,但问题仍然存在。
注意:只有当上面的代码被称为 jar 时,它才起作用。(我使用 maven-assembly 插件创建了一个胖罐子)
任何指针?
Ale*_*ndr 10
也许不是人们想听到的答案类型,但reflections
版本0.9.12
包含此处描述的错误:https : //github.com/ronmamo/reflections/issues/273
此 PR 中已提供错误修正(尚未合并):https : //github.com/ronmamo/reflections/pull/278
version0.9.11
和version 之间的主要区别0.9.12
是在0.9.12
Guava 中删除了依赖项,以支持 Java 8 Streams API。
如果需要在没有Guava传递依赖的情况下包含这个依赖,可以看下一个项目:https : //github.com/aschoerk/reflections8
目前,最新可用的版本reflections8
是0.11.7
. 注意,reflections8
由于0.9.12
发布了,现在被认为是过时的,reflections
但他们的版本0.11.7
不包含这个错误,这个版本不依赖于 Guava。因此,一个可能的解决方案是切换到net.oneandone.reflections8:reflections8:0.11.7
.
使用反射库的 0.9.12,我发现需要为注释的每个 ElementType 注册扫描仪。
MyAnnotation 有这些 ElementTypes
@Target({ElementType.TYPE,ElementType.FIELD})
public @interface MyAnnotation {
Run Code Online (Sandbox Code Playgroud)
在我的反射代码中,我像这样构建它
Reflections reflections = new Reflections("my.package.*",
new SubTypesScanner(),
new TypeAnnotationsScanner(),
new FieldAnnotationsScanner());
//FieldAnnotationsScanner
Set<Field> fields = reflections.getFieldsAnnotatedWith(MyAnnotation.class);
System.out.println(fields);
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
8228 次 |
最近记录: |