Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7803779
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T01:41:42+00:00 2026-06-02T01:41:42+00:00

I would like to create my own custom annotation. My framework is stand alone

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T01:41:44+00:00Added an answer on June 2, 2026 at 1:41 am

    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:

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

    And you use it some class in you application:

    @MyMessageDriven
    public class MyObject {}
    

    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;
    }
    

    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:

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

    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);
    }
    

    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:

    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;
      }
    
    }
    

    Having this, the method invokeTheMessageListener becomes like:

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

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to create my own custom NSMutableArray of my custom objects: @interface
I have my own custom configuration sections but would like to create a new
I would like to create a subsite that has its own custom Permission Levels.
I would like create my own collection that has all the attributes of python
I would like to create my own data format for an iPhone app. The
I'd like to create a map with an int and my own custom class.
hi i would like to create a custom calendar, this calendar will have custom
I'd like to create my own custom HTTP requests. The WebClient class is very
I would like to create my own subclass of UIImage that would allow me
I would like to create a custom CMS within Codeigniter, and I need a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.