Note that there are some explanatory texts on larger screens.

plurals
  1. POGuice: Cannot inject annotated type in Request scope
    text
    copied!<p>I am attempting to inject an annotated variable into the REQUEST scope:</p> <pre><code>Map&lt;Key&lt;?&gt;, Object&gt; seedMap = ImmutableMap.&lt;Key&lt;?&gt;, Object&gt;builder(). put(Key.get(String.class, Names.named("name")), name).build(); return ServletScopes.scopeRequest(new InjectingCallable&lt;&gt;(injector, GetModule.class), seedMap).call(); </code></pre> <p>Where, InjectingCallable injects GetModule inside the REQUEST scope:</p> <pre><code>/** * A Callable that is constructed in one scope and injects a Callable into a potentially separate * scope. * &lt;p/&gt; * @param &lt;V&gt; the type of object returned by the Callable * @author Gili Tzabari */ public final class InjectingCallable&lt;V&gt; implements Callable&lt;V&gt; { private final Injector injector; private final Class&lt;? extends Callable&lt;V&gt;&gt; delegate; /** * Creates a new InjectingCallable. * &lt;p/&gt; * @param injector the Guice injector * @param delegate the class to inject and delegate to */ public InjectingCallable(Injector injector, Class&lt;? extends Callable&lt;V&gt;&gt; delegate) { Preconditions.checkNotNull(injector, "injector may not be null"); Preconditions.checkNotNull(delegate, "delegate may not be null"); this.injector = injector; this.delegate = delegate; } @Override public V call() throws Exception { return injector.getInstance(delegate).call(); } } </code></pre> <p>GetModule is defined as follows:</p> <pre><code>@RequestScoped private static class GetModule implements Callable&lt;Module&gt; { private final String name; private final Session session; @Inject public GetModule(@Named("name") String name, Session session) { this.name = name; this.session = session; } } </code></pre> <p>When I run this code I get this error:</p> <pre><code>1) No implementation for java.lang.String annotated with @com.google.inject.name.Named(value=name) was bound. while locating java.lang.String annotated with @com.google.inject.name.Named(value=name) </code></pre> <p>If I bind the same variable to the global scope it works. If I remove the annotation, it works. This problem seems to be specific to Request-scoped annotated variables. Any ideas?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload