JSFアプリケーションを実行していて、アプリケーションスコープのバッキングBeanを宣言しました(common-beans.xmlまたは @ManagedBean
and @ApplicationScoped
アノテーションを使用)。
どうすればこれらのBeanに内部からアクセスできますjavax.servlet.http.HttpSessionListener
か?
FacesContext
セッションリスナーではが利用できないことを理解している ので、以下を使用します。
public class AnHTTPSessionListener implements HttpSessionListener {
...
public void sessionDestroyed(HttpSessionEvent e) {
AppBean appBean = (AppBean) FacesContext.getCurrentInstance()
.getExternalContext()
.getApplicationMap().get("appBean")
...
}
...期待どおりにNPEをスローしました。
アップデート:
(BalusCの回答前)最終的には、(アプリケーションスコープのBeanを使用する代わりに) env-entry要素を使用してweb.xmlでアクセスする必要のあるアプリケーション全体の情報を宣言し、次を使用してその情報を取得しました。
InitialContext ic = new InitialContext();
Context env = (Context) ic.lookup("java:comp/env");
appName = (String) env.lookup("appBeanValue");
それは私が考えていたものではありませんが、回避策です。