0 Replies Latest reply on Aug 20, 2012 10:55 AM by Jboss fan99

    BeanManager recreating ApplicationScoped bean instance

    Jboss fan99 Newbie

      Hi,

        I'm using BeanManager to lookup CDI beans when I can't inject them e.g. when I need to programmatically inject those beans in spawned threads. But the problem is that BeanManager returns an instance of ApplicationScoped bean which is not really ApplicationScoped. Because when I inject that bean again in some EJB bean (this time by using @Inject annotation), it creates another instance of the same applicationScoped bean. It doesn't use the instance created by BeanManager.

       

      Will appreciate any help. Below is the code I'm using to inject beans using BeanManager. Is there something wrong with this code?

       

      ---------------

      T myInstance = null;

      Set<Bean<?>> lookedBeansSet = beanManager.getBeans(type); //type is the interface class of ApplicationScoped bean

      Bean<T> resolvedBean = (Bean<T>)beanManager.resolve(lookedBeansSet);

      if (resolvedBean != null)

      {

                  CreationalContext<T> creationalContext = beanManager.createCreationalContext(resolvedBean);

                  if (creationalContext != null)

                  {

                       myInstance = resolvedBean.create(creationalContext); //use this myInstance as injected bean

                  }

      }

      ---------------------------