Is there a way to inject a class's correct key?

23 views
Skip to first unread message

glenviewjeff

unread,
Jun 4, 2012, 2:33:23 PM6/4/12
to google...@googlegroups.com
I would like to a class to request an instance of the key used to bind the object itself.  I believe I need to do this to support serialization so that I grab the correctly bound object upon reconstruction.

I thought this would work if I did something like the following:

class Foo {
   @Inject Foo(Key<Foo> fooKey) {}
}

But I got a binding error:

Error injecting constructor, java.lang.RuntimeException: Missing type parameter.

Is this possible, or would I have to make a custom provider that uses a setter to set they key after the object is instantiated?


Fred Faber

unread,
Jun 5, 2012, 6:12:14 AM6/5/12
to google...@googlegroups.com
Generally speaking, no.

I've implemented what you described by using a custom Provider that maintains a map of Key => instance.  This also involved understanding the bindings (so that the custom Provider would be used to create the serializable instances), and making sure that all required dependent bindings were available at the other end of the wire.

Fred




--
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/-/Cm0sGg1tP1MJ.
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.

Reply all
Reply to author
Forward
0 new messages