Hi Bryant! There are 3 ways to get contextual instances
1.) private @Inject MyBean x; 2.) 5.6.1 and 5.6.2. of the JSR-299 spec: private @Inject Instance<MyBean> i; ... MyBean x = i.select(); 3.) manually via the BeanManager @Inject BeanManager bm; // or get it from JNDI or extension Set<Bean<MyBean>> beans = bm.getBeans(MyBean.class); Bean<MyBean> bean = beans.iterator().next(); // would need some more to check if only 1 exists ... CreationalContext cc = bm.createCreationalContext(bean); MyBean x = (MyBean) bm.getReference(bean, MyBean.class, cc); But maybe you like to tell us what you wanna do? Maybe there are other ways to solve your problems ;) LieGrue, strub --- On Wed, 5/26/10, Bryant Luk <[email protected]> wrote: > From: Bryant Luk <[email protected]> > Subject: Creating a contextual managed bean by using BeanManager? > To: "openwebbeans-user" <[email protected]> > Date: Wednesday, May 26, 2010, 2:52 PM > Hi, > > I was looking to create managed bean instances dynamically > for an > Apache Wink (JAX-RS) plugin. > > I found the BeanManager interface and was hoping that would > be a good > way to get my instances via a programmatic interface. > Is that a > possible route for me to take? > > So pseudcode-ish, I would do: > > BeanManager beanManager = ... > Class<T> managedBeanClass = ... > AnnotatedType aType = > beanManager.createAnnotatedType(managedBeanclass); > InjectionTarget<T> iTarget = > beanManager.createInjectionTarget(aType); > > /* is this the way to get the right creational context? */ > Set<Bean<?>> beans = > beanManager.getBeans(managedBeanClass, (any > qualifier annotations here)); > Bean bean = beans.iterator().next(); > CreationalContext creationalContext = > beanManager.createCreationalContext(bean); > > T instance = iTarget.produce(creationalContext); > iTarget.inject(instance, creationalContext); > iTarget.postConstruct(instance); > > /* ...use the instance... */ > > iTarget.preDestroy(instance); > iTarget.dispose(instance); > > /* do I need to do a creationalContext.release()? */ > > I'm hoping the created instance will be correctly > injected/disposed of > and would be correctly scoped (i.e. if it's in a servlet > container and > the managed bean was @ApplicationScoped then the above code > would > still "do the right thing" instead of creating a new > instance). > > Thanks for any pointers anyone can provide. >
