I'm still at the beginning in learning scala in addition to java and i didn't get it how is one supposed to do DI there? can or should i use an existing DI library, should it be done manually or is there another way?
-
3See the [Dependency Injection in Scala Guide](http://di-in-scala.github.io/) – WeiChing 林煒清 Jul 25 '18 at 01:48
9 Answers
Standard Java DI frameworks will usually work with Scala, but you can also use language constructs to achieve the same effect without external dependencies.
-
1the link gives me a really good overview with many examples. thank you very much. – Fabian Apr 01 '10 at 22:09
-
5fwiw, that article was one of the strongest inspirations I had when I first started learning Scala. – Daniel C. Sobral Apr 01 '10 at 22:20
-
I'm curious how the cake pattern is implemented when a varying number of instances of a component might be needed by a service or registry. For example, what if WarmerComponentImpl in the linked example needed two different instances of an OnOffDeviceComponent to do its job? – Mitch Blevins Apr 01 '10 at 22:57
-
1@MitchBlevins, take a look at the "multiple implementations" section in the [Dependency Injection in Scala](http://di-in-scala.github.io/) guide. – adamw Jul 28 '14 at 10:09
-
For a general overview on how to to DI in Scala. Take a look at: http://blog.knoldus.com/2014/07/04/dependency-injection-scala/ – Sky Jan 09 '16 at 10:55
-
1
-
https://web.archive.org/web/20230523000502/http://jonasboner.com/real-world-scala-dependency-injection-di/ --- And for the link posted by @Sky: https://web.archive.org/web/20230523000600/https://blog.knoldus.com/dependency-injection-in-scala-using-self-type-annotations/ – Logan May 23 '23 at 00:10
A new dependency injection library specifically for Scala is Dick Wall's SubCut.
Whereas the Jonas Bonér article referenced in Dan Story's answer emphasizes compile-time bound instances and static injection (via mix-ins), SubCut is based on runtime initialization of immutable modules, and dynamic injection by querying the bound modules by type, string names, or scala.Symbol names.
You can read more about the comparison with the Cake pattern in the GettingStarted document.

- 379
- 4
- 4
Dependency Injection itself can be done without any tool, framework or container support. You only need to remove new
s from your code and move them to constructors. The one tedious part that remains is wiring the objects at "the end of the world", where containers help a lot.
Though with Scala's 2.10 macros, you can generate the wiring code at compile-time and have auto-wiring and type-safety.

- 8,038
- 4
- 28
- 32
-
2So few people realise this that I use it as an interview question. – Tim Barrass Nov 18 '16 at 15:15
A recent project illustrates a DI based purely on constructor injection: zalando/grafter
What's wrong with constructor injection again?
There are many libraries or approaches for doing dependency injection in Scala. Grafter goes back to the fundamentals of dependency injection by just using constructor injection: no reflection, no xml, no annotations, no inheritance or self-types.
Then, Grafter add to constructor injection just the necessary support to:
- instantiate a component-based application from a configuration
- fine-tune the wiring (create singletons)
- test the application by replacing components
- start / stop the application
Grafter is targeting every possible application because it focuses on associating just 3 ideas:
- case classes and interfaces for components
- Reader instances and shapeless for the configuration
- tree rewriting and kiama for everything else!

- 1,262,500
- 529
- 4,410
- 5,250
I haven't done so myself, but most DI frameworks work at the bytecode level (AFAIK), so it should be possible to use them with any JVM language.

- 181,030
- 38
- 327
- 365
-
2One standard Java DI framework I've used with success in Scala over the past couple years is PicoContainer. (And you can use constructor injection instead of setter injection to maintain immutability.) – Seth Tisue Apr 01 '10 at 22:47
-
Spring ME is a dependency injection framework that uses source code analysis to achieve most of what full Spring is doing at runtime. As a consequence, you end up with an almost zero footprint application context, not depending on any external libraries. Not sure if I would use it for Scala though. – Wilfred Springer Mar 01 '12 at 10:41
Previous posts covered the techniques. I wanted to add a link to Martin Odersky's May 2014 talk on the Scala language objectives. He identifies languages that "require" a DI container to inject dependencies as poorly implemented. I agree with this personally, but it is only an opinion. It does seem to indicate that including a DI dependency in your Scala project is non-idiomatic, but again this is opinion. Practically speaking, even with a language designed to inject dependencies natively, there is a certain amount of consistency gained by using a container. It is worth considering both points of view for your purposes.

- 125
- 1
- 6
I would suggest you to try distage (disclaimer: I'm the author).
It allows you to do much more than a typical DI does and has many unique traits:
- distage supports multiple configurations (e.g. you may run your app with different sets of component implementations),
- distage allows you to correctly share dependencies across your tests and easily run same tests for different implementations of your components,
- distage supports roles so you may run multiple services within the same process sharing dependencies between them,
- distage does not depend on
scala-reflect
(but supports all the necessary features of Scala typesystem, like higher-kinded types).
You may also watch our talk at Functional Scala 2019 where we've discussed and demonstrated some important capabiliteis of distage.

- 1,267
- 14
- 19
In addition to the answer of Dan Story, I blogged about a DI variant that also uses language constructs only but is not mentioned in Jonas's post: Value Injection on Traits (linking to web.archive.org now). This pattern is working very well for me.

- 3,283
- 2
- 19
- 16