简体   繁体   English

如何找到带有特定注释的所有Java对象(而非类)?

[英]How do I find all Java objects (not classes) with a particular annotation?

I am designing a system to update member variables in objects automatically by marking them with some annotation. 我正在设计一个系统,通过使用一些注释标记对象中的成员变量来自动更新它们。 Some other background thread will then update the objects. 然后,其他一些后台线程将更新对象。

To do this, the runtime component doing the updating needs to know all Java objects that contain this annotation. 为此,执行更新的运行时组件需要知道包含此批注的所有Java对象。 I can see that there are methods to find classes with an annotation type, but going from classes to instances of the classes is a lot harder. 我可以看到有找到带有注释类型的类的方法,但是从类到类的实例要困难得多。 I want to avoid explicitly "registering" the object in the constructor, although I could do that as a last resort. 我想避免在构造函数中显式“注册”对象,尽管我可以作为最后的选择。

Any suggestions as to how this might be achieved? 关于如何实现这一目标的任何建议?

You can use some kind of HashMap cache to store objects for update. 您可以使用某种HashMap缓存来存储对象以进行更新。

1) write annotation @Updatable to mark objects updatable 1)编写注释@Updatable以将对象标记为可更新

2) write HashMap that will contain all the objects to update 2)编写将包含所有要更新对象的HashMap

3) initialize this HashMap on time of app initialization by lookup the objects with @Updatable If you are using Spring that you can write custom BeanPostProcessor for that purposes. 3)在应用程序初始化时通过使用@Updatable查找对象来初始化此HashMap。如果使用的是Spring,则可以为此编写自定义BeanPostProcessor

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何查找扩展特定库的项目中的所有类? - How do I find all classes in a project that extend a particular library? 如何做注释,将所有该类型的类添加到列表中 - How do I make an annotation, that adds all classes of that type to a list 如何扫描在运行时加载的java类的特定注释作为字节码? - How to scan for a particular annotation of java classes loaded at runtime as a bytecode? Java字节/源代码分析-如何以编程方式找到使用另一个类的所有类/方法? - Java byte/source code analysis - how do I find all the classes/methods that use another class - programmatically? 在运行时阅读装饰有特定注释的所有类 - Read all classes decorated with a particular annotation at runtime 如何找到类路径上具有特定方法注释的所有类? - How can I find all classes on the classpath that have a specific method annotation? 如何用我的注释查找所有类 Wildfly JavaEE - How to find all classes with my annotation | Wildfly JavaEE 如何从类路径中的 Java 包中读取所有类? - How do I read all classes from a Java package in the classpath? 如何以编程方式查找继承了 Java 中特定 class 的类的列表 - How to programmatically find list of classes that have inherited a particular class in Java 如何在不使用批注的情况下使用JAXB将Java对象转换为xml? - How do I convert java objects to xml using JAXB without using annotation?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM