Issue 751 in google-guice: Guice is injecting members when binding an instance [bind().toInstance()].

4 views
Skip to first unread message

google...@googlecode.com

unread,
May 29, 2013, 3:05:23 PM5/29/13
to google-g...@googlegroups.com
Status: New
Owner: ----

New issue 751 by rpvi...@gmail.com: Guice is injecting members when binding
an instance [bind().toInstance()].
http://code.google.com/p/google-guice/issues/detail?id=751

Hi,

I'm not sure if this is a bug but at least is worth to talk about it.

I noticed that when we perform a bind().toInstance() and the instance is an
annotated class with @Inject etc, Guice will try to fill in those members.

My question is: is that suppose to happen? For me it was a surprise.
Imagine the following use case:

You're running an application service where each service have their own
dependency Guice instance and each of them publish some reference to a
shared JNDI space.

The you need a reference to that service in another and you try to bind
that service interface using bind().toInstance(). Guice will try to fill in
the members that are already filled in. Well this is an issue, but not the
only one. Imagine that each service run in it's classloader and inject
members that are only visible in their context. It will fail saying that no
implementation is bound (in fact that isn't) by the service trying
to "re-inject" all the members again.

Well, again, I'm not saying it is a bug, but if it's not I think it's worth
documenting about this behaviour.

I managed to to what I want using a provider that always returning the same
instance. Is there any other way to do this?

Regards,

Rui


--
You received this message because this project is configured to send all
issue notifications to this address.
You may adjust your notification preferences at:
https://code.google.com/hosting/settings

google...@googlecode.com

unread,
May 29, 2013, 3:12:17 PM5/29/13
to google-g...@googlegroups.com

Comment #1 on issue 751 by mccu...@gmail.com: Guice is injecting members
when binding an instance [bind().toInstance()].
http://code.google.com/p/google-guice/issues/detail?id=751

Yes it is expected:
http://code.google.com/p/google-guice/wiki/Injections#Automatic_Injection

You can use the
http://google-guice.googlecode.com/git/javadoc/com/google/inject/util/Providers.html#of(T)
utility method to skip this automatic injection. Alternatively add a
@Provides method that returns the instance in your module:
http://code.google.com/p/google-guice/wiki/ProvidesMethods

google...@googlecode.com

unread,
May 29, 2013, 3:35:54 PM5/29/13
to google-g...@googlegroups.com

Comment #2 on issue 751 by rpvi...@gmail.com: Guice is injecting members
when binding an instance [bind().toInstance()].
http://code.google.com/p/google-guice/issues/detail?id=751

I see, thanks!

--

google...@googlecode.com

unread,
May 29, 2013, 3:42:47 PM5/29/13
to google-g...@googlegroups.com
Updates:
Status: WorkingAsIntended

Comment #3 on issue 751 by sberlin: Guice is injecting members when binding
(No comment was entered for this change.)
Reply all
Reply to author
Forward
0 new messages