我試圖向我的bean注入服務,但始終是null
。 我收到以下錯誤:WELD-001000解決屬性userBean針對基本null錯誤。「@inject」-ed屬性保留爲空
一些代碼片段:
的index.xhtml
<h:body>
Hello from Facelets
#{userBean.name}
</h:body>
userbean.java
package beans;
import Domain.User;
import java.io.Serializable;
import javax.enterprise.context.SessionScoped;
import javax.inject.Inject;
import javax.inject.Named;
import service.UserService;
@Named
@SessionScoped
public class UserBean implements Serializable{
@Inject UserService service;
private User user;
public UserBean(){
this.user = service.find_user("foo");
}
public String getName(){
return "bar";
}
}
UserService.java
package service;
import Domain.User;
import javax.ejb.Stateless;
import javax.inject.Named;
@Named
@Stateless
public class UserService {
public UserService(){}
public User find_user(String name){
return new User();
}
}
這似乎是的情況下,如何將我解決這一問題? – martijnve 2011-04-14 09:55:25
@Martijn v E - 想到一個解決方案,看我最後的編輯:) – 2011-04-14 10:01:23
@Martijn v規範依賴注入E發生後的構造函數。 – Laures 2011-04-14 20:50:09