> How do integrate scala into guice?
> TIA for any pointers!
Guice integrates with Scala almost as seamlessly as Java. There are some corner
cases, but nothing which can't be worked around.
I use Guice on a regular basis with Scala and Wicket. Works great for me.
Best regards, --- Jan.
Well, it's actually almost as referred to you in the link from the other answer:
http://jonasboner.com/2008/10/06/real-world-scala-dependency-injection-di/
class MyClient {
@Inject val toBeInjected: AnotherClass = toBeInjected // !!
}
trait ServiceInjector {
ServiceInjector.inject( this )
}
object ServiceInjector {
private val injector = Guice.createInjector( Array[Module]( new YourModule ) )
def inject( obj: AnyRef ) = injector.injectMembers( obj )
}
Usage:
val client = new MyClient with ServiceInjector
or:
class InjectedMyClient extends MyClient with ServiceInjector
Best regards, --- Jan.
--
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 google-guice...@googlegroups.com.
To post to this group, send email to google...@googlegroups.com.
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/0b5c01b0-ad63-4a37-916e-6b00c7ceaa8c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
@Plugin(id = "au.id.rleach.overmind", name = "Overmind", version = "0.0.1-SNAPSHOT")
final class OvermindPlugin {
@Inject
private val game: Game = null
@Inject
private val logger: Logger = null
@Inject
private val pluginManager: PluginManager = null
@Inject
private val serviceManager: ServiceManager = null
@Inject
private val eventManager: EventManager = null
@Inject
private val gameRegistry: GameRegistry = null
@Inject
private val teleportHelper: TeleportHelper = null
@Subscribe def onConstruction(event: ConstructionEvent): Unit = {
logger debug "Plugin constructed"
ServiceInjector.module = new OModule(this, game, logger, pluginManager, serviceManager, eventManager, gameRegistry, teleportHelper)
val x = new TestListener with Listener with ServiceInjector
x.init()
}
}
class OModule(val plugin: Object, val game: Game, val logger: Logger, val pluginManager: PluginManager, val serviceManager: ServiceManager, val eventManager: EventManager, val gameRegistry: GameRegistry, val teleportHelper: TeleportHelper) extends Module {
override def configure(binder: Binder): Unit = {
binder.bind(classOf[Game]).toInstance(game)
binder.bind(classOf[Logger]).toInstance(logger)
binder.bind(classOf[PluginManager]).toInstance(pluginManager)
binder.bind(classOf[ServiceManager]).toInstance(serviceManager)
binder.bind(classOf[EventManager]).toInstance(eventManager)
binder.bind(classOf[GameRegistry]).toInstance(gameRegistry)
binder.bind(classOf[TeleportHelper]).toInstance(teleportHelper)
binder.bind(classOf[Object]).toInstance(plugin)
binder.bind(classOf[Object]).annotatedWith(Names.named(plugin.getClass.getAnnotation(classOf[Plugin]).id())).toInstance(plugin);
//bind(classOf[File]).annotatedWith(new ConfigDirAnnotation(true)).toInstance(Loader.instance.getConfigDir)
}
}
/**
* Marker class to mark that the class will not function correctly unless initialized after injection/construction.
*/
trait Initializable {
/**
* Must be run after a class is injected to get the class ready to use
*/
def init() : Unit
}
trait ServiceInjector {
ServiceInjector.inject(this)
}
object ServiceInjector {
var module : Module = null
private lazy val injector = Guice.createInjector(
module
)
def inject(obj: Initializable) = {
injector.injectMembers(obj)
obj.init()
To view this discussion on the web visit https://groups.google.com/d/msgid/google-guice/5009b55d-52cf-4ba8-ba4f-34fe23f9590d%40googlegroups.com.