在Managed bean中注入Sessionscoped bean的问题

时间:2010-04-07 10:40:00

标签: java dependency-injection ejb

我有一个会话范围的bean

@SessionScoped
public class UserData implements Serializable {

private String uid;

public String getUid() {
    return uid;
}

public void setUid(final String uid) {
    this.uid = uid;
}

我在无状态会话bean

中的SessionScoped bean中设置了一个值
public void setOperator(final Operator operator) {
    userData.setUid(operator.getId());
}

当我尝试使用@Inject获取对象时,我只得到null

@ManagedBean(name = "RoleController")
@SessionScoped
public class RoleController {

...

@Inject
private UserData userData;

...

public UserData getUserData() {
    System.out.println("ID");
    System.out.println(userData.getUid());

我有一个bean.xml

1 个答案:

答案 0 :(得分:4)

替换

@ManagedBean(name = "RoleController")

@Named("RoleController")

并确保您为两个类导入javax.enterprise.context.SessionScoped而不是javax.faces.bean.SessionScoped。