Reputation: 157
According to this previously answered question (link), toInstance
is already an eagerly-loaded singleton but I had an injection of a table dependency which involves the cache update and it is happening only during the first call instead of at service startup. Could someone please tell what I am missing here and how to update the guice injection to make the cache update during startup instead of during the first call?
Upvotes: 1
Views: 1007
Reputation: 35467
Guice's wiki says:
Eager singletons reveal initialization problems sooner, and ensure end-users get a consistent, snappy experience. Lazy singletons enable a faster edit-compile-run development cycle. Use the
Stage
enum to specify which strategy should be used.
PRODUCTION DEVELOPMENT .asEagerSingleton() eager eager .in(Singleton.class) eager lazy .in(Scopes.SINGLETON) eager lazy @Singleton *eager lazy
*
Guice will only eagerly build singletons for the types it knows about. These are the types mentioned in your modules, plus the transitive dependencies of those types.
So when you create your injector, you should decide whether you want a development stage or a production one. The injection of existing singletons happen at the same time as described above.
To do that, create the Injector
like this:
Injector injector = Guice.createInjector(Stage.PRODUCTION, modules);
Upvotes: 1