title: Dynamically injecting instances via CDI
body: <p>In Java EE platform with the CDI there is ability to inject instances of POJO classes.
  In very simple way we need use @Inject annotation to inject a default instance of some Interface. We also may use qualifiers to inject concrete class to our field.
  But these solutions are rather static.</p>
  
  <p>I need some more dynamical model of injecting stuff.</p>
  
  <p>Let me introduce my problem:
  Let's say that we have interface Animal and three classes that implement that interface: Ant, Dog, Elephant.
  I would like to dynamically inject instance of one of these three classes, and it depends on some variable like string (animal name).
  In Java SE I'd do it like below:</p>
  
  <pre><code>Map&lt;String, Animal&gt; animalMap = new HashMap&lt;&gt;();
  animalMap.put("ant", new Ant());
  animalMap.put("dog", new Dog());
  animalMap.put("elephant", new Elephant());
  ...
  String animalName = ...;
  Animal animal = animalMap.get(animalMap);
  animal.doSomething();
  </code></pre>
  
  <p>So I need something like:</p>
  
  <pre><code>class AnimalManager {
     @Inject // ?
     private Animal animal; // ?
  
     public void run(String animalName) {
        // based on animalName get new instance of animal and run doSomething()
        ...
        animal.doSomething(); // if animalName is "ant" call the doSomething on Ant class
     }
  }
  </code></pre>
  
  <p>In all classes that implement Animal interface I need use variables with @EJB annotations.</p>
  
  <p>What the best and most proper way to do it in Java EE?</p>
  
  <p>EDIT:<br>
  OK based on response from Svetlin Zarev and hwellmann (thanks!) I have created this:</p>
  
  <p>On the beginning we will create Animal model:</p>
  
  <pre><code>@Qualifier
  @Retention(RetentionPolicy.RUNTIME)
  @Target({ElementType.FIELD, ElementType.TYPE})
  public @interface AnimalModel {
  
     Type value();
  
     enum Type { ANT, DOG, ELEPHANT }
  
  }
  </code></pre>
  
  <p>Let's create the Animal:</p>
  
  <pre><code>public interface Animal {
     public void eat(Object food);
  }
  </code></pre>
  
  <p>Next, concrete classes:</p>
  
  <pre><code>@AnimalModel(AnimalModel.Type.ANT)
  public class Ant implements Animal {
  
     @Override   
     public void eat(Object food) {
        ...
     }
  
  }
  
  @AnimalModel(AnimalModel.Type.DOG)
  public class Dog implements Animal {
  
     @Override   
     public void eat(Object food) {
        ...
     }
  
  }
  
  @AnimalModel(AnimalModel.Type.ELEPHANT)
  public class Elephant implements Animal {
  
     @Override   
     public void eat(Object food) {
        ...
     }
  
  }
  </code></pre>
  
  <p>Next, AnimalLiteral:</p>
  
  <pre><code>public class AnimalLiteral extends AnnotationLiteral&lt;AnimalModel&gt; implements AnimalModel {
  
     private static final long serialVersionUID = 1L;
     private Type type;
     public AnimalLiteral(Type type) {
        this.type = type;
     }
  
     public Type value() {
        return type;
     }
  }
  </code></pre>
  
  <p>The main component is the Animal factory:</p>
  
  <pre><code>@Dependent
  public class AnimalFactory {
  
     @Inject
     @Any
     private Instance&lt;Animal&gt; animals;
  
     private static Map&lt;String, AnimalModel.Type&gt; animalMap;
  
     public AnimalFactory() {
         animalMap = new HashMap&lt;&gt;();
         animalMap.put("ant", AnimalModel.Type.ANT);
         animalMap.put("dog", AnimalModel.Type.DOG);
         animalMap.put("elephant", AnimalModel.Type.ELEPHANT);
     }
  
     public Animal getAnimal(String animalName) {
        AnimalModel.Type type = animalMap.get(animalName);
        AnimalLiteral literal = new AnimalLiteral(type);
        Instance&lt;Animal&gt; animalInstance = animals.select(literal);
        return animalInstance.get();
     }
  }
  </code></pre>
  
  <p>And client:</p>
  
  <pre><code>public class Client {
  
     @Inject
     private AnimalFactory animalFactory;
  
     public void run(String animalName) {
        Animal animal = animalFactory.getAnimal(animalName);
        animal.eat("some food...");
     }
  
  }
  </code></pre>
  
  <p>I don't know that putting the map animalMap in that place is right...?</p>