DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Rodolfo has posted 3 posts at DZone. View Full User Profile

An Experiment With Scala An Guice

07.30.2008
| 9169 views |
  • submit to reddit
        // An experiment with Scala an Guice


/*
 * Main.scala
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package scalaapplication1

import scala.reflect._
import com.google.inject._
import com.google._

// some interfaces

trait HeadHunterService {
  def findSomeone(): Option[Person]
}

trait Person {
 var name: String = _ 
 def tellSomething = println("hey, I am " + name)
 override def toString = name
}

trait Company {
  var name: String = _
  def tellSomething 
  def hireTheGuy()  
  @Inject 
  var service: HeadHunterService = _
  var person: Option[Person] = _
}

// now some implementations

class PersonImpl extends Person {
}

class CompanyImpl extends Company {
 def hireTheGuy = {this.person = service.findSomeone()}
 def tellSomething = {
   val greeting: String = person match {
                 case None => "I don't have any employee"
                 case Some(_) => "I have this employee: " + person
                 case _ => "I don't have any employee (indeed, why this one is matching instead of first option ??) "
                }
   println("hey, I am a company named " + name + "  " + greeting)
 }
}

class HeadHunterServiceImpl extends HeadHunterService {
 def findSomeone() = Some(new PersonImpl()) ;
}

// guice module 

class MyModule extends AbstractModule {
  override def configure() {
    bind(classOf[Person]).to(classOf[PersonImpl])
    bind(classOf[Company]).to(classOf[CompanyImpl])    
    bind(classOf[HeadHunterService]).to(classOf[HeadHunterServiceImpl])        
  }
}

object Main {

  /**
   * @param args the command line arguments
   */
  def main(args: Array[String]) = {

    println("Nice to meet you, Guice!")

    val arrayOfModules = new Array[Module](1) 
    arrayOfModules(0) = new MyModule()  
        
    // for some reason I dont know yet, this just works with an array of modules //

    val inj:Injector = Guice.createInjector(arrayOfModules)
    
    val company:Company = inj.getInstance(classOf[Company])

    company.name = "Yahoo"
    company.tellSomething
    company.hireTheGuy
    company.person.get.name = "John"
    company.person.get.tellSomething
    company.name = "microsoft!"
    company.tellSomething

    // this was a previous try but using Spring instead
    // val res:Resource  = new FileSystemResource("beans.xml");
    // val factory: BeanFactory = new XmlBeanFactory(res);
    // println(factory.containsBean("company"))
    // Scala compiler complains about bellow line because it isn't type safe
    // val company:Company = factory.getBean("company", classOf[Company])
    // UPDATE: the problem could be easily fixed by casting with asInstanceOf :
    // http://www.scala-lang.org/docu/files/api/scala/Any.html#asInstanceOf

    println("bye") 

  }

}