托管 Bean 继承

发布于 2024-11-05 11:12:29 字数 1796 浏览 7 评论 0原文

我希望为一些托管 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

北音执念 2024-11-12 11:12:29

我想说,即使你最终得到一个从其基类继承字段的类,注入也是单独为这个类完成的,这意味着为了工作,基类也必须被注释 - 但这是只是一个猜测。

您是否尝试过使用 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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文