1 Reply Latest reply on Jan 31, 2018 5:48 PM by Martin Kouba

    Is it legal for an Extension to keep a reference to BeanManager?

    Laird Nelson Apprentice

      In CDI 2.0, if I "leak" a BeanManager reference from a container lifecycle event observer method into an instance or static field in my Extension, can I safely and portably make use of that BeanManager reference from elsewhere?  Or should I always obtain a BeanManager reference at the point where I need it by calling CDI.current().getBeanManager()?

       

      Best,

      Laird

        • 1. Re: Is it legal for an Extension to keep a reference to BeanManager?
          Martin Kouba Master

          Hi Laird,

          I think it's ok to keep a reference to a BeanManager in an extension instance. However, beware of static fields - such a BeanManager instance could potentially survive the shutdown of a container it comes from. And in such case, it should become unusable as its state should be cleaned up. If you only use one container at the same time then WeldContainer.current().getBeanManager() is probably the most efficient way to obtain a BeanManager.