扫描类路径以查找具有自定义注释的类
例如,如果我有注释 @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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
是的,请查看 Scannotation 库。
另请参阅以下博客文章,记录了 Scannotation 的使用。
基本示例:
您的注释需要具有“运行时”保留,以便它们在运行时在
.class
文件中可用。Yes, check out the Scannotation library.
Also, see the following blog post that documents use of Scannotation.
Basic example:
Your annotations will need to have 'runtime' retention so that they are available in the
.class
file at runtime.你可以尝试我的库 FastClasspathScanner:
You could try my library FastClasspathScanner:
我实际上推荐另一种方法,比所有其他方法都好(因为它们都使用类路径扫描,这很慢)。它称为 ClassIndex,它索引带注释的类:
https://github.com/atteo/classindex
I'd actually recommend another approach, better than all others (since they all use classpath scanning, which is slow). It's called ClassIndex and it INDEXES annotated classes:
https://github.com/atteo/classindex
你可以尝试 corn-cps
示例:
将下面的依赖项放入你的 pom.xml 中
You can try corn-cps
Example:
put the dependecy below in your pom.xml