3
我希望爲少數託管bean創建超類,其中包含注入的@SessionScoped託管bean和@EJB。以下作品託管Bean繼承
超類:
@ManagedBean
@ViewScoped
public abstract class UserPageController implements Serializable {
private static final long serialVersionUID = -6118448960678133445L;
@ManagedProperty(value = "#{sessionController}")
protected SessionController sessionController;
private User user;
private String userId;
@EJB protected UserBean userBean;
public UserPageController() {
}
public SessionController getSessionController() {
return sessionController;
}
public void setSessionController(SessionController sessionController) {
this.sessionController = sessionController;
}
//-------------------------------- User ------------------------------------
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
public User getUser() {
if (user == null) {
if (getUserId() != null && sessionController.getLoggedInUser().getRole().isManager()) {
User dbUser = userBean.findUser(Long.parseLong(userId));
if (dbUser != null) {
setUser(dbUser);
}
} else {
setUser(userBean.findUser(sessionController.getLoggedInUser().getId()));
}
}
return user;
}
public void setUser(User user) {
this.user = user;
}
}
用法:
@ManagedBean
@ViewScoped
public class UserCompetenceController extends UserPageController implements Serializable {
// Stuff that uses sessionController & userBean.
}
有什麼辦法避免註釋抽象超類爲管理bean?它不知何故感覺不對,但沒有它,依賴注入將無法工作。