That works for me (too). thanks again for this topic and all responses, Marco and Mark. :)
On Sat, Mar 30, 2013 at 5:53 PM, Howard W. Smith, Jr. < [email protected]> wrote: > Marco, thanks for replying with your code. Are you calling the method like > this below? > > getBean(someClass.class) > > > > > On Sat, Mar 30, 2013 at 5:42 PM, Marco de Booij <[email protected]>wrote: > >> 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 >>>> >>> >>> >>> >> >
