Uploaded image for project: 'RESTEasy'
  1. RESTEasy
  2. RESTEASY-3256

CDI managed beans do not inject @Context injection targets

XMLWordPrintable

      In a provider that is not annotated with @Provider and is a CDI managed bean, the @Context injection targets are left null. An example would be a filter that is installed via a feature.

      @Provider
      public class FilterRegistrationFeature implements Feature {
          @Override
          public boolean configure(FeatureContext context) {
              context.register(TestFilter.class);
              return true;
          }
      }
      
      @ApplicationScoped
      public class TestFilter implements ContainerRequestFilter {
      
          @Context
          HttpServletRequest request;
      
          @Context
          UriInfo uriInfo;
      
          @Inject
          HttpServletRequest injectedRequest;
      
          @Inject
          TestBean testBean;
      
          public void filter(ContainerRequestContext requestContext) {
              System.out.println("request " + request);
              System.out.println("injectedRequest " + injectedRequest);
              System.out.println("uriInfo " + uriInfo);
              System.out.println("testBean " + testBean);
          }
      }
      

      In this case the request and uriInfo are both null. In RESTEasy 6.1+ you can just @Inject these types. However, with 5.x and lower that is not possible. The JavaDoc for Configurable.register(Class) indicates:

      As opposed to component instances registered via register(Object) method, the lifecycle of components registered using this class-based register(...) method is fully managed by the JAX-RS implementation or any underlying IoC container supported by the implementation.

              jperkins-rhn James Perkins
              jperkins-rhn James Perkins
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

                Created:
                Updated:
                Resolved: