问题描述
我想在 Java 中实现一个基于注解的初始化机制.具体来说,我定义了一个注释:
I want to implement an intialization mechanism that is annotation-based in Java. Specifically, I have an annotation I've defined:
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface Initialization {
/**
* If the eager initialization flag is set to <code>true</code> then the
* initialized class will be initialized the first time it is created.
* Otherwise, it will be initialized the first time it is used.
*
* @return <code>true</code> if the initialization method should be called
* eagerly
*/
boolean eager() default false;
}
另外,我有一个界面:
public interface SomeKindOfBasicInterface {}
我想在我的类路径上找到 SomeKindOfBasicInterface
类的每个实现,在方法上有 @Initialization
注释.我正在查看 Spring 的 MetaDataReader
工具,它看起来是在我执行此操作时延迟加载其他 SomeKindOfBasicInterface
实现的最佳方法......但我不是确定如何像我描述的那样进行搜索.有什么提示吗?
I want to find every implementation of the SomeKindOfBasicInterface
class on my classpath that has the @Initialization
annotation on a method. I'm looking at Spring's MetaDataReader
tools, which look like the best way to defer loading the other SomeKindOfBasicInterface
implementations while I'm doing this... but I'm not sure how to do a search like I'm describing. Any tips?
推荐答案
你可以使用 Reflections,这是一个 Java 运行时元数据分析工具.我已经用它来获取给定类型的所有子类型,但它也可以处理您的情况.
You could use Reflections, which is a Java runtime metadata analysis tool. I've used it to get all subtypes of a given type, but it can handle your case as well.
这篇关于如何在类路径上找到具有特定方法注释的所有类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!