I found an behavior that I wanted to explore a bit, mainly because it was 
revealed as a bug when I changed from a default binding to a Provider. Here is 
my class:

@Singleton
public class MyClass implements MyInterface {
...
}

I originally had this bound via @ImplementedBy and/or a module binding like 
this:

bind(MyInterface.class).to(MyClass.class);

I switched this to a Provider like this:

public class MyProvider implements Provider<MyInterface> {
...
}

and my new binding:

bind(MyInterface.class).toProvider(MyProvider.class);

The issue after this refactor was that the Singleton was now lost because my 
provider was returning a new instance each time. Therefore, I was creating new 
instances each request and this quickly ran out of memory (my class is a 
cache). 

I made the assumption that since the class was annotated with Singleton it 
would be placed in that scope after the provider created it. This was not the 
case. Is this expected or something that should be handled to allow for these 
types of refactoring?

I'm inclined to think that Providers shouldn't maintain singleton instances, 
although that might be the intention of them in this case. I'm also inclined to 
think that Singleton annotations should be respected regardless of the binding 
method.

Thoughts?

-bp

-- 
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