title: CDI Dynamic Bean Instances
body: <p>Working with Wildfly / JBoss Weld / CDI 1.1.</p>
  
  <p>Lets say you have a database. Can be anything, MySQL, MongoDB. Could be a REST service. From that database, you get a list of Animals.</p>
  
  <pre><code>[
    "Cat",
    "Dog",
    "Giraffe",
    "Tiger",
    "Chicken"
  ]
  </code></pre>
  
  <p>You do not know what animals you will get from this service, but what you want to do is make them available for Instance injection.</p>
  
  <p>Animal Class:</p>
  
  <pre><code>public class Animal {
    private final String type;
    public String getType() {
      return type;
    }
    public Animal(String aType) {
      type = aType;
    }
  }
  </code></pre>
  
  <p>Injection Point:</p>
  
  <pre><code>@Inject @Any
  public Instance&lt;Animal&gt; animals;
  </code></pre>
  
  <p>You can make a Producer method that makes AN animal, for instance with a qualifier to make a certain animal:</p>
  
  <pre><code>@Produces @AnimalType
  public Animal makeAnimal(InjectionPoint ip) {
    // Get AnimalType qualifier and make a new Animal(typeString), 
    // ...
    return animal;
  }
  </code></pre>
  
  <p>But how do you Produce ALL (known from data) animals so that you can iterate over them with instance?</p>
  
  <pre><code>for(Animal animal : animals) {
    // ...
  }
  </code></pre>
  
  <p>I do want each Animal to get the benefits of Dependency Injection and other Weld/CDI goodies.</p>