Announcement Announcement Module
No announcement yet.
How to add Qualifier to Singleton Bean Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to add Qualifier to Singleton Bean

    I would like to register an existing object as a bean and add a qualifier to that singleton bean. I know, I can register an existing object with registerSingleton. However, I cannot add a qualifier. i.e. I would like to do the equivalent of

      <bean class="example.SimpleMovieCatalog">
          <qualifier value="main"/>
    with a singleton bean. Then I would like to inject the bean into another bean (nothing special here):

      public void setMovieCatalog(@Qualifier("main") MovieCatalog catalog) { ... }

    Is this possible at all?

    To give you some context. Here is what I am trying to achieve. I build an application context that scans (ClassPathBeanDefinitionScanner) for classes annotated with a particular annotation. These classes depend on objects, some of which have been created programmatically (hence the question above). For each particular qualifier annotation value, there is a registered singleton bean matching the qualifier annotation. So I need to attach that qualifier annotation to the singeltons I try to register.

      public class ActionMovieStore {
        @Inject @Qualifier("action")
        MovieCatalog movieCatalog;
    MovieCatalog in this case is a provided singleton object created programmatically. Let's say there is just a single implementation of MovieCatalog. The different instances are created by loading data from a database. So there is no way to add the Qualifier annotation on the single implementation class. I need to specificy it when definining the bean.