-
1. Re: Create Bean programmatically
pmuir Jul 8, 2009 4:53 PM (in response to alexanderbell)You should use:
@Current Instance<Object> myService; myService.select(MyService.class).doFoo();
then the container will do the cleanup for you.
-
2. Re: Create Bean programmatically
alexanderbell Jul 9, 2009 2:24 PM (in response to alexanderbell)Unfortunately I cannot use the @Current annotation in my class because it isn't created by the WebBeans Manager.
So I have to do in in a full programmatically manner.Any ideas?
-
3. Re: Create Bean programmatically
niravshah Jul 14, 2009 4:31 PM (in response to alexanderbell)Am using a similar kind of programatic creation of beans (since my base class is not manager created).
Here's the code. Hope it helps.Set<Bean<CieResource>> beans = manager.resolveByType(CieResource.class, new AnnotationLiteral<CieStandardProvBT>(){}); for(Bean<CieResource> discoveredResource : beans) { CieResource resource = discoveredResource.create(new CreationalContextImpl<CieResource>(discoveredResource)); resource.register(this); }
Cheers!
Nirav -
4. Re: Create Bean programmatically
alexanderbell Jul 14, 2009 9:52 PM (in response to alexanderbell)Unfortunately CreationalContextImpl is a WebBeans specific class.
IMO there have to be an API for creating and destroying beans programmatically
In the previous spec. there was a method for creating Beans in the BeanManager API. Unfortunately I don't know the name of the method anymore.@Pete
Do u know a better way for that case? -
5. Re: Create Bean programmatically
pmuir Jul 20, 2009 6:07 PM (in response to alexanderbell)
Alexander Bell wrote on Jul 09, 2009 14:24:
Unfortunately I cannot use the @Current annotation in my class because it isn't created by the WebBeans Manager.
So I have to do in in a full programmatically manner.
Any ideas?What is the class created by?
In the PFD you would do:
manager.createInjectionTarget(type).inject(instance, manager.createCreationalContext(null));
NB implementing this is on my todo list this week.
-
6. Re: Create Bean programmatically
alexanderbell Jul 22, 2009 8:07 AM (in response to alexanderbell)The object is created by Axis (it's the ServiceMessageReceiver).
After reading some webbeans-code I chose this way:BeanManagerImpl manager = CurrentManager.rootManager(); Set<Bean<?>> beans = manager.getBeans(serviceClass); if(beans != null && beans.size() == 1) { Bean<?> bean = beans.iterator().next(); CreationalContext<?> creationalCtx = CreationalContextImpl.of(bean); Object serviceObject = manager.getInjectableReference(bean, creationalCtx); return serviceObject; ...
The serviceObject and CreationalContext are saved in the AxisServiceContext.
Code for disposing
bean.destroy(serviceObject, creationalCtx);
I've got two more questions ;-)
What is a PFD?
What is the meaning of CreationalContext?Alex
-
7. Re: Create Bean programmatically
pmuir Jul 23, 2009 6:40 PM (in response to alexanderbell)Basically, that is correct, except that you should switch to using non-contextual injection once available in CR1. I wrote an FAQ for you http://www.seamframework.org/Documentation/HowDoIDoNoncontextualInjectionForAThirdpartyFramework.
PFD is the proposed final draft of the spec (which is currently published). CreationalContext is a context associated with instance creation and destruction.
-
8. Re: Create Bean programmatically
alexanderbell Aug 20, 2009 7:31 AM (in response to alexanderbell)Thanks Pete
-
9. Re: Create Bean programmatically
pmuir Aug 20, 2009 1:15 PM (in response to alexanderbell)BTW InjectionTarget is implemented in trunk :-)