【发布时间】:2013-10-02 20:34:28
【问题描述】:
我正在尝试在 JSF 上构建一个简单的博客。但是,我不知道如何将相同的有状态 ejb 实例注入 2 个不同的托管 bean。我知道可以通过使用 @ManagedProperty 注释间接完成注入。类似的东西:
@ManagedBean
@ViewScoped
public class PostController implements Serializable {
private static final long serialVersionUID = 1L;
private Post temporaryPost;
@ManagedProperty(value = "#{authenticationController}")
private AuthenticationController authenticationController;
@Inject
private PostEJB postEJB;
public void save() {
getTemporaryPost().setAuthor(
getAuthenticationController().getAuthenticationEJB()
.getCurrentSessionUser());
postEJB.create(getTemporaryPost());
}
}
我想摆脱
@ManagedProperty(value = "#{authenticationController}") private AuthenticationController authenticationController;
直接注入AuthenticationEJB,比如
@Inject private AuthenticationEJB authenticationEJB;
所以,而不是
getAuthenticationController().getAuthenticationEJB() .getCurrentSessionUser()
我会得到
authenticationEJB.getCurrentSessionUser()
但是,问题是这是新的 authenticationEJB 实例,它不包含当前登录的用户(用户为空)。同时 authenticationController.authenticationEJB.currentsessionuser 包含登录用户。
提前致谢!
终于找到答案了!很简单:
@ManagedProperty(value = "#{authenticationController.authenticationEJB}")
private AuthenticationEJB authenticationEJB;
现在它指向同一个 authenticationEJB 实例。但是,我相信可能还有其他方法可以做到这一点。
【问题讨论】:
-
您是否尝试过将
@SessionScoped用于PostController托管bean? -
刚刚试过,@SessonScoped 没有帮助。
-
对于 EJB,我使用
@EJB而不是@Inject,也许对你有帮助 -
不,这也没有帮助。但我找到了anwser。我马上贴出来!
-
终于找到答案了!很简单:@ManagedProperty(value = "#{authenticationController.authenticationEJB}") private AuthenticationEJB authenticationEJB;现在它指向同一个 authenticationEJB 实例。但是,我相信可能还有其他方法可以做到这一点。
标签: jakarta-ee ejb managed-bean