Call an init method after injection is done

7,172 views
Skip to first unread message

David Nouls

unread,
Feb 27, 2012, 4:53:16 AM2/27/12
to google...@googlegroups.com
Hi,
 
I'm looking for a feature in Guice (and GIN) that allows me to call a method after injection has been done on an object.
I found a blogpost that gives me a solution, but is really a cludge for something that should be somehow part of Guice.
http://cristianvrabie.blogspot.com/2009/11/simulate-bean-initialization-in-gwt.html
 
What I need is some kind of way to mark a method that needs to be invoked when construction, member and setter injections have been performed.
 
I don't want to add some maybeInitialize method and call that from everywhere in the object since that will bring in multithreading issues.
 
I can not/do not want to use constructor injection because in my case I have a baseclass that need injections but I don't want to polute the derived classes to also have to call super( ... ). This would basically expose the internals of the root class.
 
David
 
 

scl

unread,
Feb 27, 2012, 7:52:26 AM2/27/12
to google...@googlegroups.com
Hi David

You could try to use an injector listener (http://google-guice.googlecode.com/svn/trunk/javadoc/com/google/inject/spi/InjectionListener.html)

Take the example from customer injection (http://code.google.com/p/google-guice/wiki/CustomInjections) but instead of registering a MembersInjector you would register a InjectoListener.



David Nouls

unread,
Feb 27, 2012, 8:38:24 AM2/27/12
to google...@googlegroups.com
Hi scl,
 
I don't like the idea that a method that must be invoked on a specific implementation needs to be separately bound in a module.
 
A feature with @afterInjection would be a better solution.
 
David

Marko Lavikainen

unread,
Feb 27, 2012, 8:52:00 AM2/27/12
to google-guice
This is probably your answer:

http://code.google.com/p/mycila/wiki/MycilaGuice

On 27 helmi, 11:53, David Nouls <david.no...@gmail.com> wrote:
> Hi,
>
> I'm looking for a feature in Guice (and GIN) that allows me to call a
> method after injection has been done on an object.
> I found a blogpost that gives me a solution, but is really a cludge for
> something that should be somehow part of Guice.
>
> http://cristianvrabie.blogspot.com/2009/11/simulate-bean-initializati...
> <http://cristianvrabie.blogspot.com/2009/11/simulate-bean-initializati...>

David Nouls

unread,
Feb 28, 2012, 3:53:05 AM2/28/12
to google...@googlegroups.com
Hi,
 
That will not work with GIN/GWT.
 
David

dg

unread,
Mar 3, 2012, 6:54:28 AM3/3/12
to google-guice
Guice does not do object lifecycle. It's a thing. AFAIK Guice (by
design) does not ship @AfterInjection/@PostConstruct or similar
things, but provides you the tools to implement that. I am not sure
of the restrictions imposed by GIN/GWT.

So, If injection listener & friends don't work for you then you have
other options.

(Option 1) Use a provider method to fake it. Bind the real instances
with an annotation and use a provider method to intercept the
constructed & injected class before it is delivered to your
application. You can also write your own provider which does the same
thing. Consider in your module

@Provides Foo provideFoo(@Named("module-internal) Foo foo) {
foo.init();
return foo;
}

(Option 2) Guice specifies the order that it does injection. I
believe it's constructor, member, method. So if you were comfortable
using member injection in your super class, then just mark your init
method with @Inject and you should be good to go. Something like

@Inject init(String s) // maybe the string is not required.

Of course this only guarantees that the superclass injection is done.
If you have methods in derived classes also annotated with @Inject, I
don't believe there is a defined order (although there may be).

-dg

Adrian Cole

unread,
Mar 3, 2012, 11:08:47 AM3/3/12
to google...@googlegroups.com
Considering normally, init methods perform some sort of heavy stuff,
I'd recommend against using providers for this. In jclouds, we
internally wire up @PostConstruct, and add a runnable to call said
methods to an executionlist, which is invoked after the injector is
created. This seems to be very similar to Mycilia, which looks quite
handy.

-A

> --
> You received this message because you are subscribed to the Google Groups
> "google-guice" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/google-guice/-/Qzqir1dQgvUJ.
> To post to this group, send email to google...@googlegroups.com.
> To unsubscribe from this group, send email to
> google-guice...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/google-guice?hl=en.

Christian Edward Gruber

unread,
Mar 6, 2012, 4:00:00 PM3/6/12
to google...@googlegroups.com
A common way to do this is to use a multibinder to bind services, and have a service manager which starts up services post-injection. You can't easily follow the dependency graph without digging into the SPI, but you could use a MapBinder to declare services and what services they need to be started before they can start, and manually walk that graph.

Or provide an ordinal - a little like the way inetd does it, with level zero services starting before level one services, etc.

Having said that, I'm looking at how lifecycle has been implemented in other containers, or in a guice fork and whether this can/should be brought into the main guice tree. Zero promises that this will be part of Guice, but it's on my list of improvements to examine.

Christian.

Simone Tripodi

unread,
Oct 31, 2012, 5:07:48 AM10/31/12
to google...@googlegroups.com
Hi David,

have you ever had a look at
http://99soft.github.com/lifegycle/after-injection.html ?

HTH,
-Simo

http://people.apache.org/~simonetripodi/
http://simonetripodi.livejournal.com/
http://twitter.com/simonetripodi
http://www.99soft.org/
> --
> You received this message because you are subscribed to the Google Groups
> "google-guice" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/google-guice/-/zbX_giSnsnQJ.
Reply all
Reply to author
Forward
0 new messages