I have a class i wan't inject
For legacy code support it is like this.
@Singleton
public class Applications extends AbstractModule {
private Applications() {}
private static class ApplicationsLoader {
static final Applications INSTANCE = new Applications();
}
@Override protected void configure() {}
@Provides @Singleton
public Applications get() {
return ApplicationsLoader.INSTANCE;
}
public static Applications getApps() {
return ApplicationsLoader.INSTANCE;
}
}
On application startup , I create injector.
public void startup() throws Exception {
initLogging();
Injector injector = Guice.createInjector(Stage.PRODUCTION, Applications.getApps());
}
but some while after, when I need Applications.class i always get a null
public class Blob {
@Inject private Applications applications;
.....
}
Any suggestions how to share Applications.class singleton?
injector.getInstance(Applications.class) working fine.
Regards,
Valentin.
The question is:
How are you creating the instance of Blob?
Most likely the instance of Blob was not created by guice. This
happens when you call "new". In order to have guice create the
instance you must do either one of the following:
- ask the injector for an instance --
injector.getInstance(Blob.class)
- have Blob injected into another class.
To avoid such situations it is always a possibility to use constructor injection instead of field injection:
public class Blob {
private final Applications applications;
@Inject
Blobb(Applications applications) {
this.applications = applications;
}
.....
}
--
You received this message because you are subscribed to the Google Groups "google-guice" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-guice...@googlegroups.com.
To post to this group, send email to google...@googlegroups.com.
Visit this group at https://groups.google.com/group/google-guice.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-guice/d02fdaa7-59e9-4b4e-8f47-4a43a04d53db%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-guice/6e966ba5-10cf-59a1-dad4-c604df112767%40gmx.ch.