Is it possible to prohibit an injection?

27 views
Skip to first unread message

glenviewjeff

unread,
Jul 19, 2012, 12:20:17 PM7/19/12
to google...@googlegroups.com
Also posted on Stack Overflow

I'm using a couple of different injectors and I want to ensure I don't accidentally access a class that's bound automatically from an injector, especially since its scope is important.

Is there a way I can force an automatic binding to fail, or at least report a run-time error?

For instance if I have a method Foo that is "illegally" requesting a Bar:

class Bar { @Inject Bar() {} } // Shouldn't be accessible in Foo's injector
class Foo { @Inject Foo(Bar bar) {} } // Binding should fail here

Sam Berlin

unread,
Jul 19, 2012, 12:24:10 PM7/19/12
to google...@googlegroups.com
Binder.requireExplicitBindings


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

glenviewjeff

unread,
Jul 19, 2012, 3:54:19 PM7/19/12
to google...@googlegroups.com
Thanks Sam, but doesn't that force explicit bindings for all classes?  I take it there's no way to prohibit bindings to just a handful of classes?  I suppose I could make a provider method that throws a runtime exception?


On Thursday, July 19, 2012 11:24:10 AM UTC-5, Sam Berlin wrote:
Binder.requireExplicitBindings

On Thu, Jul 19, 2012 at 12:20 PM, glenviewjeff wrote:
Also posted on Stack Overflow

I'm using a couple of different injectors and I want to ensure I don't accidentally access a class that's bound automatically from an injector, especially since its scope is important.

Is there a way I can force an automatic binding to fail, or at least report a run-time error?

For instance if I have a method Foo that is "illegally" requesting a Bar:

class Bar { @Inject Bar() {} } // Shouldn't be accessible in Foo's injector
class Foo { @Inject Foo(Bar bar) {} } // Binding should fail here

--
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/-/dPLIkgQMVngJ.
To post to this group, send email to google...@googlegroups.com.
To unsubscribe from this group, send email to google-guice+unsubscribe@googlegroups.com.

Sam Berlin

unread,
Jul 19, 2012, 4:14:18 PM7/19/12
to google...@googlegroups.com

Ya, that sounds right.
sam

To view this discussion on the web visit https://groups.google.com/d/msg/google-guice/-/VBkey9i7yKYJ.

To post to this group, send email to google...@googlegroups.com.
To unsubscribe from this group, send email to google-guice...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages