我有以下课程:
ClassA的
import java.io.Serializable;
import javax.enterprise.context.SessionScoped;
import javax.inject.Inject;
import javax.inject.Named;
import javax.persistence.EntityManager;
import timereport.db.UserBean;
import timereport.utils.JPAUtil;
@Named("classA")
@SessionScoped
public class ClassA implements Serializable {
@Inject
protected UserBean userBean;
public void logout() {
userBean = null;
}
public void login() {
EntityManager em = JPAUtil.getEntityManagerFactory().createEntityManager();
userBean = em.find(UserBean.class, userBean.getUsername());
}
//setter and getter for userBean
}
此处login()
我在做
EntityManager em = JPAUtil.getEntityManagerFactory().createEntityManager();
userBean = em.find(UserBean.class, userBean.getUsername());
获取整个UserBean
对象,这是对的。这就是问题......
ClassB
import java.io.Serializable;
import java.lang.annotation.Annotation;
import java.sql.Date;
import java.util.List;
import java.util.ResourceBundle;
import javax.enterprise.context.SessionScoped;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.inject.Inject;
import javax.inject.Named;
import javax.persistence.EntityManager;
import javax.persistence.Query;
import org.apache.log4j.Logger;
import timereport.db.UserBean;
import timereport.ClassB;
import timereport.utils.JPAUtil;
@Named("classB")
@SessionScoped
public class ClassB implements Serializable {
@Inject private UserBean userBean;
String throughUserBean = userBean.getUsername();
...
}
在这里,我不知道为什么,但我希望在用户登录时设置用户名。当我调用@Inject UserBean或@Inject ClassB时,我希望它们来自同一会话范围并保持在登录时初始化的UserBean对象。但是当我引用它们时,(classB和userBean)都返回NULL。我对设置这些对象的期望是错误的还有另外一种方法吗?
以下是UserBean类的一部分:
import java.io.Serializable;
import javax.enterprise.context.SessionScoped;
import javax.inject.Named;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import com.proxiad.timereport.utils.AESEncryptDecryptUtil;
@Named("user")
@SessionScoped
@Entity
@Table(name="t_user")
public class UserBean implements Serializable{
/**
*
*/
private static final long serialVersionUID = -56986575421886097L;
@Id
@Column(name="username")
private String username;
@Column(name="fullname")
private String fullname;
@Column(name="password")
private String password;
@Column(name="email")
private String email;
@ManyToOne(cascade={CascadeType.ALL})
@JoinColumn (name="department", referencedColumnName="value")
private ReferenceDataBean department;
@ManyToOne(cascade={CascadeType.ALL})
@JoinColumn(name="role", referencedColumnName="value")
private ReferenceDataBean role;
public UserBean() {}
public String getFullname() {
return fullname;
}
public void setFullname(String fullname) {
this.fullname = fullname;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
try {
this.password = AESEncryptDecryptUtil.encrypt(password);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public ReferenceDataBean getDepartment() {
return department;
}
public void setDepartment(ReferenceDataBean department) {
this.department = department;
}
public ReferenceDataBean getRole() {
return role;
}
public void setRole(ReferenceDataBean role) {
this.role = role;
}
@Override
public int hashCode() {
return getUsername().hashCode() + 17 * getEmail().hashCode();
}
@Override
public boolean equals(Object obj) {
if (obj instanceof UserBean) {
UserBean user = (UserBean) obj;
if (getUsername().equals(user.getUsername()) && getEmail().equals(user.getEmail())) {
return true;
}
}
return false;
}
@Override
public String toString() {
return String.format("Username: %s\nEmail: %s\nDepartment:\n%s\nRole:\n%s\n",
getUsername(), getEmail(), getDepartment(), getRole());
}
}
答案 0 :(得分:3)
构建后进行注入。施工期间无法使用注入的属性。访问注入属性的最早点是@PostConstruct
方法。
所以,替换
@Inject private UserBean userBean;
String throughUserBean = userBean.getUsername();
通过
@Inject private UserBean userBean;
String username;
@PostConstruct
public void init() {
username = userBean.getUsername();
}
@Inject ClassB
毫无意义,所以我删除了它。