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
68
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
690
Intro to Scala Server Toolkit
jakubjanecek
0
550
Massively Scalable Services at AVAST: Case Study
jakubjanecek
0
84
Scala 2.10
jakubjanecek
0
130
Scala - What Makes the Difference? Part 2 - Code Examples
jakubjanecek
0
100
Other Decks in Programming
See All in Programming
EventSourcingの理想と現実
wenas
6
2.3k
Better Code Design in PHP
afilina
PRO
0
120
Amazon Bedrock Agentsを用いてアプリ開発してみた!
har1101
0
330
Generative AI Use Cases JP (略称:GenU)奮闘記
hideg
1
290
役立つログに取り組もう
irof
28
9.6k
Duckdb-Wasmでローカルダッシュボードを作ってみた
nkforwork
0
120
.NET のための通信フレームワーク MagicOnion 入門 / Introduction to MagicOnion
mayuki
1
1.4k
Jakarta EE meets AI
ivargrimstad
0
530
OSSで起業してもうすぐ10年 / Open Source Conference 2024 Shimane
furukawayasuto
0
100
Make Impossible States Impossibleを 意識してReactのPropsを設計しよう
ikumatadokoro
0
170
Tauriでネイティブアプリを作りたい
tsucchinoko
0
370
Laravel や Symfony で手っ取り早く OpenAPI のドキュメントを作成する
azuki
2
110
Featured
See All Featured
The Art of Programming - Codeland 2020
erikaheidi
52
13k
Done Done
chrislema
181
16k
How GitHub (no longer) Works
holman
310
140k
10 Git Anti Patterns You Should be Aware of
lemiorhan
654
59k
Keith and Marios Guide to Fast Websites
keithpitt
409
22k
Building Applications with DynamoDB
mza
90
6.1k
Designing the Hi-DPI Web
ddemaree
280
34k
How To Stay Up To Date on Web Technology
chriscoyier
788
250k
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
42
9.2k
Into the Great Unknown - MozCon
thekraken
32
1.5k
No one is an island. Learnings from fostering a developers community.
thoeni
19
3k
Distributed Sagas: A Protocol for Coordinating Microservices
caitiem20
329
21k
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)