Dependency Injection in Scala

Dependency Injection in Scala

Presented at Czech Scala Enthusiasts meetup in January 2013.


Jakub Janeček

January 29, 2013


  1. Dependency Injection in Scala Vlastimil Menčík, Jakub Janeček, Michal Příhoda

    Czech Scala Enthusiasts
  2. Java DI frameworks with Scala • important for existing Java-based

    projects/platforms • Java-Scala interoperability on JVM-level bears fruits • some issues may arise ◦ we will show some later
  3. Spring • most popular • well-designed • a lot more

    than just an IoC container • a lot of people have experimented with Spring + Scala • SpringSource is working on the Spring Scala project
  4. The coffee-making domain

  5. Spring (annotations)

  6. Spring (the same in Java)

  7. Spring (application context)

  8. Spring observations • actual differences between Java and Scala were

    minimal • constructor injection works well with immutability • setter injection requires some additional work
  9. Setter injection in Java

  10. Setter injection in Scala

  11. Spring Scala project • developed by SpringSource • targets Spring

    3.2 • work in progres (1.0.0.M1) • wiring beans using functional syntax • functional wrappers for Spring templates ◦ functions instead of anonymous callbacks
  12. Spring Scala (no annotations)

  13. Spring Scala (wiring)

  14. Spring Scala (application context)

  15. Google Guice • focuses only on the DI • leaner,

    easier to get into • less magic • explicit bindings ◦ defined in module classes • just-in-time bindings ◦ automatic discovery
  16. Vanilla Guice

  17. Guice with Scala wrapper

  18. Guice Observations • simplicity • interesting if you don't need

    (or want) Spring • easy to integrate with other frameworks (even the Scala ones)
  19. Contexts and Dependency Injection • the standard (JSR-299) • part

    of Java EE 6 (and every compliant container) • created with lessons from previous DI solutions in mind
  20. CDI annotation(s)

  21. CDI (beans.xml)

  22. Scopes and Contexts • singleton • prototype (aka dependent) •

    session • request • application • ...
  23. Scopes cont. • in CDI, only singleton and dependent are

    injected as direct references ◦ different lifecycles, serialization • DI framework will inject a proxy instead ◦ thread-local magic
  24. Proxies • restrictions on the proxied class ◦ might vary

    between DI frameworks • needs a default constructor • cannot be final or have final methods
  25. Proxy problems • Scala compiler emits final methods ◦ even

    if there are none in the source code • access to a private val from a closure • such class is "unproxyable"
  26. Java DI Frameworks Summary • you can use them if

    you want ◦ Spring is not just DI
  27. Java DI Frameworks Summary • maybe with some additional wrappers

    to make them more Scala-friendly
  28. Java DI Frameworks Summary • problems may arise if there

    are additional restrictions on classes (setters, proxies, ...) ◦ these can be avoided with some effort
  29. SubCut "Scala Uniquely Bound Classes Under Traits" • very

    small and simple DI library • pure and idiomatic Scala • binding DSL • uses implicits to cut down on boilerplate JavaPosse ScalaWags
  30. Step 1: Define Your Components

  31. Step 2: Wire'em All

  32. Step 3: Define Your Injectable Class

  33. Step 4: Run or Test

  34. Pros&Cons • Pros ◦ straightforward and simple ◦ idiomatic •

    Cons ◦ no automatic injection ◦ possible runtime errors (not static as Cake)