How would one do dependency injection in scala?

2019-03-08 09:13发布

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?

8条回答
贪生不怕死
2楼-- · 2019-03-08 09:58

Dependency Injection itself can be done without any tool, framework or container support. You only need to remove news 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.

See the Dependency Injection in Scala Guide

查看更多
等我变得足够好
3楼-- · 2019-03-08 09:58

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.

查看更多
登录 后发表回答