• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

Scanning for Beans with particular annotation

 
Ranch Hand
Posts: 162
1
Android Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How to Scan Beans with particular Annotation. For instance I would like to know all the beans  with @Command Annotation on them.
 
Ranch Hand
Posts: 954
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Quick google gives me several option. One of is this.

You can check that as well.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What Tushar posted probably works, but it looks more complicated than necessary. The Spring ApplicationContext has a method to do exactly what you want: getBeansWithAnnotation.

It returns a Map in which the keys are bean names and the values are bean instances.

For example:

 
Tushar Goel
Ranch Hand
Posts: 954
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Jesper.. I was unaware of that..
 
Sangel Kapoor
Ranch Hand
Posts: 162
1
Android Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jesper de Jong wrote:What Tushar posted probably works, but it looks more complicated than necessary. The Spring ApplicationContext has a method to do exactly what you want: getBeansWithAnnotation.

It returns a Map in which the keys are bean names and the values are bean instances.

For example:



Thanks a bunch, I have a follow up question , We also have CommandHandlers which handles particular Command.
I would like to annotate method in class as follows



There is another class Router which will route Command to its handler and invoke handle method.

Class Router is as follows


}


I want Spring to populate above map automatically when Spring Context is loaded. How to achieve this , given now I know how to find classes with @Command Annotations.

 
Jesper de Jong
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I found this an interesting problem, so I played around with it a bit. Here is my solution. I modified some of your original ideas.

Instead of having a @Command annotation, I made an enum to define the different kinds of commands:

Then I created two annotations: @CommandHandlers, which is supposed to be used on a class that contains handler methods, and @CommandHandler, which is supposed to be used on the methods themselves:


An example of a container with handler methods then looks like this:

Then the Router class. It contains an initialize() method with a @PostConstruct annotation, so that Spring will call this method when it creates the Router bean. The initialize() method is going to look for all Spring beans with the @CommandHandlers annotation. For each one that it finds, it's going to look at all the methods that have a @CommandHandler annotation, and it registers a handler.

And an example of how to call the router to route a command:

reply
    Bookmark Topic Watch Topic
  • New Topic