स्कैला में, क्या मैं Guice का उपयोग स्कैला object
एस इंजेक्ट करने के लिए कर सकता हूं?क्या गिला स्केल ऑब्जेक्ट इंजेक्ट कर सकते हैं
उदाहरण के लिए, क्या मैं निम्नलिखित ऑब्जेक्ट में s
में इंजेक्ट कर सकता हूं?
import org.junit.runner.RunWith
import org.scalatest.WordSpec
import org.scalatest.matchers.MustMatchers
import org.scalatest.junit.JUnitRunner
import com.google.inject.Inject
import com.google.inject.Module
import com.google.inject.Binder
import com.google.inject.Guice
import uk.me.lings.scalaguice.ScalaModule
@RunWith(classOf[JUnitRunner])
class GuiceSpec extends WordSpec with MustMatchers {
"Guice" must {
"inject into Scala objects" in {
val injector = Guice.createInjector(new ScalaModule() {
def configure() {
bind[String].toInstance("foo")
bind[GuiceSpec.type].toInstance(GuiceSpec)
}
})
injector.getInstance(classOf[String]) must equal("foo")
GuiceSpec.get must equal("foo")
}
}
}
object GuiceSpec {
@Inject
val s: String = null
def get() = s
}
इसका मतलब यह है कि आप scala-guice और ScalaTest उपयोग कर रहे हैं:
object GuiceSpec {
@Inject
val s: String = null
def get() = s
}
नीचे पोस्ट में जवाब चलता है कि यह स्केला में निर्भरता इंजेक्षन करना संभव नहीं है वस्तु। इस बारे में कोई विचार है? http://stackoverflow.com/questions/31100534/can-we-use-google-guice-di-with-a-scala-object-instead-of-a-scala-class-in-play –