Bryant,
If you want that container manages the lifecycle of your instances, you have to
provide "Bean" for it. Currently, all of the POJO Beans are automatically
handled as ManagedBean with JSR-299 containers. Just add META-INF/beans.xml
into your JARs.
>>>What is the difference between using InjectionTarget calls and the
>>>>BeanManager.getReference()?
InjectionTarget is provided that is responsible for creating a bean instance,
injecting its dependencies and calling lifecycle callbacks. In OWB we provide
default implementation of this interface.
You can also register your custom InjectionTarget instance for your classes.
To do this, give an Extension that observes ProcessInjectionTarget event, like
this
public void observe(@Observes ProcessInjectionTarget<WinkResourceClass> event){
event.setInjectionTarget("Your custom injection target") --> new
CustomInjectionTarget(event.getInjectionTarget());
}
Then all calls of the InjectionTarget methods by the container are propogated
into your custom InjectionTarget instance.
public class CustonInjectionTarget() implements InjectionTarget{
InjectionTarget orjinal ; --> This is the OWB provided default injection
target
public CustomInjectionTarget(InjectionTarget orjinal){
this.orjinal = orjinal;
}
public void inject(){
orijinal.inject
//Anything you want
}
}
For example, lets says that you have a class WinkResource and classpath
contains the META-INF/beans.xml, then your POJO is autmatically handled by the
OWB container. When container creates you bean instance for some injection, it
uses your provided injection target instance;
yourCustomInjectionTarget.create(....)
yourCustomInjectionTarget.inject(.....)
etc.
BeanManager#getReference returns a Proxy for bean instances that have normal
scoped and returns a actual bean instances if bean has a scope dependent.
InjectionTarget and getReference has totally different aim.
Thanks;
--Gurkan
________________________________
From: Bryant Luk <[email protected]>
To: openwebbeans-user <[email protected]>
Sent: Wed, May 26, 2010 6:31:27 PM
Subject: Re: Creating a contextual managed bean by using BeanManager?
Hi strub,
Thanks for the response. I want to use JCDI / OpenWebBeans to manage
the lifecycles/injection of my JAX-RS resources/providers. Currently,
Wink does the lifecycle management and injection. So if a HTTP
request comes in to our RestServlet, we determine which resource class
(basically a POJO with @Path) to use, instantiate it, and inject it.
What I want to do is to support JSR-299 managed beans here so we get
the benefit of the scopes, @Inject support, and other goodies.
I played around a little bit with the Tomcat / OpenEJB / OpenWebBeans
integration that Gurkan posted to his blog (cool stuff except I think
the patch needed to modify some of the package names with the recent
OSGi change; I was going to post back to the OpenEJB issue but I keep
getting a 503 Service Unavailable for JIRA). I was thinking I could
replace some of our lifecycle management code with calls to the
BeanManager.
What is the difference between using InjectionTarget calls and the
BeanManager.getReference()?
I was also looking into using a JCDI Extension to modify the
"metadata" to possibly add our own injections for some of the JAX-RS
injections but thought I'd tackle the lifecycle questions first.
On Wed, May 26, 2010 at 9:52 AM, Bryant Luk <[email protected]> wrote:
> 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.
>