4 Replies Latest reply on Aug 24, 2010 1:30 PM by wangliyu

    Pls Help: Can't find the BeanManager object from the PhaseListener

    wangliyu

      Hi,
      I tried to use JNDI lookup to get the BeanManager in the PhaseListener, see the following code:




      private static BeanManager bmgr \= null;
      ...
      
      private static BeanManager getBeanManager() {
         if (bmgr \=\= null) {
              try {
                  bmgr \= (BeanManager) new InitialContext().lookup("java:comp/BeanManager");
              } catch (NamingException e) {
                  try {
                      bmgr \= (BeanManager) new InitialContext().lookup("java:app/BeanManager");
                  } catch (NamingException e1) {
                      throw new RuntimeException(e1);
                  }
      
              }
          }
          return bmgr;
      }





      and it always return null, I deployed this on TC.6 and GF.3 and none of them worked, I checked the Weld source code, found in Weld, it use the following code to get the bmgr outside the Weld container:




      private BeanManager getBeanManager() {
              if (mgr \=\= null) {
                  ServletContext ctx \= (ServletContext) FacesContext.getCurrentInstance().getExternalContext().getContext();
                  mgr \= (BeanManager) ctx.getAttribute(BeanManager.class.getName());
              }
              return mgr;
          }





      But according to the JavaDoc:



      Java EE components may obtain an instance of BeanManager from JNDI by looking up the name java:comp/BeanManager