Disposing objects created through @Provides

328 views
Skip to first unread message

Anurag Laddha

unread,
May 27, 2015, 3:25:34 AM5/27/15
to google...@googlegroups.com
Greetings,

I am creating objects in my application using following approach (using @Provides): https://github.com/google/guice/wiki/ProvidesMethods

public class SomeClassProvider extends AbstractModule {

   @Override
    protected void configure(){
    }

   @Provides
   @Singleton
    SomeClass getSomeClass(){
        //Create object of SomeClass using new SomeClass()
        return someClassObject;
    }
}

I access the instance of SomeClass using injector.getInstance(SomeClass.class). 

(I know this is not the way Guice must ideally be used. I should be injecting SomeClass wherever it is needed.
I am using this approach since i cannot inject non serialisable objects in the constructor of the class where SomeClass needs to be used. SomeClass may be a non serialiazable class)

Anyways, I want to know how do i dispose instance of SomeClass when the application shutsdown?

Thanks!
Anurag

Robert Voliva

unread,
May 27, 2015, 8:25:29 AM5/27/15
to google...@googlegroups.com
There was a good response to a similar question a couple of weeks ago:

Vyacheslav Rusakov <vya...@gmail.com>

May 14 (13 days ago)
to google-guice
If you just want spring-like @PostConstruct and @PreDestroy methods support then there are many libs that implements it like https://github.com/mycila/guicehttps://github.com/Netflix/governator etc.

In short, you should avoid implicit bootstrap logic and do some special lifecycle service with start/stop methods which should be called from outside world.
For example, in web application it could be servlet context listener which will call start / stop.

--
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 http://groups.google.com/group/google-guice.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-guice/f64d007e-f4ab-4553-8b08-e5655a3dd9e2%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Anurag Laddha

unread,
May 31, 2015, 2:09:04 AM5/31/15
to google...@googlegroups.com
Thanks Robert. Will explore these options.
Reply all
Reply to author
Forward
0 new messages