我想创建自己的自定义注释。我的框架是独立的Java应用程序。当某人注释他的POJO级A类“隐藏”代码时,将触发方法。
例如,今天在Java EE中,我们有 @messagedriven
注释。
当您用 @messagedriven
and and ant章时,并且实现MessageListener接口时,有一个背后的代码将触发 onMessage(Message MSG)
。当消息从队列/主题到达时。
我如何创建注释( @mymessagedriven
),该注释可以添加到POJO中,并实现 mycustMustmassageListener
。
我想要的结果是(我的)“隐藏”代码的触发器,它将触发一种实现界面的方法(完全适用于我在下面写的示例)。
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.
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.
How do I create an annotation (@MyMessageDriven
) which could be added to a pojo and also implement MyCustomMessageListener
.
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).
发布评论
评论(2)
我建议阅读直到作者记得他可以访问Spring的组件扫描功能的地步。
最初的问题是扫描类路径以找到具有自定义注释的类。完成此操作后,您的独立应用程序中的对象使用
object.getClass()。getAnnotations()
,您可以注入侦听器或自定义行为,您需要添加到持有自定义注释的对象中。假设您有以下自定义注释:
您在应用程序中使用了一些类:
现在,在应用程序中的适当位置,您应该有一种方法来提供所有携带
myMessagedriven
的类别我认为您的应用程序中有一个点(1)您可以访问应用程序中的对象,或者(2)应用程序中的所有对象都有访问者或迭代器模式。因此,在某些时候,我认为我们将所有目标对象都是
对象
:另一方面,当对象具有具有的对象具有具有的对象时
MyMessagedriven
:此答案是根据博客条目量身定制的,因此请参阅博客以获取库的配置。而且,最后但并非最不重要的一点是,这是解决该问题的示例代码,可以重构为图案兼容和优雅的样式。
Update :有一个要求目标对象应意识到自己的听众。因此,我建议采用以下方法。让我们有一个接口
myMessageListeneraware
:现在,目标对象应该实现上述接口:
有了这个,方法
InvokeThemessageListener
,尽管,强烈建议您阅读有关访问者或策略模式。在我看来,您的目标是您需要某些对象对应用程序中的常见对象/事件进行反应/ACT/过程,但是每个 都会使用他们自己的解释/算法/实现。
I recommend to read this blog entry (snapshot on archive.org) up to the point where the author remembers (s)he has access to Spring's component scan feature.
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:
And you use it some class in you application:
Now, in the appropriate location in your application, you should have a method to give out all classes carrying
MyMessageDriven
:Having this, I assume that there is a point in your application that either (1) you have access to the objects in your application, or (2) there is a visitor or iterator pattern on all the objects in the application. So, in some point, I assume that we have all targeted objects as
objects
:On the other hand, there should be some implementation of
MyMessageListener
that is available when the objects havingMyMessageDriven
are found: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.
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
:Now, the target objects should implement the above interface:
Having this, the method
invokeTheMessageListener
becomes like: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.
创建一个类似的注释:
您有一个可以应用这样的接口:
使用classloader加载上面的类并使用反射检查检查注释是主题。
如果存在,请使用加载实例执行它。
您可以将侦听器实现放入MyMessage类或实现MessageListener的其他类。
在这种情况下,需要为
message()
提供实现。但是,此类应该加载,更重要的是如何加载MyMessage类。
这是基于MyMessage类中存在的元数据。类似方式,在实时方案中,这也是它的工作原理。
注释是基于所提供的数据,做某事的类的元数据。
希望这对您有帮助。
create an annotation something like this:
And you have an interface that can apply annotation like this:
Load the above class using classloader and using reflections check the annotation is presrent.
if it is present, use loaded instance to execute it.
Listener implementation you can put in MyMessage class or some other class that implements MessageListener.
In this case, need to provide implementation for
message()
what it is going to do.But this class should be loaded and more important thing here is how your MyMessage class is loaded.
That is based on the meta data present in the MyMessage class.Similar way, in the real time scenario as well this is how it works.
Annotation is a metadata to a class that says based on the supplied data, do something.Had this metadata not present in the MyMessage class, you need not execute
message()
method.Hope this will help you.