It works. I changed the getBean method a bit and now it works. It needs the class as a parameter and no longer the name of the Managed Bean.

  @SuppressWarnings("unchecked")
  public static <T> T getBean(Class<T> type) {
    BeanManager beanManager = getBeanManager();
    if (null == beanManager) {
      return null;
    }

    Bean<T>               bean              =
        (Bean<T>) beanManager.resolve(beanManager.getBeans(type));
    CreationalContext<T>  creationalContext =
        beanManager.createCreationalContext(bean);

    return (T) beanManager.getReference(bean, type, creationalContext);
  }

Thanks for the quick support.

Marco

Op 30-03-13 21:15, Marco de Booij schreef:
Hello Mark,

Thanks for the information. I will try it this way. I do not want to use more packages as needed.

Regards,

Marco

Op 30-03-13 19:06, Mark Struberg schreef:
Hi Marco!

Please don't do beans.iterator().next() - you can do BeanManager#resolve() instead.

Also Bean<T> != Contextual Instance of T

You would to create a CreationalContext<T> and invoke BeanManager#getReference

You might take a look at Apache DeltaSpike BeanProvider [1] which provides easy helpers for exactly that: getContextualReference(..) Plus this also works in environments without a writeable JNDI contract like in plain tomcat.

LieGrue,
strub



Reply via email to