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
89
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Dependency Injection in Scala
Presented at Czech Scala Enthusiasts meetup in January 2013.
Jakub Janeček
January 29, 2013
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
790
Intro to Scala Server Toolkit
jakubjanecek
0
720
Massively Scalable Services at AVAST: Case Study
jakubjanecek
0
110
Scala 2.10
jakubjanecek
0
140
Scala - What Makes the Difference? Part 2 - Code Examples
jakubjanecek
0
130
Other Decks in Programming
See All in Programming
技術的負債解消で開発者の未来を開く- AIの力でコード刷新
kmd2kmd
0
120
Signal Forms: Details & Live Coding @enterJS 2026 in Mannheim
manfredsteyer
PRO
0
200
さぁV100、メモリをお食べ・・・
nilpe
0
160
JavaDoc 再入門
nagise
1
420
Contextとはなにか
chiroruxx
1
380
キャリア迷子上等 ─ "ない道"は自分で作ればいい
16bitidol
3
2.3k
Datadog LLM Observabilityで実現する 安全なLLM Usage 管理
3150
0
120
Oxcを導入して開発体験が向上した話
yug1224
4
340
Inside Stream API
skrb
1
800
決定論的オーケストレーションの設計と実装 / Design and Implementation of Deterministic Orchestration
nrslib
4
1.5k
「AIで開発し、AIを届ける」をEvalでつなぐ 〜AIネイティブに始めるプロダクト開発の実践〜 / Connecting "Develop with AI, deliver AI" with Eval
rkaga
4
5.4k
ローカルLLMを使ってB2Bサービスを作っていての学び
yaotti
0
220
Featured
See All Featured
The #1 spot is gone: here's how to win anyway
tamaranovitovic
3
1.1k
What does AI have to do with Human Rights?
axbom
PRO
1
2.2k
Faster Mobile Websites
deanohume
310
32k
Redefining SEO in the New Era of Traffic Generation
szymonslowik
1
350
Build The Right Thing And Hit Your Dates
maggiecrowley
39
3.2k
The Language of Interfaces
destraynor
162
27k
How to make the Groovebox
asonas
2
2.2k
Helping Users Find Their Own Way: Creating Modern Search Experiences
danielanewman
31
3.2k
Six Lessons from altMBA
skipperchong
29
4.3k
The Pragmatic Product Professional
lauravandoore
37
7.3k
Leveraging Curiosity to Care for An Aging Population
cassininazir
1
280
Exploring the Power of Turbo Streams & Action Cable | RailsConf2023
kevinliebholz
37
6.5k
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)