Possible to detect when a binding has been injected to help lazy init?

33 views
Skip to first unread message

Kevin Burton

unread,
Feb 4, 2016, 2:13:41 PM2/4/16
to google-guice
I have a component that loads a fairly large corpus off disk to perform language classification.

We have a few actually.

What I'd like to do is ONLY do initialization if I was actually injected.

I DO NOT want to do initialization it wasn't injected and isn't going to be used.  This should dramatically speed up our unit tests (we have thousands of them and a few seconds here and there adds up)

I can't do lazy init because then my code has additional and non-deterministic latency when first used.  Additionally, it now has to handle exceptions ... 

What I'd really like is an API so that it can detect that it was injected and then only do initialization now that its going to be used by some API.

Is this possible?

Sam Berlin

unread,
Feb 4, 2016, 2:28:09 PM2/4/16
to google-guice

You should be able to use the ProvisionListener API to do this.

sam


--
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/a30b61dd-a488-41b0-89de-467285032abd%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Luke Sandberg

unread,
Feb 5, 2016, 12:38:59 PM2/5/16
to google-guice
Shouldn't you just move the initialization logic into the provider?  assuming it isn't marked @EagerSingleton (or @Singleton and you are in Stage.PRODUCTION), guice will only invoke your provider if someone is actually injecting it

Reply all
Reply to author
Forward
0 new messages