[英]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.