我有一個ManagedBean gett無狀態bean注入,但它總是給我一個注入空指針。我在這裏做錯了什麼? (I'm學習JSF和它只是一個例子,所以請忽略類等的命名)注入到託管bean的statless bean給出空指針
有兩個不同的罐子(web.jar和services.jar)
託管bean
@ManagedBean
@RequestScoped
public class HelloPB
{
@Inject
private ExamServiceBase examService;
private String name = "";
public String getName()
{
Exam exam = examService.getSingleExam();
return exam.getName();
}
public void setName(final String name)
{
//this.name = name;
}
public String getHello()
{
if (name == null || name.length() < 1)
{
return null;
}
return "Hello " + name;
}
}
無國籍豆
@Stateless
public class ExamServiceBase implements ExamService{
@PersistenceContext(name="QuestifyPersistUnit")
private EntityManager em;
public Exam getExam(String id){
return em.find(Exam.class, id);
}
public Exam getSingleExam(){
return em.find(Exam.class, "9E69F3EE-AE9E-4D53-B531-35504EDA450F");
}
}
的index.xhtml
<h:body>
<h:outputScript name="jsf.js" library="javax.faces" target="body">
</h:outputScript>
<h1>JSF 2 Demo</h1>
<h:form>
<h:inputText id="name" value="#{helloPB.name}">
<f:ajax render="helloTextOutput" />
</h:inputText>
<h:commandButton value="Say Hi via Ajax">
<f:ajax execute="name" render="helloTextOutput" />
</h:commandButton>
<h:outputText id="helloTextOutput" value="#{helloPB.hello}" />
</h:form>
</h:body>
ExamServiceBase是一個EJB,所以你試圖通過EJB註釋注入它嗎?我不確定它是否適用於Inject ... – perissf 2013-03-24 18:11:19
@perissf,它的工作原理和它是完全合法的。 @Martin,是'ExamService'和'@ Local'接口?你應該嘗試注入,如果它是 – kolossus 2013-03-24 18:48:52
@kolossus我確實有一個本地接口,這就是我原來使用的,但既沒有,也沒有我的實際實現。兩者都給出空指針例外 – Marthin 2013-03-25 08:38:41