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 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).
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.
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.