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
Sponsored
·
Your Podcast. Everywhere. Effortlessly.
Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
→
Jakub Janeček
January 29, 2013
Programming
0
82
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
770
Intro to Scala Server Toolkit
jakubjanecek
0
700
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
120
Other Decks in Programming
See All in Programming
ぼくの開発環境2026
yuzneri
0
240
Rust 製のコードエディタ “Zed” を使ってみた
nearme_tech
PRO
0
190
AgentCoreとHuman in the Loop
har1101
5
240
Unicodeどうしてる? PHPから見たUnicode対応と他言語での対応についてのお伺い
youkidearitai
PRO
1
2.6k
Fluid Templating in TYPO3 14
s2b
0
130
FOSDEM 2026: STUNMESH-go: Building P2P WireGuard Mesh Without Self-Hosted Infrastructure
tjjh89017
0
170
CSC307 Lecture 02
javiergs
PRO
1
780
「ブロックテーマでは再現できない」は本当か?
inc2734
0
1k
生成AIを使ったコードレビューで定性的に品質カバー
chiilog
1
270
Apache Iceberg V3 and migration to V3
tomtanaka
0
160
20260127_試行錯誤の結晶を1冊に。著者が解説 先輩データサイエンティストからの指南書 / author's_commentary_ds_instructions_guide
nash_efp
1
980
AtCoder Conference 2025
shindannin
0
1.1k
Featured
See All Featured
New Earth Scene 8
popppiees
1
1.5k
Prompt Engineering for Job Search
mfonobong
0
160
Abbi's Birthday
coloredviolet
1
4.8k
How Software Deployment tools have changed in the past 20 years
geshan
0
32k
Evolution of real-time – Irina Nazarova, EuRuKo, 2024
irinanazarova
9
1.2k
Practical Tips for Bootstrapping Information Extraction Pipelines
honnibal
25
1.7k
Why Our Code Smells
bkeepers
PRO
340
58k
HU Berlin: Industrial-Strength Natural Language Processing with spaCy and Prodigy
inesmontani
PRO
0
220
The Cult of Friendly URLs
andyhume
79
6.8k
The Art of Programming - Codeland 2020
erikaheidi
57
14k
The untapped power of vector embeddings
frankvandijk
1
1.6k
実際に使うSQLの書き方 徹底解説 / pgcon21j-tutorial
soudai
PRO
196
71k
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)