使用自定义注释扫描类路径以查找类 [英] Scan the classpath for classes with custom annotation

查看:85
本文介绍了使用自定义注释扫描类路径以查找类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,如果我有注释 @MyOwnAnnotation 并且在我的类路径中有这些类,那么我可以使用某种过滤器扫描类路径(例如。仅扫描包从 my.own.app。* 开始,获取带注释 @MyOwnAnnotation 的所有类的列表?我使用guice作为注入框架,我不使用Spring。

For example if I have annotation @MyOwnAnnotation and have these classes in my classpath, so that I could scan classpath possibly with some kind of filter (example. scan only packages starting with my.own.app.*) and get list of all classes with annotation @MyOwnAnnotation? I'm using guice as injection framework and I don't use Spring.

推荐答案

是的,看看 Scannotation 库。

另外,请参阅以下博客文章文档使用Scannotation

Also, see the following blog post that documents use of Scannotation.

基本示例:

URL[] urls = ClasspathUrlFinder.findClassPaths(); // scan java.class.path
AnnotationDB db = new AnnotationDB();
db.scanArchives(urls);
Set<String> entityClasses =
    db.getAnnotationIndex().get(MyOwnAnnotation.class.getName());

您的注释需要具有运行时保留,以便它们在<$ c $中可用c> .class 运行时的文件。

Your annotations will need to have 'runtime' retention so that they are available in the .class file at runtime.

这篇关于使用自定义注释扫描类路径以查找类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆