How to insert a CDI Bean in a ManagedBean?

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() { // dBean = new BaseBean(); customers = dBean.getCusts(); } public List<String> getCustomers() { return customers; } public List<String> getCustomersSelect() { return customersSelect; } public void setCustomersSelect(List<String> customersSelect) { this.customersSelect = customersSelect; } } 

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\""}}}} 
+6
source share
3 answers

What @patlov suggests will work if you use @Named on your CDI beans. However, if you are working in a CDI-enabled environment, do not use @ManagedBean . Instead, use full CDI. See this answer , and I am sure that you can find many others who strongly advise against what you are trying to do.

Just switch from javax.faces.bean.SessionScoped to javax.enterprise.context.SessionScoped and everything will work magically. However, you may run into the lack of @ViewScoped from CDI, in which case use something like JBoss Seam or Apache Deltaspike that will implement it for you. As an added benefit, they will automatically automatically replace all JSF areas with CDI areas if you already have code written for JSF.

Update: This should be the contents of your beans.xml:

 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/beans_1_0.xsd"> </beans> 
+9
source

Make sure you enable CDI by placing the WEB-INF/beans.xml file in the application.

+3
source

If you are using @ManagedBean , use @ManagedProperty to enter properties:

 @ManagedProperty(value = "#{baseBean}") private BaseBean dBean; // getter and setter 
+1
source

All Articles