Guice + Jersey 2 + ContainerRequestFilter and @Context
<p>I'm attempting to use Guice to inject dependencies into a <a href="https://jax-rs-spec.java.net/nonav/2.0-SNAPSHOT/apidocs/javax/ws/rs/container/ContainerRequestFilter.html" rel="nofollow">ContainerRequestFilter</a> that is being registered via a <a href="https://docs.oracle.com/javaee/7/api/javax/ws/rs/container/DynamicFeature.html" rel="nofollow">DynamicFeature</a>. I also need Jersey to inject the HttpServletRequest and I am currently attempting to do this via @Context.   This is in a Dropwizard app.</p>

<p>My end goal is to have an AuthenticationFilter that is applied to specific Resources via a DynamicFeature.  My AuthenticationFilter has a few dependencies that must be injected and it also needs access to the HttpServletRequest to do its job.  This is part of a Dropwizard project and the pattern is based on Dropwizard's <a href="http://www.dropwizard.io/0.9.1/dropwizard-auth/apidocs/io/dropwizard/auth/AuthDynamicFeature.html" rel="nofollow">AuthDynamicFeature</a> and <a href="http://www.dropwizard.io/0.9.1/dropwizard-auth/apidocs/io/dropwizard/auth/AuthFilter.html" rel="nofollow">AuthFilter</a> but modified to support injection.</p>

<p>So my AuthenicationFilter looks like this:</p>

<pre><code>public class AuthFilter implements ContainerRequestFilter {

    @Context
    private HttpServletRequest httpServletRequest;

    @Context
    private HttpServletResponse httpServletResponse;

    @Inject
    private InjectableResource injectableResource;

    public void filter(ContainerRequestContext requestContext) throws IOException {

       // Do Auth
    }
}
</code></pre>

<p>And my DynamicFeature looks like this:</p>

<pre><code>public class InjectableAuthDynamicFeature implements DynamicFeature {

    // Have tried multiple methods to register fitlers: using Injector,
    // using Provider and using the normal Class
    @Inject
    private Provider&lt;AuthFilter&gt; authFilterProvider;

    @Inject
    private Injector injector;

    @Override
    public void configure(ResourceInfo resourceInfo, FeatureContext context) {

        // Logic to decide on registering of filter followed by one of the
        // following depending on injection method:

        context.register(AuthFilter.class);
        context.register(this.injector.getInstance(AuthFilter.class);
        context.register(this.authFilterProvider.get());
    }
}
</code></pre>

<p>Unfortunately my AuthFilter never gets created/run with all of the dependencies that I need.  If I use this.injector or this.authFilterProvider then my @Inject fields are correct by my @Context fields are not.  My understanding is that this is because I'm registering an instance of the object and thus Jersey cannot manage it/inject it's @Context fields.  However when I simply register the class my @Context fields are registered but my @Inject fields are not.</p>

<p>What registration/injection process can I do to ensure that both @Context and @Inject are properly filled at runtime?</p>

<p>One other piece of information potentially useful information: if I register a resource with the following line:</p>

<pre><code>// AKA Dropwizard environment.jersey().register(...);
resourceConfig.register(this.injector.getInstance(MyResource.class));
</code></pre>

<p>And MyResource contains @Context members and @Inject members both the @Context/@Inject members are filled properly at runtime.  So for some reason the Resource registration and Filter registration/management are behaving differently.</p>

<p>Any ideas/insights would be greatly appreciated. </p>