3 Replies Latest reply on May 4, 2010 8:27 PM by Nicklas Karlsson

    Get bean reference on a non EJB aware context

    Cloves Almeida Newbie


      I needed to access a managed bean on a non-managed PhaseListener and had to write (straight from Seam3Faces module)

      BeanManager beanManager;
      try {
           beanManager = (BeanManager) new InitialContext().lookup("java:app/BeanManager");
      } catch (NamingException e) {
           throw new RuntimeException(e);
      Bean<ILoginService> bean = (Bean<ILoginService>) beanManager.getBeans(ILoginService.class).iterator().next();
      CreationalContext<ILoginService> context = beanManager.createCreationalContext(bean);
      ILoginService loginService = (ILoginService) beanManager.getReference(bean, LoginService.class, context);

      IMO, such thing should be part of Weld API (even if as an extension). Something like a static helper method on BeanManager.

      ILoginService service = BeanManager.inject(<type>,<qualifiers>...);