我想在JSF上構建一個簡單的博客。但是,我不知道如何將相同的有狀態ejb實例注入到2個不同的託管bean中。我知道注入可以通過使用@ManagedProperty註釋間接完成。類似的東西:注入相同的有狀態ejb實例到不同的managedbeans
@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的(值= 「#{} authenticationController」) 私人AuthenticationController authenticationController;
和直接噴射AuthenticationEJB,像
@注入私人AuthenticationEJB authenticationEJB;
所以,與其
getAuthenticationController()。getAuthenticationEJB() .getCurrentSessionUser()
我會得到
authenticationEJB.getCurrentSessionUser()
但問題是,這是新的authenticationEJB實例,它不包含當前登錄的用戶(用戶爲空)。同時,authenticationController.authenticationEJB.currentsessionuser包含登錄用戶。
在此先感謝!
Finnaly得到了答案!這很容易:
@ManagedProperty(value = "#{authenticationController.authenticationEJB}")
private AuthenticationEJB authenticationEJB;
現在它指向相同的authenticationEJB實例。不過,我相信可能有其他方法可以做到這一點。
有YOUT使用'@ SessionScoped'爲'PostController'中託管bean試過嗎? –
剛剛嘗試過,@SessonScoped沒有幫助。 –
對於EJB,我使用'@ EJB'而不是'@Inject',這可能對你有幫助 –