Listening for object instantiation so I can call start() on services.

97 views
Skip to first unread message

Kevin Burton

unread,
Oct 21, 2016, 6:38:11 PM10/21/16
to google-guice
I'm trying to figure out if this is possible but I think I'm looking in the wrong places.

I want to add some syntactic sugar to Guice so that we have AutoService interfaces on our objects that have start / stop methods.

This way I can create a normal Guice binding, create my injector, and then I can have my 'app' call start() in one place which starts all the services.

This way I can have a basic injector to just test bindings, then in production start() my app which will connect to databases, open large files, etc.

The problem I'm having is I need a handle on every instance of a new object guice creeates.

I thought I could do this via BindListener but I only get the class, not that actual instance that was created.

I'm sure I'm just looking in the wrong place so some advice would be super helpful... 

Stephan Classen

unread,
Oct 22, 2016, 4:21:59 PM10/22/16
to google...@googlegroups.com
Are your services singletons? Because if they are not this is not possible because you do not have all instances at application startup.

If yes the BindingListener is sufficient. Because if you have the class you can always get the singleton instance from the injector using the get() method of the injector.

Tavian Barnes

unread,
Oct 23, 2016, 12:06:41 PM10/23/16
to google-guice
This is what ProvisionListener is for.

Andrei

unread,
Oct 27, 2016, 11:37:07 PM10/27/16
to google-guice
Would this also work for scoped instances ? If not, what are recommendations for cleaning / closing resources created in some scope (eg. request) ? 
Reply all
Reply to author
Forward
0 new messages