-
Notifications
You must be signed in to change notification settings - Fork 1.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Lifecycle support #62
Comments
From dhanji on March 14, 2007 16:36:02 Couldnt That being said, I feel that lifecycle methods are a legacy of setter-injection and It's also significant (different from guice) that |
From peter.royal@pobox.com on March 14, 2007 16:43:44 lifecycle (which is what this is) will certainly be needed. +1 to hani's JSR-250 suggestion. (and its bogus to say that cxtor injection is now "on par" with setting injection. picocontainer has been doing |
From hani@formicary.net on March 14, 2007 17:39:38 Nooo!
I do think that |
From dhanji on March 14, 2007 18:27:15 proper lifecycle is much more of an app-server/container issue. IMO I think lifecycle yes I am well aware pico did ctor injection long before anyone else, that's not what |
From dhanji on March 14, 2007 18:29:00 yea come to think |
From kevinb9n on March 15, 2007 07:55:42 I am also deeply suspicious of the entire idea of "initialization" as separate from That said, I can imagine some real value in an object being notified when its Scope I wonder if this could be accomplished by having Guice or the Scope detect that your Status: Accepted |
From hani@formicary.net on March 15, 2007 07:58:59 Here's a very simple example. You have field injection, and would like to do some init work. You can't do it in the ctor since the fields can't be injected until after the ctor has run. If you want concrete 'init' work, imagine I want to cache some data from an injected DataSource, if it's a field, |
From kevinb9n on March 15, 2007 11:04:51 Stock answer: then don't use field injection? Now I need more meat to the example to |
From crazyboblee on March 15, 2007 14:03:26 I'm inclined to lean on garbage collection for finalization logic. Or to delegate We really need an external post-injection/pre-scoping hook. Then you could do
Maybe something like this: interface ProviderInterceptor<T> { Then you could apply them like this: void bindInterceptor(Matcher<Key<...>> keyMatcher, ProviderInterceptor<...> interceptor); |
From dhanji on March 15, 2007 17:45:01 Im with kevin--use ctor injection its safer (final publication), better (obvious to the only reason I use field inj is convenience/laziness. gc is probably a different stage of the lifecycle than closing/shutting down--we have That having been said I still think proper lifecycle should be managed by an |
From crazyboblee on March 15, 2007 17:54:09 I think we need more use cases. I've never actually needed to run code when the |
From dhanji on March 15, 2007 18:00:38 I suppose ctor injection would be out if you were "reinjecting" an object (with |
From dhanji on March 15, 2007 18:05:21 You may want to release resources on hotdeploys (like datasources)--which REALLY I dont really like the idea of lifecycle in DI anyway. |
From james.strachan on March 16, 2007 00:04:08 Here's a use case. Being able to DI any POJO developed for use in Spring or EJB3. There's a ton of code If you're writing really simple POJOs which only have a set of mandatory However as soon as you have complex objects with multiple optional properties which The great thing about the init method is its called by the IoC container in context |
From crazyboblee on March 16, 2007 08:23:24 Sorry, I meant use cases for the "tear down"/"stop" side of things. The general ProviderInterceptor mechanism I suggested above should be able to support |
From james.strachan on March 17, 2007 00:29:22 OK the common use case in EJB3 or Spring POJOs is to close down expensive resources e.g. its fairly common in web applications to have some singleton POJOs which need This could be implemented using a provider interceptor; calling a stop method is just So to nicely support Spring POJOs we'd just look for DisposableBean interface; for |
From kevinb9n on March 19, 2007 11:31:12 The only action we plan to take on this is to resolve issue 78 . After we do that we Owner: crazyboblee |
From ekuleshov on April 20, 2007 14:22:04 Tear down/close hooks can be very useful for session-scoped beans that may hold |
From stolsvik on June 05, 2007 09:47:12 I have this "Spring-based" swing application that I'd like to convert to Guice, as However, right after ditching Spring from the classpath to start my convertion, I Is there a way to get hold of all objects that Guice have instantiated? Is it Basically, I'd like a Inject.destroy()/stop() method - but I'd be happy to make it |
From crazyboblee on June 05, 2007 09:55:29 Don't you have some root object you could just throw away and re-create? Is there If not, construction listeners (coming in the next release) will do what you need. |
From stolsvik on June 05, 2007 12:06:43 .. okay, listeners sounds good! When it comes to garbage collection, I really don't like to rely on that for Googling "finalization java" gives several articles pointing out this fact, e.g. (Really a mailinglist question, but..) Is there a timeframe for next version? Is |
From crazyboblee on June 05, 2007 13:07:41 You should release a critical resource in a finally block in close proximity to where We don't have a concrete date for the next release yet, but it won't be long. |
From kevinb9n on June 05, 2007 13:59:00 In other words, no object should ever hold a resource "occupied" between method |
From crazyboblee on June 05, 2007 14:25:51 I wouldn't say that. More detailed use cases would help. |
From peter.kitt.reilly on June 05, 2007 15:17:06 If objects are created in a scope, it only The "classic" example is an object grabbing an expensive |
From stolsvik on June 06, 2007 17:40:48 crazyboblee: so I should make my database pool where I need a database connection, It is of course possible to handle all external resources ("heavy Closables") (.. but so far I like it real good! XML-hell be gone!) |
From crazyboblee on June 06, 2007 18:09:05 Ha ha. I was talking about releasing connections for example, but why does Guice need We're trying to find a solution to a problem we haven't really defined. |
From stolsvik on June 08, 2007 06:27:11 bob: i think the thing here is "in and out of scopes", like kevin refers to comment 6. If I'm instantiated within some scope, I'd possibly like to know when that scope is However, there is a difference between singleton and instance "beans" (objects) here. This goes hand in hand with the logic that states that "don't clean anything other Thus basically, if only Guice somehow would let me known which objects it's currently |
From stolsvik on July 11, 2007 06:00:47 As a further point towards init() methods. I just got myself Brian Goetz's "Java Concurrency in Practice". In chapter 3.2.1 Brian suggest as a solution "expose a start() or initialize() method...", or make the |
From stolsvik on July 11, 2007 12:47:54 PS: I just realized that this bug concerns two entirely different concepts: the JSR I HIGHLY vote for the JSR 250 annotations being honored, natively, by Guice. One (I also vote for the lifecycle methods (obviously, given my comments).) |
From endre.stolsvik on March 14, 2012 19:46:47 Quick question: Have Guice gotten intelligent about the order in which it instantiates objects? Earlier, it just did one or the other random order, not caring about each instance dependencies, creating those annoying proxies when it got to any dependency it didn't already have available. Then one got the option of specifying that proxies should not be used. However, the order hadn't gotten any smarter, so one still could arbitrarily crash into the ordering-problem, only now it stops with exception, not creating the proxy. This even though a simple reordering of the flow would have fixed it. First create the leafs; the ones without dependencies. Then work your way up: Create instances whose dependencies at this point be fulfilled. If you at some point iterate through the entire list of "to be instantiated" w/o instantiating any, then you have a dependency-loop. Loops are only evaluated by constructors. Fields and methods can always be injected afterwards. .. which leads to my point #1 about stuff like |
From endre.stolsvik on March 14, 2012 19:52:41 You need This because you might need some service of some injected instance to start up. And that instance was injected by means of field injection. No good with constructor startup then. Also, according to JCIP IIRC, one shall not leak unfinished objects. In a constructor, you could e.g. register yourself with the OnIncomingUserEventService, and then your object instantiation crashes with a NPE for some other reason. Therefore, thou shall not ever do "startup stuff" in a constructor. Only construct yourself, do not e.g. attach yourself to other object graphs. |
From endre.stolsvik on March 14, 2012 20:12:46 When you folks create features out of this request, please enable me to do custom initialization. I have always ended up in needing several initialization phases, not only a single What I've needed, is at least a "now you've got all your dependencies and configuration, do your init code", and then a "now you can do listener registration with each other". So my ideal solution to this is to get a list (preferably in instantiation order) so that I can invoke my different init methods on the relevant objects. This could be fetched by user code when the injector was finished setting up the entire graph (and have invoked What I currently do, is to have a LifeCycle service which instances can depend on. There is also the LifeCycled interface. The objects register with the LifeCycle service, either in constructor (which is bad according to JCIP), or in the method injecting the LifeCycle service (better). After injector is finished, I run through all the instances that have registered with the LifeCycle service, all of which shall implement the LifeCycled interface. I then invoke method "phase1" on all objects, then method "phase2" on all objects, etc. (On application shutdown, I have corresponding phase-destroy methods on the LifeCycled interface, which are invoked in the opposite order). |
From noctariushtc on March 14, 2012 22:17:02 It would be nice to see some help in Guice to build a service framework but it isn't needed, since startup order of services could be possibly different from injection dependecy order. In my framework I made it something like Bob meantioned. I used an the InjectionListenr / ProvisionListener to collect services implementing the frameworks service interface and after injection finished the frameworks ServiceManager collects and orders the services startup dependencies, searches for postConstruct() handlers and finally calls the startup() method of every service. It was a bit tricky at some small points but that is more a problem of the framework, which can be sticked together from plugins (all have their own AbstractModule) and a plugin descriptor, not a Guice problem. |
From limpbizkit on March 14, 2012 22:17:18 PROPOSAL 102. Add an extension that uses Guava's Service interface, and that makes it easy to bind services and start them. Perhaps a basic service registration DSL: Plus a mechanism to start and stop services. This could track user-specified service dependencies. public static void main(String... args) { ServiceManager serviceManager = injector.getInstance(ServiceManager.class); Ideally ServiceManager has a rich API that lets you do all the natural things you want to do with services. Stuff like starting them up, shutting them down, listening for state changes, printing statistics like startup time, and interrogating the dependency graph. It could even do its own logging (in a verbose mode?) to keep the common case concise. |
From noctariushtc on March 15, 2012 00:43:17 A nice concept but isn't this the first step to make Guice a second Spring? Guice wasn't designed as an application framework, I'm not sure if it would be good to go that way. |
From marchign on March 15, 2012 14:16:48 Using a marker interface or annotations makes no great difference to me. I think that the reason why people (including me) request this feature is because it provides a simple machinery to forward the "scope begin" / "scope end" events to objects in that scope without requiring any dependency among who starts/stops the scopes and the object living there. Following this view, I think that it would make sense to generalize the eagerness attribute to each scope: instead of bind(X).to(Y).asEagerSingleton(), one could use something like bind(X).toY().eager().in(Singleton.class). In this way one could perform some initialization phase at (session / request / whatever ) start time. I think that this extension would also make less arbitrary the choice of which objects Guice should starts and stop. The user could force an object to be initialized (and destroyed) by Guice at scope beginning (end) simply by binding it eagerly in that scope. |
From cowwoc@bbs.darktech.org on March 15, 2012 14:44:18 Regarding annotations vs marker interfaces, why would you need to iterate over the methods? Wouldn't you only scan the class itself for an annotation (same as a marker interface)? |
From cgruber@google.com on July 16, 2012 11:03:09 Unclear whether this needs to be in core, or can be fully expressed in an extension, but if it is to be, we need to expand the SPI to support it, as I think it would incur a cost on those who didn't need it if it were in core. For now, I'm putting this into a "New" component in the issues, as we figure out how best to do this. Status: Acknowledged |
From antony.stubbs on November 07, 2012 06:40:24 You guys might want to look at GuiceBox's |
From davide.cavestro on May 10, 2013 13:21:30 Please note this is blocking even GWT/GIN |
From goktug@google.com on May 10, 2013 16:23:59 Clarification: I said "I don't see this [ We might end up having extensions points some time in the future and then one can write extension similar to that - but that is a different story... |
From mcculls on May 12, 2013 15:06:28 Support for |
From davide.cavestro on May 12, 2013 23:20:57 so, is lifegycle (and generally speaking guice extensions) directly compatible with GIN? |
From mcculls on May 14, 2013 03:16:51 GIN only uses Guice at compile time, so additional work is required to support extensions - for example, assistedinject and multibinder have both been adapted in GIN (but the API/design is based on the original extension). See the various submodules under https://code.google.com/p/google-gin/source/browse/#svn%2Ftrunk%2Fsrc%2Fcom%2Fgoogle%2Fgwt%2Finject%2Fclient So while the API and code to support lifecycles is available, it will need to be adapted for GIN. Whether this is done by porting and re-implementing the underlying TypeListener/ProvisionListener API on top of GIN's GWT runtime, or by looking at lifegycle's higher-level design and porting that, is a question for the GIN folks. (PS. the original home for lifegycle was http://99soft.github.io/lifegycle/ before it moved to Apache/Onami) |
From cowwoc@bbs.darktech.org on December 29, 2013 11:49:16 Bob, In comment #93 you said "Maybe I don't want the object to be closed." When I asked you for a concrete use-case you responded with some general statement about avoiding unexpected behavior.
|
From b.k.oxley on January 04, 2014 22:13:28 This would be great & simple functionality to fold into Guice. Lifegycle uses a simple type listener & module to implement, so the is optional, a good candidate for guice extensions. Simply supporting " |
+1, any plan to support this feature? |
At the least, it would be great to have an official "We don't have this implemeted yet, but if you want to shutdown cleanly, do this." |
We have no plans of implementing lifecycle support. You can use Guava's For things where you want to do something after creation/injection (otherwise known as provisioning), you can use a Guice has no concept of "finishing" a scope or object, so anything where you want to do something on-destroy must be done through a separate framework that's better suited to lifecycle management. |
From hani@formicary.net on March 14, 2007 09:22:31
Support for JSR-250 (javax.annotation) would also be very useful for all those people using init()
methods and the list.
The mapping I think is:
@
PostConstruct -> Called right after construction@
Resource -> Analogous to@
Inject@
PreDestroy -> No concept of destruction, is there?@
Generated -> Doesn't applyOriginal issue: http://code.google.com/p/google-guice/issues/detail?id=62
The text was updated successfully, but these errors were encountered: