在Java中创建注释对象时得到通知 [英] Get notified when annotated objects are created in Java

查看:192
本文介绍了在Java中创建注释对象时得到通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义的Java注释: @DynamicField

I've got a custom Java annotation: @DynamicField

public class RESTEndpointInvoker {
  @DynamicField(key="httpTimeout")
  private long httpTimeout = 8000L;

  public void setHttpTimeout(long t){
    this.httpTimeout = t;
}

当有人更改对应于注解的字段文件或数据库中的值,调用与该物业所有实例的二传手,以反映新的价值。所以,我需要保持使用注释,在地图什么的所有实例的轨道,当变化发生在外部对其进行更新:

When someone changes a value in a file or database corresponding to the annotated field, invoke the setter for all instances with that property to reflect the new value. So I need to keep track of all instances that use the annotation, in a map or something, to update them when a change occurs externally:

Map<Key,List<Instance>>

注:我打算用某种形式的 WeakHashMap中的()来避免持有引用陈旧实例

Note: I intend to use some form of WeakHashMap() to avoid holding references to stale instances.

在理想情况下,我想通知创建一个实例的时候。

Ideally, I want to be notified the moment an instance is created.

我可以扫描类的类型的,然后的 HTTP ://stackoverflow.com/a/16468167/791406>搜索注释字段每一类(见下文),但我不知道如何跟踪活动实例

I can scan the classpath for class types, then search each class for annotated fields (see below), but I'm not sure how to track live instances.

  //get classes with annotation
  Reflections r = new Reflections("com.foo", new TypeAnnotationsScanner());
  Set<Class<?>> allAnnotated = r.getTypesAnnotatedWith(DynamicField.class);
  //identify fields
  for(Class<?> cls : allAnnotated){
    for(Field field : cls.getDeclaredFields()){
    Class type = field.getType();
    String name = field.getName();
    if(field.isAnnotationPresent(DynamicField.class)){
      ....
    }
  }
}

解决方案的Spring bean

如果这些实例是春天豆类,我能做到这一点,但我宁愿避免这种限制。

Solution for Spring beans

If those instances are spring beans, I can do it, but I'd rather avoid that limitation.

推荐答案

您需要创建这些对象时要留意了。有两种方法:AOP(或其他code修改),以及agentlib

You need to take notice when such objects are created. There are two approaches: AOP (or other code modification), and an agentlib.

使用AspectJ(或其他AOP系统),就可以得到在该构造这些类的所有对象的时间控制。要知道,以处理那些构造函数,你需要,你写,扫描类路径与注释类。

With AspectJ (or some other AOP system), you can get control at the time that all objects of these classes are constructed. To know which constructors to tackle, you'll need, as you write, to scan the classpath for classes with the annotation.

AspectJ的文档主要是描述了抓在编译时已知的东西,但我有理由相信,你可以锁存到的东西动态。如果没有,ASM或其他codeGEN工具可以应用之一;例如,见<一href=\"http://zeroturnaround.com/rebellabs/how-to-make-java-more-dynamic-with-runtime-$c$c-generation/\" rel=\"nofollow\">http://zeroturnaround.com/rebellabs/how-to-make-java-more-dynamic-with-runtime-$c$c-generation/对于在你家附近的东西的讨论。如果你控制类装载器,你可以通过拦截类加载并注意到那些与你的注释避免扫描。

AspectJ's doc mostly describes grabbing things known at compile time, but I'm reasonably sure that you can latch into things dynamically. If not, asm or one of the other codegen tools can be applied; see for example http://zeroturnaround.com/rebellabs/how-to-make-java-more-dynamic-with-runtime-code-generation/ for a discussion of something in your neighborhood. If you control the class loader, you could avoid scanning by intercepting class loads and noticing those with your annotation.

另一种方法是使用的配置文件系统;注册一个agentlib。这是相当侵入性,而不是可能是一个诱人的前景。

The other alternative is that used by profile systems; registering an agentlib. This is quite intrusive and not likely to be an attractive prospect.

这篇关于在Java中创建注释对象时得到通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆