Managed Bean inheritance
I wish to have make a super class for a few managed beans, containing amongst other things an injected @SessionScoped managed bean, and an @EJB. The following works
Super class:
@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 = user开发者_JAVA百科Id;
}
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;
}
}
Usage:
@ManagedBean
@ViewScoped
public class UserCompetenceController extends UserPageController implements Serializable {
// Stuff that uses sessionController & userBean.
}
Is there any way to avoid annotating the abstract super class as a managed bean? It somehow feels wrong, but without it, dependency injection won't work.
I would say, that even though you end up with a class inheriting the fields from its base class, the injection is done for the this class separately, meaning in order to work, the base class has to be annotated as well - but this is only a guess.
Have you tried the same construct using CDI?
精彩评论