openwebbeans-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Gurkan Erdogdu <>
Subject Re: Creating a contextual managed bean by using BeanManager?
Date Wed, 26 May 2010 15:17:43 GMT
Hello Bryant;

AFAIK from your context, you want to add your own JSR-299 beans into the container. So you
can extend the container beans with using Extensions. So, you create a class that implements
javax.enterprise.inject.spi.Extension and put it into  the classpath "META-INF/services" with
file name "javax.enterprise.inject.spi.Extension" name.

In this class, you could observe the AfterBeanDiscoveryEvent event like this

public class MyExtensionForCustomBean implements  javax.enterprise.inject.spi.Extension
     public void observe(@Observes AfterBeanDiscoveryEvent event){
             event.addBean("Your custom bean")             

Your custom bean might be
public class MyCustomBean extends OwbBean or Bean {

     Add your bean methods


methods are used for non-contextual components for injecting the JSR-299 dependencies. For
example, if you have a servlet instance and would like to inject JSR-299 dependencies for
it, you could do this with above methods like we did for Apache Tomcat integration.

For example : We have a non-contextual servlet instance ServletInstance

Create injection target with BeanManager#createInkectionTarget(....)

When servlet instance is destroyed, its dependencies must also be destroyed via call to
CreationalContext # release(). So you have to call CreationalContext#release method.

I hope it helps;



From: Bryant Luk <>
To: openwebbeans-user <>
Sent: Wed, May 26, 2010 5:52:52 PM
Subject: Creating a contextual managed bean by using BeanManager?


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);

/* ...use the 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.

View raw message