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
94
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
800
Intro to Scala Server Toolkit
jakubjanecek
0
730
Massively Scalable Services at AVAST: Case Study
jakubjanecek
0
120
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に全振りするために辞めたこと ― / Organization by Subtraction
hirokiyamamoto14
PRO
0
150
自動化したのに回らないテスト運用の壁ーAI時代の品質責任と生産性
mfunaki
1
140
Terraform標準の組織で AWS CDKをどう使うか
mu7889yoon
1
530
SlackアプリとLambdaの 連携を構築した話
pawn_4_s
1
120
Android CLI
fornewid
0
220
the container ship “Apple Silicon”@WWDC26 Recap -Japan-\(region).swift
shingangan
0
130
Laravel Boostに学ぶ、AIにPHPを書かせる技術 〜OSSの実装から蒸留するエージェント制御の王道〜
kentaroutakeda
3
710
関東Kaggler会_NVIDIA_Nemotron_コンペ_振り返り
rick_ds
0
620
20260722_microCMSで考える、AI時代のコンテンツ運用設計
yosh1
0
410
バグを直したら useEffect が消えた
colorful12
2
490
komatsuna「分散システムにおけるバグ分析手法」
komatsunaqa
0
260
言葉の格闘技のススメ~紙とペンと言葉から始める、キャリアの描き方~
progresscicada
2
150
Featured
See All Featured
Efficient Content Optimization with Google Search Console & Apps Script
katarinadahlin
PRO
1
800
Visualization
eitanlees
152
17k
So, you think you're a good person
axbom
PRO
2
2.1k
Dominate Local Search Results - an insider guide to GBP, reviews, and Local SEO
greggifford
PRO
0
290
The Illustrated Guide to Node.js - THAT Conference 2024
reverentgeek
1
430
Navigating Team Friction
lara
192
16k
We Are The Robots
honzajavorek
0
300
More Than Pixels: Becoming A User Experience Designer
marktimemedia
3
490
Marketing to machines
jonoalderson
1
5.7k
The browser strikes back
jonoalderson
0
1.5k
The agentic SEO stack - context over prompts
schlessera
0
860
Java REST API Framework Comparison - PWX 2021
mraible
34
9.6k
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)