Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I lifecycle managed object in a Dagger graph

I can't see any support for life-cycle management in Dagger. Only DI, and nothing for @PostConstruct or @PreDestroy.

I'd like Dagger to start/stop object is the graph in the same manner Spring does. How can I do this?

like image 202
Alex Collins Avatar asked Aug 17 '13 16:08

Alex Collins


1 Answers

The life-cylce of objects in the graph is the same as the life-cycle of the graph itself. As soon as the graph gets garbage collected, so will all objects within the graph.

I accomplish this by creating sub-graphs in Dagger. The sub-graph will contain all objects (singletons within that graph) that require a shorter life-cycle. Application-wide objects go in the main graph which the sub-graph is dependent on.

PS. I am not familiar with Spring so I can't explain how this relates to Spring.

like image 135
Jeroen Avatar answered Sep 27 '22 18:09

Jeroen