Dependency Injection     in Scala  Vlastimil Menčík, Jakub Janeček, Michal Příhoda             Czech Scala Enthusiasts
Java DI frameworks with Scala● important for existing Java-based  projects/platforms● Java-Scala interoperability on JVM-l...
Spring● most popular● well-designed● a lot more than just an IoC container● a lot of people have experimented with  Spring...
The coffee-making domain
Spring (annotations)
Spring (the same in Java)
Spring (application context)
Spring observations● actual differences between Java and Scala  were minimal● constructor injection works well with  immut...
Setter injection in Java
Setter injection in Scala
Spring Scala project●   developed by SpringSource●   targets Spring 3.2●   work in progres (1.0.0.M1)●   wiring beans usin...
Spring Scala (no annotations)
Spring Scala (wiring)
Spring Scala (application context)
Google Guice●   focuses only on the DI●   leaner, easier to get into●   less magic●   explicit bindings    ○ defined in mo...
Vanilla Guice
Guice with Scala wrapper
Guice Observations● simplicity● interesting if you dont need (or want) Spring● easy to integrate with other frameworks  (e...
Contexts and Dependency Injection● the standard (JSR-299)● part of Java EE 6 (and every compliant  container)● created wit...
CDI annotation(s)
CDI (beans.xml)
Scopes and Contexts●   singleton●   prototype (aka dependent)●   session●   request●   application●   ...
Scopes cont.● in CDI, only singleton and dependent are  injected as direct references   ○ different lifecycles, serializat...
Proxies● restrictions on the proxied class   ○ might vary between DI frameworks● needs a default constructor● cannot be fi...
Proxy problems● Scala compiler emits final methods  ○ even if there are none in the source code● access to a private val f...
Java DI Frameworks Summary● you can use them if you want  ○ Spring is not just DI
Java DI Frameworks Summary● maybe with some additional wrappers to  make them more Scala-friendly
Java DI Frameworks Summary● problems may arise if there are additional  restrictions on classes (setters, proxies, ...)   ...
SubCut"Scala Uniquely Bound Classes Under Traits"   https://github.com/dickwall/subcut●    very small and simple DI librar...
Step 1: Define Your Components
Step 2: Wireem All
Step 3: Define Your Injectable Class
Step 4: Run or Test
Pros&Cons● Pros  ○ straightforward and simple  ○ idiomatic● Cons  ○ no automatic injection  ○ possible runtime errors (not...
Nächste SlideShare
Wird geladen in …5
×

Dependency Injection in Scala, part 1

8.972 Aufrufe

Veröffentlicht am

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).

Veröffentlicht in: Technologie
0 Kommentare
11 Gefällt mir
Statistik
Notizen
  • Als Erste(r) kommentieren

Keine Downloads
Aufrufe
Aufrufe insgesamt
8.972
Auf SlideShare
0
Aus Einbettungen
0
Anzahl an Einbettungen
6
Aktionen
Geteilt
0
Downloads
0
Kommentare
0
Gefällt mir
11
Einbettungen 0
Keine Einbettungen

Keine Notizen für die Folie

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" https://github.com/dickwall/subcut● 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)

×