我想在Java中实现一个基于注释的初始化机制。具体来说,我定义了一个注释:
@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
方法上的注释。我在看春天的MetaDataReader
工具,这看起来是推迟加载其他工具的最佳方式SomeKindOfBasicInterface
我正在执行此操作时的实现...但我不确定如何进行我所描述的搜索。有小费吗?
你可以使用反思 https://github.com/ronmamo/reflections,这是一个Java运行时元数据分析工具。我用它来获取给定类型的所有子类型,但它也可以处理您的情况。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)