Forgot to mention, my recommendation would be to inject a Provider[MyObject] instead of the injector as I did in my example. I injected the injector just in case the results were different (i.e. delayed exception); turned out it failed at injector construction time (so it shouldn't matter which you do).
Nate On Thu, Oct 2, 2014 at 8:52 AM, Nate Bauernfeind <[email protected] > wrote: > I did a tiny code test, and it seems like this is already going to fail at > initial start time (note: not compilation time). > > object Test { > import net.codingwell.scalaguice.InjectorExtensions._ > > class FooService > class MyObject @Inject() (foo: FooService) > > class InjectionTest @Inject() (inj: Injector) { > def doTest() = inj.instance[MyObject] > } > > def main(args: Array[String]): Unit = { > val injector = Guice.createInjector(new ScalaModule() { > override def configure(): Unit = { > binder().requireExplicitBindings() > bind[MyObject] > bind[InjectionTest].asEagerSingleton() > } > }) > > injector.instance[InjectionTest].doTest() > } > } > > The error is this (rightly so): > Exception in thread "main" com.google.inject.CreationException: Guice > creation errors: > > 1) Explicit bindings are required and Test$FooService is not explicitly > bound. > while locating Test$FooService > for parameter 0 at Test$MyObject.<init>(Test.scala:22) > at Test$$anon$1.bind(Test.scala:27) > > > Did you test the behavior already, or were you just curious of the > behavior? It seems to work the way you want it to. > > > On Thu, Oct 2, 2014 at 12:33 AM, Kevin Burton <[email protected]> > wrote: > >> I'm curious what the best practice is here. >> >> Let's say that deep in your code you wanted to create a new instance of >> MyObject and pass it a FooService... >> >> One could use an injector to do this.. but what if we actually forgot to >> bind anything to FooService? >> >> You would deploy your application, it would be running just fine for >> months, then all of a sudden you would need FooService and your app would >> fail. >> >> How does one avoid that scenario? >> >> If you used explicitly provided dependencies, your app would fail at >> compilation time. >> >> Now we've postponed to fail at run time.. which is not fun. >> >> Any solutions here? >> >> -- >> You received this message because you are subscribed to the Google Groups >> "google-guice" group. >> To unsubscribe from this group and stop receiving emails from it, send an >> email to [email protected]. >> To post to this group, send email to [email protected]. >> Visit this group at http://groups.google.com/group/google-guice. >> To view this discussion on the web visit >> https://groups.google.com/d/msgid/google-guice/b138aea2-f477-414b-8b36-f3e306ca12d2%40googlegroups.com >> <https://groups.google.com/d/msgid/google-guice/b138aea2-f477-414b-8b36-f3e306ca12d2%40googlegroups.com?utm_medium=email&utm_source=footer> >> . >> For more options, visit https://groups.google.com/d/optout. >> > > -- You received this message because you are subscribed to the Google Groups "google-guice" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To post to this group, send email to [email protected]. Visit this group at http://groups.google.com/group/google-guice. To view this discussion on the web visit https://groups.google.com/d/msgid/google-guice/CAHNex98hm1%2Bg5Pkzo4cK7%2B6x1vN4%2Bnqcgbso3UMZN8uGMo784Q%40mail.gmail.com. For more options, visit https://groups.google.com/d/optout.
