-
Notifications
You must be signed in to change notification settings - Fork 1.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Difference between @Singleton and .asEagerSingleton() #1834
Comments
This is, actually, a consequence of the problem you already reported to guicey. I did not see hangs in the second case, but the problem should be caused by two service instances, created in the second case. In short, Long version: Private module declaration: public class MyGuiceModule extends PrivateModule {
@Override
protected void configure() {
bind(Sample.class).to(TheProblemClass.class).asEagerSingleton();
expose(Sample.class);
}
} Guicey detects target class ( binder().bind(TheProblemClass.class); In order to register extension, guicey requests its instance: injector.getInstance(TheProblemClass.class); This would cause instance creation from the unetrgetted prototype binding. @Inject
public SampleResource(Sample sampleProblem) {
this.sampleProblem = sampleProblem;
} To verify this just add simple constructor to the TheProblemClass:
Without
With singleton annotation, there would be only one instance:
|
@xvik Thanks for the detailed explanation. So there are a few issues:
What I understand is that having |
Normally, guicey should detect already declared binding and did not register another one.
The problem is that there is actually two bindings, so In context of guicey, using
|
I think this is where we had seen non-reproducible behaviour where we managed to register two instances. For now, I suspect the right thing to do is use |
Hello,
I have a project where I see my service is failing to initialise.
I am using dropwizard guicey - but it uses Guice underneath, and my question is specific for Guice because the dependency injection is the theme here.
I am attaching the project in question. So that the issue can be reproduced and seen.
The class -
TheProblemClass
extendsAbstractScheduledService
(Guava) and implementsManaged
interface in Dropwizard.The idea is that I ought to be able to start/stop the service upon Main server startup and shutdown respectively. Also, I could schedule a periodic call to one of its methods.
Some things I have noticed.
TheProblemClass
is annotated with@Singleton
- it is working fineTheProblemClass
is NOT annotated with@Singleton
- it is always hanging.In my Module - I have bound it as an eager singleton. My understanding was that I only needed to do this in one place. And the rest should be OK. Have I misunderstood how Singleton Annotation and Module Configs are supposed to be working together?
Archive.zip
The text was updated successfully, but these errors were encountered: