Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Dependency Injection in Scala, part 1


Published on

First part of the presentation about dependency injection in Scala from January's meetup of the Czech Scala Enthusiasts.
This part addresses usage of Java DI frameworks in Scala (CDI, Spring, Guice) and SubCut (pure Scala library).

Published in: Technology
  • Be the first to comment

Dependency Injection in Scala, part 1

  1. 1. Dependency Injection in Scala Vlastimil Menčík, Jakub Janeček, Michal Příhoda Czech Scala Enthusiasts
  2. 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. 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. 4. The coffee-making domain
  5. 5. Spring (annotations)
  6. 6. Spring (the same in Java)
  7. 7. Spring (application context)
  8. 8. Spring observations● actual differences between Java and Scala were minimal● constructor injection works well with immutability● setter injection requires some additional work
  9. 9. Setter injection in Java
  10. 10. Setter injection in Scala
  11. 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. 12. Spring Scala (no annotations)
  13. 13. Spring Scala (wiring)
  14. 14. Spring Scala (application context)
  15. 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. 16. Vanilla Guice
  17. 17. Guice with Scala wrapper
  18. 18. Guice Observations● simplicity● interesting if you dont need (or want) Spring● easy to integrate with other frameworks (even the Scala ones)
  19. 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. 20. CDI annotation(s)
  21. 21. CDI (beans.xml)
  22. 22. Scopes and Contexts● singleton● prototype (aka dependent)● session● request● application● ...
  23. 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. 24. Proxies● restrictions on the proxied class ○ might vary between DI frameworks● needs a default constructor● cannot be final or have final methods
  25. 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. 26. Java DI Frameworks Summary● you can use them if you want ○ Spring is not just DI
  27. 27. Java DI Frameworks Summary● maybe with some additional wrappers to make them more Scala-friendly
  28. 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. 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. 30. Step 1: Define Your Components
  31. 31. Step 2: Wireem All
  32. 32. Step 3: Define Your Injectable Class
  33. 33. Step 4: Run or Test
  34. 34. Pros&Cons● Pros ○ straightforward and simple ○ idiomatic● Cons ○ no automatic injection ○ possible runtime errors (not static as Cake)