扫描类路径以查找具有自定义注释的类

发布于 2024-12-03 06:36:56 字数 195 浏览 0 评论 0原文

例如,如果我有注释 @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技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

沫尐诺 2024-12-10 06:36:56

是的,请查看 Scannotation 库。

另请参阅以下博客文章,记录了 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());

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

Yes, check out the Scannotation library.

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

Basic example:

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

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

梦冥 2024-12-10 06:36:56

你可以尝试我的库 FastClasspathScanner

List<String> classNames = new FastClassPathScanner("my.own.app")
    .scan()
    .getNamesOfClassesWithAnnotation(MyOwnAnnotation.class);

You could try my library FastClasspathScanner:

List<String> classNames = new FastClassPathScanner("my.own.app")
    .scan()
    .getNamesOfClassesWithAnnotation(MyOwnAnnotation.class);
但可醉心 2024-12-10 06:36:56

我实际上推荐另一种方法,比所有其他方法都好(因为它们都使用类路径扫描,这很慢)。它称为 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

掩饰不了的爱 2024-12-10 06:36:56

你可以尝试 corn-cps

示例:

List<Class<?>> classes = CPScanner.scanClasses(new PackageNameFilter("net.sf.corn.cps.*"),new ClassFilter().appendAnnotation(SampleAnnotation.class));

将下面的依赖项放入你的 pom.xml 中

<dependency>
    <groupId>net.sf.corn</groupId>
    <artifactId>corn-cps</artifactId>
    <version>1.0.1</version>
</dependency>

You can try corn-cps

Example:

List<Class<?>> classes = CPScanner.scanClasses(new PackageNameFilter("net.sf.corn.cps.*"),new ClassFilter().appendAnnotation(SampleAnnotation.class));

put the dependecy below in your pom.xml

<dependency>
    <groupId>net.sf.corn</groupId>
    <artifactId>corn-cps</artifactId>
    <version>1.0.1</version>
</dependency>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文