openwebbeans-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Gurkan Erdogdu <gurkanerdo...@yahoo.com>
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
}


BeanManager#createAnnotatedType
BeanManager#createInjectionTarget

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(....)
InjectionTarget.inject(servletInstance)

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;

Thanks;

--Gurkan




________________________________
From: Bryant Luk <bryant.luk@gmail.com>
To: openwebbeans-user <openwebbeans-user@apache.org>
Sent: Wed, May 26, 2010 5:52:52 PM
Subject: Creating a contextual managed bean by using BeanManager?

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.



Mime
View raw message