On 9 Feb 2013, at 18:26, [email protected] wrote:

> I'm currently developing a web framework based on Guice + Guice Servlet.
> 
> I have a component that needs to dynamically check to see if a Provider has 
> been binded for a given type. To do that, I inject the Injector in its 
> constructor.
> 
> But it seems that if I only inject the Injector itself, the Injector isn't 
> able to get a Provider :
> 
> ------------
> public class MyComponent
> {
>     @Inject
>     public MyComponent(Injector injector)
>     {
>         // Throws a ConfigurationException exception :
>         Provider<?> provider = injector.getProvider(HttpServletRequest.class);
>     }
> }
> 
> ------------
> 
> But if I inject any other dependency at the same time, then the Injector 
> works :
> 
> ------------
> public class MyComponent
> {
>     @Inject
>     public MyComponent(AnotherComponent anotherComponent, Injector injector)
>     {
>         // Works!!
>         Provider<?> provider = injector.getProvider(HttpServletRequest.class);
>     }
> }
> 
> ------------
> 
> I'd really like to understand this behavior! Any help?


Could you paste the ConfigurationException stack trace? The only thing I can 
think of right now is perhaps adding the component changes the injection order 
and that has a side-effect in your particular setup (such as with respect to 
just-in-time bindings). BTW, are you just using a single injector or do you 
have any child injectors?

You might also want to look at using getExistingBinding which doesn't throw a 
ConfigurationException for missing bindings (it returns null instead).

--
Cheers, Stuart


-- 
You received this message because you are subscribed to the Google Groups 
"google-guice" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/google-guice?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to