title: How to inject a CDI Bean in a ManagedBean?
body: <p>I want to inject a CDI Bean in a ManagedBean either with the annotation @Inject or @Produce. The CDI Bean which I use is:</p>
  
  <pre><code>@Named
  @Startup
  @ApplicationScoped
  public class BaseBean {
  
  private List&lt;String&gt; custs;
  
  public List&lt;String&gt; getCusts() {
      return custs;
  }
  
  public void setCusts(List&lt;String&gt; emps) {
      this.custs = emps;
  }
  
  public BaseBean(){
  
  }
  
  @PostConstruct
  void init() {
      custs = new ArrayList&lt;String&gt;();
      custs.add("Cust1");
      custs.add("Cust3");
      custs.add("Cust2");
      custs.add("Cust4");
  }
  
  }
  </code></pre>
  
  <p>The ManagedBean, in which I want to inject the CDI Bean is:</p>
  
  <pre><code>@SessionScoped
  @ManagedBean
  public class Hello implements Serializable {
  
  @Inject
  private BaseBean dBean;
  
  private static final long serialVersionUID = 1L;
  private List&lt;String&gt; customers;
  private List&lt;String&gt; customersSelect;
  
  public Hello() {
  }
  
  @PostConstruct
  void init() {
  //  dBean = new BaseBean();
      customers = dBean.getCusts();
  }
  
  public List&lt;String&gt; getCustomers() {
      return customers;
  }
  
  public List&lt;String&gt; getCustomersSelect() {
      return customersSelect;
  }
  
  public void setCustomersSelect(List&lt;String&gt; customersSelect) {
      this.customersSelect = customersSelect;
  }
  }
  </code></pre>
  
  <p>In the init function however, it throws NullPointerException. If I use the annotation @Produces instead of @Inject, the result is the same: NullPointerException. Is anything wrong with the CDI Bean (improper annotations)? Do I try to inject it with a wrong way? Does my code lack of something? How can I get it work? Here is the JSF code:</p>
  
  <pre><code>&lt;h:form id ="f"&gt;
      &lt;h:selectManyCheckbox layout="pageDirection" border="1"  value="#{hello.customersSelect}"&gt;
          &lt;f:selectItems value="#{hello.customers}"&gt;&lt;/f:selectItems&gt;
      &lt;/h:selectManyCheckbox&gt;&lt;br /&gt;
      &lt;h:commandButton action="response.xhtml" value="Click me" /&gt;
  &lt;/h:form&gt;
  </code></pre>
  
  <p>PS: If I use a Stateless Bean as BaseBean and I inject it with the annotation @EJB, it works with no problem.  </p>
  
  <p><strong>UPDATE:</strong> I have tried it with the annotations <code>@SessionScoped</code> (<code>javax.enterprise.context.SessionScoped</code>) and <code>@Named</code> on the <code>Hello</code> class. Although I do not receive a <code>NullPointerException</code>, the <code>h:selectManyCheckbox</code> is empty. moreover, it strikes me, that when I add the <code>beans.xml</code> file under <code>META-INF</code> folder, I receive a <code>StartException</code>, although the file is there supposed to be. I think my application lacks the proper configuration to be capable of Dependency Injection. What is likely to need additional configuration?  </p>
  
  <p><strong>UPDATE 2:</strong> This error appears when I add the beans.xml file in the WEB-INF folder. The beans.xml file is empty, it contains only the line : </p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; 
  </code></pre>
  
  <p>The error is:</p>
  
  <pre><code>Services which failed to start:      service jboss.deployment.unit."JSF1.war".PARSE: org.jboss.msc.service.StartException in service jboss.deployment.unit."JSF1.war".PARSE: Failed to process phase PARSE of deployment "JSF1.war"
  
  12:51:11,482 ERROR [org.jboss.as.server.deployment.scanner] (DeploymentScanner-threads - 1) {"JBAS014653: Composite operation failed and was rolled back. Steps that failed:" =&gt; {"Operation step-2" =&gt; {"JBAS014671: Failed services" =&gt; {"jboss.deployment.unit.\"JSF1.war\".PARSE" =&gt; "org.jboss.msc.service.StartException in service jboss.deployment.unit.\"JSF1.war\".PARSE: Failed to process phase PARSE of deployment \"JSF1.war\""}}}}
  </code></pre>