1
votes

I have : - Repository Class:

@SessionScoped
public class EmployeeRepository {
    @PersistenceContext
    EntityManager entityManager;
    public List<Employee> getEmployees(){
        TypedQuery<Employee> qu = entityManager.createQuery("select * from Employee", Employee.class);
        List<Employee> emp2 = qu.getResultList();
        return emp2;
    }
}

and

Managed Bean:

@ManagedBean(name = "helloWorldBean")
public class HelloWorldBean {
    @Inject
    private EmployeeRepository employeerepo;
    public String getMsg() {
        return "Hallo";
    }
    public String getEmployees() {
        return String.valueOf(employeerepo.getEmployees().size());
    }
}

and a JSF Page:

<h:head>
    <title>JavaCodeGeeks</title>
</h:head>
<h:body>
    - Message : <h:outputText value="#{helloWorldBean.msg}" />
    - Employee count : <h:outputText value="#{helloWorldBean.employees}" />
</h:body>
</html>

I have an beans.xml in my META-INF Folder (src\META-INF) without special configurations:

<?xml version="1.0"?>
<beans xmlns="http://xmlns.jcp.org/xml/ns/javaee" 
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
                      http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd"
  bean-discovery-mode="all" version="1.1">
</beans>

Problem:

Page throws nullpointer exception, because the EmployeeRepository is not injected in the HelloWorldBean.

What to do to be able to inject Instances of classes in my case ?

1
Which version of CDI are you using? - Rouliboy
javaee-api-6.0.jar . beans_1_1.xsd. My beans.xml is in src\META-INF\ - mcfly soft
Ok, beans_1_1 is CDI 1.1. Do you have bean-discovery-mode property in beans.xml? Which is the value? By the way, CDI 1.1 is Java EE 7 while you have Java EE 6 api... - Rouliboy
I have added the content of beans.xml to the question. Thanks for helping. - mcfly soft
Which application server or servlet container (and version) are you using? - Selaron

1 Answers

4
votes

As you are using CDI, you should not be using @ManagedBean (which is a JSF annotation). While I have actually seen this working, most implementations will not allow you to inject a CDI bean into a classic JSF bean.

To allow for CDI injection in JSF beans, the CDI specification allows you to define JSF backing beans by specifying the @Named annotation in combination with a scope (@RequestScoped, @javax.faces.view.ViewScoped, @SessionScoped and @ApplicationScoped).

So in summary, the following piece of code should solve your problem,

@Named
@RequestScoped
public class HelloWorldBean {
    @Inject
    private EmployeeRepository employeerepo;
    public String getMsg() {
        return "Hallo";
    }

    public String getEmployees() {
        return String.valueOf(employeerepo.getEmployees().size());
    }
}

You can also read up further on the topic via some old Q/A's here on the site,

How to inject a CDI Bean in a ManagedBean?

@ManagedProperty does not work in a CDI managed bean

CDI beans injection