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

    Create Bean programmatically

    Alexander Bell Newbie

      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