I have tried that remove the second binding from the Module and use
injector.getProvider(x), but I still get different instance. See my
code below:

Injector injector = Guice.createInjector(new MouseActionModule());

                mouseActionFactoryProvider = injector.getProvider(
                                MouseActionFactoryProvider.class).get();
                System.out.println(mouseActionFactoryProvider.get());
                System.out.println(mouseActionFactoryProvider.get());
                System.out.println(mouseActionFactoryProvider.get());
                System.out.println(mouseActionFactoryProvider.get());

Each time I call mouseActionFactoryProvider.get(), I will get the new
instance.

On Aug 15, 10:55 am, Max Bowsher <[email protected]> wrote:
> zhaoyi wrote:
> > This is my code. Whenever I call mouseActionFactoryProvider.get(), it
> > will return create an instance. It seems that the @Singleton doesn't
> > work here.
>
> > @Singleton
> > public class MouseActionFactoryProvider implements
> > Provider<MouseActionListenerFactory> {
>
> >   �...@override
> >         @Singleton
> >    public MouseActionListenerFactory get() {
> >            return new MouseActionListenerFactory();
> >    }
> > }
>
> Scope annotations apply to providers as a whole, annotating the get()
> method with @Singleton is erroneous and ignored.
>
> > public class MouseActionModule extends AbstractModule {
>
> >   �...@override
> >    protected void configure() {
> >            bind(MouseActionListenerFactory.class).toProvider(
> >                            
> > MouseActionFactoryProvider.class).in(Singleton.class);
> >            bind(MouseActionFactoryProvider.class).in(Singleton.class);
> >    }
>
> > }
>
> What you have done here is:
>
> 1) Bind MouseActionListenerFactory, in singleton scope, to be produced
> via your Provider.
>
> 2) ENTIRELY SEPARATELY bind a different instance of the Provider as a
> second singleton.
>
> > public static void main(String []args){
> >                 Injector injector = Guice.createInjector(new
> > MouseActionModule());
> >            mouseActionFactoryProvider = injector
> >                            .getInstance(MouseActionFactoryProvider.class);
>
> > }
>
> And here you retrieve the singleton created by the second binding. Any
> calls you make to its get() method are NOT MANAGED by Guice, so each one
> runs the get() method afresh.
>
> You should REMOVE the second binding from your Module, and you should
> call injector.getProvider(x) in the case above where you are currently
> calling injector.getInstance(x).
>
> Max.
>
>  signature.asc
> < 1KViewDownload
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to