title: Use CDI to load Beans dynamically - EJB3
body: <p>I am using EJB3 in my application and I want to know can I <code>inject</code> beans dynamically at runtime based on some parameter ?</p>
  
  <p>Because I have a <code>@Local</code> interface which extends several beans which works in different manner. So at run time I want to load each bean depend on the logic.  So far I used usual <code>JNDI lookup</code> But I would like to do it with <code>@Inject</code>. Is there a way that I can inject the bean dynamically?</p>
  
  <p>Here is my existing <code>lookup</code> code. I pass the bean name as <code>jndi</code> here and build the instance dynamically.</p>
  
  <pre><code>IService bean = (IService) initialContext.lookup(jndi+"Bean/local"); 
  bean.initializeTimer(firstDate, period, request);
  </code></pre>
  
  <hr>
  
  <h2>UPDATE</h2>
  
  <p>I have three beans for the moment. <br> 
  first bean is SingleServiceBean</p>
  
  <pre><code> @Stateless
   public class SingleServiceBean implements IService{
  
      @Override
      public void doSomething (){
       log.debug("inside do something");
      }
  
      private final Log log = LogFactory.getLog(SingleServiceBean.class.getName());
   }
  </code></pre>
  
  <p>Second bean is PeriodicService</p>
  
  <pre><code> @Stateless
   public class PeriodicServiceBean implements IService{
  
      @Override
      public void doSomething (){
       log.debug("inside do something");
      }
  
      private final Log log = LogFactory.getLog(PeriodicServiceBean.class.getName());
   }
  </code></pre>
  
  <p>Third bean</p>
  
  <pre><code> @Stateless
   public class AsyncServiceBean implements IService{
  
      @Override
      public void doSomething (){
       log.debug("inside do something");
      }
  
      private final Log log = LogFactory.getLog(AsyncServiceBean.class.getName());
   }
  </code></pre>
  
  <hr>
  
  <h2>2nd Update</h2>
  
  <p>Qualifier   :</p>
  
  <pre><code> @Qualifier
   @Target({ TYPE, METHOD, PARAMETER, FIELD })
   @Retention(RUNTIME)
   @Documented
   public @interface Services {
     String type(); 
  
   }
  </code></pre>
  
  <p>AnnotationLiteral</p>
  
  <pre><code> public class ServiceQualifier extends AnnotationLiteral&lt;Services&gt; implements Services{
  
  private static final long serialVersionUID = 6471734834552932687L;
  private String type;
  
  public String TypeQualifier(String t) {
        this.type = t;
        return type;
  }
  
  public String type() {
      return type;
  }
   }
  </code></pre>
  
  <p>And new bean class like below</p>
  
  <pre><code> @Services(type = "SingleService" )
   @Stateless
   public class SingleServiceBean implements IService{
   .....
   }
  </code></pre>
  
  <p>then I add the below lines in my bean caller class </p>
  
  <pre><code> @Inject
  private Instance&lt;IService&gt; iServiceInstance;
  
  public void someMethod() {
  
  // this line gives me error by red underline in .select(...)
  IService service = iServiceInstance.select(new ServiceQualifier().TypeQualifier("SingleService")).get();
  }
  </code></pre>
  
  <p>Error I get in eclipse : "The method select(Annotation...) in the type Instance is not applicable for the arguments (String)"</p>