9 Replies Latest reply on Aug 20, 2009 1:15 PM by pmuir

    Create Bean programmatically

      Hi,


      I want to retrieve a Bean with a given Type programmatically.
      Until now I use this way:


      BeanManager manager = CurrentManager.rootManager();
      Set<Bean<?>> beans = manager.getBeans(MyService.class);
      if(beans != null && beans.size() == 1) {
          Bean<?> bean = beans.iterator().next();
          Object serviceObject = manager.getReference(bean, MyService.class);
          return serviceObject;
      } else {
          ...
      }



      I use the serviceObject and afterwards I wanna destroy it because I want that all dependent objects are destroyed and all disposer methods are called.
      I've found a method called javax.enterprise.inject.spi.Bean.destroy.
      But this method needs a CreationalContext as one of its parameters.
      So what is a CreationContext and where can I get one?


      Maybe there's another way to get a Bean programmatically and destroy it afterwards.


      Alex

        • 1. Re: Create Bean programmatically
          pmuir

          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

            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

              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

                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

                  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

                    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

                      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

                        Thanks Pete

                        • 9. Re: Create Bean programmatically
                          pmuir

                          BTW InjectionTarget is implemented in trunk :-)