Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The problem is that you don't have a binding for this type. Just because you are explicitly seeding the value does not mean you don't have to bind it. you could say:</p> <pre><code>bind(String.class) .annotatedWith(Names.named("name")) .toProvider(Providers.&lt;String&gt;of(null)); </code></pre> <p>and then if the <code>name</code> variable has the value <code>"foo"</code>, you will get <code>"foo"</code> injected because you're seeding it. Seeding a value places it in the scope (which is just a cache) so that Guice won't run the provider of the value. By using a provider of null you can just let the value blow up if it's not seeded.</p> <p>In short, Guice requires that you specify a way to provision every dependency, regardless of whether you plan to manually seed scopes (which should be a fairly rare thing btw).</p> <p>Some unsolicited advice: - Please avoid injecting the injector. It makes catching these kinds of problems harder. It's best to have a single "root object". This is the single object you need to call <code>injector.getInstance</code> to create. For a lot of applications, this can just be your application server. (e.g. - <code>injector.getInstance(MyServer.class).startServer()</code>). Why does this help you? It makes it easier to detect at startup that all your dependencies are satisfied. If you inject the injector during a request and can call it to create arbitrary objects, you run the risk of getting some provision error due to a missing binding much later during runtime. Also if you do all your getInstance calls early on, it's easier to write tests that do this for you so that you can simply run a test to know that your Guice bindings are satisfied.</p> <p>UPDATE:</p> <blockquote> <p>If I bind the same variable to the global scope it works.</p> </blockquote> <p>Hmm, did you basically do what I did? If so, my explanation above explains why that works :-).</p> <blockquote> <p>If I remove the annotation, it works.</p> </blockquote> <p>The reason this works is because Guice does have a binding for <code>String</code> since <code>String</code> has an empty constructor :-). Basically you have to have a single <code>@Inject</code>-able constructor, a no-arg constructor, or a provider to bind a type.</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