1 Reply Latest reply on Aug 26, 2010 11:40 AM by Wang Liyu

    Using WELD from ServlerFilter

    Diego Ramp Newbie

      Is there any possibility to use WELD-Beans from non-weld-enabled components (servlet 2.5 on different platforms) like
      - jsf-converter
      - jsf-phase-listener
      - servlet filter


      in converters/phaselisteners i make roundtrip accessing them by name which works fine.


      but in servlet filter this does not work because a faces-context does not exist at this time.

        • 1. Re: Using WELD from ServlerFilter
          Wang Liyu Apprentice

          For JSF converter/phase-listener/system-listener, you can call FacesContext.getApplication().addConverter()/subscribe/ in the @PostConstruct method of your CDI beans.


          or another way around:


             


          public static <T> T getBean(Class<? extends T> clazz, Annotation... qualifiers) {
                  logger.debug("try to get the bean as {}", clazz.toString());
                  try {
                      Bean<T> bean = (Bean<T>) getBeanManager().resolve(getBeanManager().getBeans(clazz, qualifiers));
                      return (T) getBeanManager().getReference(bean, clazz, getBeanManager().createCreationalContext(bean));
                  } catch (Exception e) {
                      logger.error("can't get the bean reference", e);
                  }
                  return null;
              }
          




          lookup BeanManager from jndi names: {"java:comp/BeanManager", "java:comp/env/BeanManager", "java:app/BeanManager"};


          you can call this static method in the listeners.