Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Dependency Injection in Scala
Search
Jakub Janeček
January 29, 2013
Programming
0
72
Dependency Injection in Scala
Presented at Czech Scala Enthusiasts meetup in January 2013.
Jakub Janeček
January 29, 2013
Tweet
Share
More Decks by Jakub Janeček
See All by Jakub Janeček
Scala Server Toolkit: How to Write FP Microservice Step-by-step
jakubjanecek
0
740
Intro to Scala Server Toolkit
jakubjanecek
0
600
Massively Scalable Services at AVAST: Case Study
jakubjanecek
0
87
Scala 2.10
jakubjanecek
0
130
Scala - What Makes the Difference? Part 2 - Code Examples
jakubjanecek
0
110
Other Decks in Programming
See All in Programming
CSC307 Lecture 17
javiergs
PRO
0
110
Efficiency and Rock 'n’ Roll (Really!)
hollycummins
0
680
Babylon.js 8.0のアプデ情報を 軽率にキャッチアップ / catch-up-babylonjs-8
drumath2237
0
120
Perlで痩せる
yuukis
1
680
KotlinConf 2025 現地で感じたServer-Side Kotlin
n_takehata
1
110
REST API設計の実践 – ベストプラクティスとその落とし穴
kentaroutakeda
2
350
Gleamという選択肢
comamoca
4
500
つよそうにふるまい、つよい成果を出すのなら、つよいのかもしれない
irof
0
240
2度もゼロから書き直して、やっとブラウザでぬるぬる動くAIに辿り着いた話
tomoino
0
140
プロダクト改善のために新しいことを始める -useContextからの卒業、Zustandへ-
rebase_engineering
1
110
単体テストの始め方/作り方
toms74209200
0
370
レガシーシステムの機能調査・開発におけるAI利活用
takuya_ohtonari
0
500
Featured
See All Featured
Why Our Code Smells
bkeepers
PRO
337
57k
Large-scale JavaScript Application Architecture
addyosmani
512
110k
No one is an island. Learnings from fostering a developers community.
thoeni
21
3.3k
Embracing the Ebb and Flow
colly
85
4.7k
Save Time (by Creating Custom Rails Generators)
garrettdimon
PRO
31
1.2k
Understanding Cognitive Biases in Performance Measurement
bluesmoon
29
1.7k
CoffeeScript is Beautiful & I Never Want to Write Plain JavaScript Again
sstephenson
161
15k
Building an army of robots
kneath
306
45k
個人開発の失敗を避けるイケてる考え方 / tips for indie hackers
panda_program
106
19k
Building a Modern Day E-commerce SEO Strategy
aleyda
41
7.3k
Mobile First: as difficult as doing things right
swwweet
223
9.6k
The Art of Programming - Codeland 2020
erikaheidi
54
13k
Transcript
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-level bears fruits • some issues may arise ◦ we will show some later
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
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 immutability • setter injection requires some additional work
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 using functional syntax • functional wrappers for Spring templates ◦ functions instead of anonymous callbacks
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 module classes • just-in-time bindings ◦ automatic discovery
Vanilla Guice
Guice with Scala wrapper
Guice Observations • simplicity • interesting if you don't need
(or want) Spring • easy to integrate with other frameworks (even the Scala ones)
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
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, serialization • DI framework will inject a proxy instead ◦ thread-local magic
Proxies • restrictions on the proxied class ◦ might vary
between DI frameworks • needs a default constructor • cannot be final or have final methods
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"
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, ...) ◦ these can be avoided with some effort
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
Step 1: Define Your Components
Step 2: Wire'em 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 static as Cake)