我有一个 @SessionScoped cdi bean,用于跟踪我的 Web 应用程序中的用户会话信息。有没有办法从另一个 @ApplicationScoped bean 中找到这个 bean 的所有对象?
问问题
1026 次
1 回答
2
You cannot do this out of the box. Java EE forbid this kind of things for security reason.
Now you can imagine a more elaborate approaches to keep track of these session beans at your application scope level. The cleanest way would be to produce them from an @ApplicationScoped
bean :
@ApplicationScoped
public class Registry {
private List<SessionData> data = new ArrayList<>;
@Produces
@SessionScoped
public SessionData produceSessionData() {
SessionData ret = new SessionData();
data.add(ret);
return ret;
}
public void cleanSessionData(@Disposes SessionData toClean) {
data.remove(toClean);
}
}
Note the @Dispose
method which will be called when your produced bean has ended its lifecycle. A convenient way to keep your list up to date and avoid extra memory usage.
于 2014-11-15T02:49:19.250 回答