3 Replies Latest reply on Aug 16, 2011 4:25 PM by lightguard

    Programatic injection inside an interceptor

    tsweeney56

      In seam 2 injection into an around invoke interceptor was simple using the Contexts.getSomeContext.get(Name).  Using CDI the programmatic way of injecting still requires an injection of an instance object.  In Seam 3/CDI is there any idea of a Contexts object to access the contexts to avoid the need for any @Inject?

        • 1. Re: Programatic injection inside an interceptor
          lightguard

          You can do it, but it's messy :) If you still want the code let us know. Also curious as to why you'd want to do it this way.

          • 2. Re: Programatic injection inside an interceptor
            tsweeney56

            I am converting a seam 2 app to seam 3, there are a few cases that we intercept method calls to cache the result or move along a state machine.


            I would love the code regardless how messy it is.

            • 3. Re: Programatic injection inside an interceptor
              lightguard

              If you're doing interception, why not write interceptors and use injection that way?


              Creating an interceptor and doing injection into the interceptor would be a much better solution though.


              It's not perfectly documented, but you can use this method:


              @SuppressWarnings("unchecked")
              public static <T> T getContextualInstance(final BeanManager manager, final Class<T> type)
              {
                 T result = null;
                 Bean<T> bean = (Bean<T>) manager.resolve(manager.getBeans(type));
                 if (bean != null)
                 {
                    CreationalContext<T> context = manager.createCreationalContext(bean);
                    if (context != null)
                    {
                       result = (T) manager.getReference(bean, type, context);
                    }
                 }
                 return result;
              }