Am 03.12.2011 00:18, schrieb Stuart McCulloch: > On 2 December 2011 14:31, Noctarius <[email protected] > <mailto:[email protected]>> wrote: > > You can use: > > Foo foo = new FooImpl(); > bind(Foo.class).toInstance(foo); > > This is an implicit singleton and you can use the same > instance for > multiple injectors. Alternativly you can use ChildInjectors. > > > One thing to watch out for with "bind...toInstance(...)" is that > the injector will inject any fields/setters marked with @Inject in > the instance. So if you share the same instance between injectors > it could end up being injected multiple times. In some situations > this might be what you want (for example you could use a setter to > add a new element per-injector) but if you want to avoid this then > use: > > bind( Foo.class ).toProvider( Providers.of( instance ) ) > > which protects the instance from being injected by the injector.
Nice hint, thanks for the conclusion. > > Cheers Noc > > Am 02.12.2011 15:00, schrieb Sandro Munda: > > The @Singleton scope does not refer to the Singleton pattern. > > > > According to the "Dependency Injection" book of "Dhanji" : > > > > "Very simply, a singleton’s context is the injector > itself. The > > life of a singleton is tied to the life of the injector. > Therefore, > > only one instance of a singleton is ever created per > injector. It is > > important to emphasize this last point, since it is possible for > > multiple injectors to exist in the same application. In such a > > scenario, each injector will hold a different instance of the > > singleton-scoped object." > > > > Thus, is it possible to share the same Singleton instance > through > > multiple modules and multiple injectors ? > > > > The same question is available on StackOverflow : > > > > http://stackoverflow.com/questions/8356640/guice-how-to-share-the-same-singleton-instance-through-multiple-injectors-modu > > > > Thanks. > > > > Sandro Munda > > <[email protected] <mailto:[email protected]>> > > > > -- > You received this message because you are subscribed to the > Google Groups "google-guice" group. > To post to this group, send email to > [email protected] > <mailto:[email protected]>. > To unsubscribe from this group, send email to > [email protected] > <mailto:google-guice%[email protected]>. > For more options, visit this group at > http://groups.google.com/group/google-guice?hl=en. > > > > > -- > Cheers, Stuart > -- > You received this message because you are subscribed to the Google > Groups "google-guice" group. > To post to this group, send email to [email protected]. > To unsubscribe from this group, send email to > [email protected]. > For more options, visit this group at > http://groups.google.com/group/google-guice?hl=en. -- You received this message because you are subscribed to the Google Groups "google-guice" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/google-guice?hl=en.
