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
·
SiteGround - Reliable hosting with speed, security, and support you can count on.
→
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
SourceGeneratorのマーカー属性問題について
htkym
0
180
encoding/json/v2のUnmarshalはこう変わった:内部実装で見る設計改善
kurakura0916
0
370
go directiveを最新にしすぎないで欲しい話──あるいは、Go 1.26からgo mod initで作られるgo directiveの値が変わる話 / Go 1.26 リリースパーティ
arthur1
2
520
Go Conference mini in Sendai 2026 : Goに新機能を提案し実装されるまでのフロー徹底解説
yamatoya
0
550
Ruby and LLM Ecosystem 2nd
koic
0
250
モジュラモノリスにおける境界をGoのinternalパッケージで守る
magavel
0
3.5k
今、アーキテクトとして 品質保証にどう関わるか
nealle
0
210
What Spring Developers Should Know About Jakarta EE
ivargrimstad
0
320
Codex の「自走力」を高める
yorifuji
0
1.1k
エージェント開発初心者の僕がエージェントを作った話と今後やりたいこと
thasu0123
0
240
Fundamentals of Software Engineering In the Age of AI
therealdanvega
1
230
DevinとClaude Code、SREの現場で使い倒してみた件
karia
1
990
Featured
See All Featured
Gemini Prompt Engineering: Practical Techniques for Tangible AI Outcomes
mfonobong
2
310
Thoughts on Productivity
jonyablonski
75
5.1k
Exploring the Power of Turbo Streams & Action Cable | RailsConf2023
kevinliebholz
37
6.3k
Into the Great Unknown - MozCon
thekraken
40
2.3k
Navigating the moral maze — ethical principles for Al-driven product design
skipperchong
2
280
Collaborative Software Design: How to facilitate domain modelling decisions
baasie
0
150
Designing Powerful Visuals for Engaging Learning
tmiket
0
260
Unsuck your backbone
ammeep
672
58k
Lightning Talk: Beautiful Slides for Beginners
inesmontani
PRO
1
470
ラッコキーワード サービス紹介資料
rakko
1
2.6M
Groundhog Day: Seeking Process in Gaming for Health
codingconduct
0
120
The World Runs on Bad Software
bkeepers
PRO
72
12k
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)