1

The following map binder binding gives me a com.google.inject.OutOfScopeException: Not in MyScope:

mapbinder.addBinding("ferrari").to(Ferrari.class).in(Singleton.class); 

I am guessing this is probably because the constructor of Ferrari has dependency on an object which is annotated with @MyScope. How can I create a Ferrari Singleton in such as case ?

I know that (how to create an object from a different scope) to create object of different scope, we need Providers. If that is the case what should I write in the get method of FerrariProvider so that it always returns me the same instance of Ferrari ? What ways do I have to create a Ferrari Singleton which depends on an object in a different scope ?

Community
  • 1
  • 1
Crusaderpyro
  • 2,163
  • 5
  • 29
  • 53

1 Answers1

1

You can add the singleton annotation to your provider and cache to-be-returned result of the get method in a field and return that. So every time the provider is called, it will return the same object. You will also be able to inject dependencies regardless of their scopes in your provider.

Nektie
  • 94
  • 9