如何创建与code后面的自定义批注 [英] How to create custom annotation with code behind

查看:176
本文介绍了如何创建与code后面的自定义批注的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建自己的自定义注释。我的框架是单机Java应用程序。当有人诠释他的POJO类隐藏code后面将触发的方法。

I would like to create my own custom annotation. My framework is Stand alone java application. When someone annotate his pojo class a "hidden" code behind will trigger methods.

例如,今天在Java EE中,我们有 @MessageDriven 注释。
而当你用 @MessageDriven 注释你的类,另外实施MessageListener接口有一个背后code将触发的onMessage(消息MSG) 。当消息从队列/主题到达。

For example, today in Java EE we have @MessageDriven annotation. And when you annotate your class with @MessageDriven and in addition implement MessageListener Interface there is a behind code that will trigger onMessage(Message msg). when a message arrives from a Queue/Topic.

如何创建注释( @MyMessageDriven ),它可以被添加到一个POJO,并落实 MyCustomMessageListener

How Do I create annotation(@MyMessageDriven) which could be added to a pojo and also implement MyCustomMessageListener.

这是我的愿望是隐藏code(矿)的触发,这将触发一个实现的接口(正是因为它与我在下面写了样品的工作)。

The result which I desire is a trigger of "hidden" code (of mine) which will trigger a method of an implemented interface (exactly as it works with the sample i Wrote below).

推荐答案

我建议按照此博客条目​​到这里笔者记得点他(她)可以访问Spring的组件扫描功能。

I propose to follow this blog entry up to the point where the author remembers (s)he has access to Spring's component scan feature.

最初的问题是扫描类路径中找到与自定义注解类。一旦做到这一点,你通过它用在你的独立应用程序中的对象<一个href=\"http://docs.oracle.com/javase/6/docs/api/java/lang/Class.html#getAnnotations%28%29\"><$c$c>object.getClass().getAnnotations(),那么你可以注入你需要添加的对象持有定制注释听众或自定义行为。

The initial issue is to scan the class path to find classes with the custom annotation. Once this is done, you have the objects in your standalone application through which using object.getClass().getAnnotations(), you can then inject the listeners or custom behavior you need to add to the objects holding the custom annotations.

让我们假设你有以下自定义注释:

Let's say you have the following custom annotation:

@Target({ ElementType.TYPE })
@Retention(RetentionPolicy.RUNTIME)
public @interface MyMessageDriven {}

和你使用它的一些类应用你:

And you use it some class in you application:

@MyMessageDriven
public class MyObject {}

现在,在您的应用程序的适当位置,你应该有一个方法来给出携带 MyMessageDriven 所有类:

Now, in the appropriate location in your application, you should have a method to give out all classes carrying MyMessageDriven:

Set<Class<?>> findAllMessageDrivenClasses() {
  final StopWatch sw = new StopWatch();
  sw.start();
  final Reflections reflections = new Reflections("org.projectx", new TypeAnnotationsScanner());
  Set<Class<?>> allMessageDrivens = reflections.getTypesAnnotatedWith(MyMessageDriven.class); // NOTE HERE
  sw.stop();
  return allMessageDrivens;
}

到这一点,我认为有一个在您的应用程序的一个点,要么(1)您可以访问的对象在你的应用程序,或者(2)存在于应用程序中的所有对象访问者或迭代器模式。所以,在某些时候,我认为我们拥有所有的目标对象为对象

Set<Class<?>> msgDrivenClasses = findAllMessageDrivenClasses();
for (Object o : objects) {
  if (msgDrivenClasses.contains(o.getClass()) {
    invokeTheMessageListener(o);
  }
}

在另一方面,应该有 MyMessageListener 的一些实现可用,当物体具有 MyMessageDriven 是发现:

On the other hand, there should be some implementation of MyMessageListener that is available when the objects having MyMessageDriven are found:

void invokeTheMessageListener(Object o) {
  theMessageListener.onMessage(o);
}

这个答案是从博客条目,请参阅博客图书馆的配置定制。最后但并非最不重要的,这是一个简单的code的问题,它可以重构更格局兼容的,优雅的风格。

This answer is tailored from the blog entry so please refer to the blog for configuration of libraries. And, last but not least, this is a sample code for the problem and it can be refactored to more pattern-compatible and elegant style.

更新:有认为的针对性的要求的对象应该知道自己的听众。所以,我建议以下办法。让我们有一个接​​口 MyMessageListenerAware

Update: There is a requirement that the targeted objects should be aware of their own listeners. So, I'd suggest the following approach. Let's have an interface MyMessageListenerAware:

interface MyMessageListenerAware {
  MyMessageListener getMyMessageListener();
}

// and this is the original MyMessageListener
interface MyMessageListener {
  void onMessage(Object o);
}

现在,目标对象应该实现上述接口:

Now, the target objects should implement the above interface:

class MySampleObject implements MyMessageListenerAware {

  public MyMesssageListener getMyMessageLisener() {
    return mySampleObjectImplementationOfMyMessageListener;
  }

}

到这一点,方法 invokeTheMessageListener 变得像:

void invokeMessageListener(Object o) {
  if (o instance MyMessageListenerAware) {
    MyMessageListener l = ((MyMessageListenerAware) o).getMyMessageListener();
    l.onMessage(o);
  }
}

虽然,我强烈建议阅读有关游客或的Strategy 格局。你的目标是尽我看来,像你需要特定对象的反应/动作/过程中一个共同的对象/事件中的应用程序,但的每个的用自己的跨pretation /算法/实施

Although, I strongly recommend reading about Visitor or Strategy pattern. What you aim to do seems to me like you need certain objects react/act/process to a common object/event in the application but each with their own interpretation/algorithm/implementation.

这篇关于如何创建与code后面的自定义批注的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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