I want to insert a CDI Bean in a ManagedBean with the annotation @Inject or @Produce. CDI Bean which I use:
@Named @Startup @ApplicationScoped public class BaseBean { private List<String> custs; public List<String> getCusts() { return custs; } public void setCusts(List<String> emps) { this.custs = emps; } public BaseBean(){ } @PostConstruct void init() { custs = new ArrayList<String>(); custs.add("Cust1"); custs.add("Cust3"); custs.add("Cust2"); custs.add("Cust4"); } }
The ManagedBean I want to introduce the CDI Bean into:
@SessionScoped @ManagedBean public class Hello implements Serializable { @Inject private BaseBean dBean; private static final long serialVersionUID = 1L; private List<String> customers; private List<String> customersSelect; public Hello() { } @PostConstruct void init() {
However, in the init function, it throws a NullPointerException. If I use the @Produces annotation instead of @Inject, the result will be the same: NullPointerException. Is there something wrong with the CDI Bean (incorrect annotations)? Am I trying to enter it incorrectly? Does my code have something missing? How can I make it work? Here is the JSF code:
<h:form id ="f"> <h:selectManyCheckbox layout="pageDirection" border="1" value="#{hello.customersSelect}"> <f:selectItems value="#{hello.customers}"></f:selectItems> </h:selectManyCheckbox><br /> <h:commandButton action="response.xhtml" value="Click me" /> </h:form>
PS: If I use Stateless Bean as a BaseBean and I add it using the @EJB annotation, it works without problems.
UPDATE: I tried it with @SessionScoped ( javax.enterprise.context.SessionScoped ) and @Named in the Hello class. Although I do not get a NullPointerException , h:selectManyCheckbox empty. Moreover, it seems to me that when I add the beans.xml file to the META-INF folder, I get a StartException , although the file should be. I think my application does not have the proper configuration to be able to inject dependencies. What might require additional configuration?
UPDATE 2: This error appears when I add the beans.xml file to the WEB-INF folder. The beans.xml file is empty, it contains only the line:
<?xml version="1.0" encoding="UTF-8"?>
Error:
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:" => {"Operation step-2" => {"JBAS014671: Failed services" => {"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\""}}}}