托管 Bean 继承
我希望为一些托管 bean 创建一个超类,其中包含注入的 @SessionScoped 托管 bean 和 @EJB。以下作品
超类:
@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?不知何故感觉不对,但如果没有它,依赖注入将无法工作。
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 = 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;
}
}
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我想说,即使你最终得到一个从其基类继承字段的类,注入也是单独为这个类完成的,这意味着为了工作,基类也必须被注释 - 但这是只是一个猜测。
您是否尝试过使用 CDI 进行相同的构造?
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?