# Dependency injection

**URL:** <https://discuss.kotlinlang.org/t/dependency-injection/138>\
**Category:** Uncategorized\
**Created:** [September 3, 2015, 9:15pm UTC](https://discuss.kotlinlang.org/t/dependency-injection/138 "2015-09-03T21:15:03Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![m.h.shams](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/m.h.shams/32/1482_2.png) [@m.h.shams](https://discuss.kotlinlang.org/u/m.h.shams)\
**Post date:** [September 3, 2015, 9:15pm UTC](https://discuss.kotlinlang.org/t/dependency-injection/138/1 "2015-09-03T21:15:03Z")

</div>

Hi All,

My current approach to define dependencies that are supposed to be injected by a framework (Spring, Guice, etc) is like following:

&nbsp;

```kotlin
@Inject
private val logger: Logger? = null

```

Although, from language point of view it is true that logger in above example can be null, but considering the DI framework, we can fairely assume that logger is always **NOT** null. Is there any solution to define such depencies as following or something similar ?

&nbsp;

```kotlin
@Inject
private val logger: Logger
```

Regards,  
Mohammad Shamsi

---

<div class="post-metadata">

**Author:** ![cedricbeust](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/cedricbeust/32/1429_2.png) [@cedricbeust](https://discuss.kotlinlang.org/u/cedricbeust)\
**Post date:** [September 3, 2015, 9:45pm UTC](https://discuss.kotlinlang.org/t/dependency-injection/138/2 "2015-09-03T21:45:46Z")

</div>

I declare my Guice dependencies in the constructor:

```kotlin

public class MyClass @Inject constructor(val logger: Logger, val db: Database) { ...

```

---

<div class="post-metadata">

**Author:** ![rodrigodev](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/rodrigodev/32/1682_2.png) [@rodrigodev](https://discuss.kotlinlang.org/u/rodrigodev)\
**Post date:** [September 4, 2015, 7:00am UTC](https://discuss.kotlinlang.org/t/dependency-injection/138/3 "2015-09-04T07:00:31Z")

</div>

As an alternative to what Cedric does, you can use the not-null property delegate ([http://kotlinlang.org/docs/reference/delegated-properties.html#not-null](http://kotlinlang.org/docs/reference/delegated-properties.html#not-null)), though that would possibly introduce some function call overhead (because it always needs to check before reading the property?), but you should probably not worry about that most of the time (though I would prabably prefer using constructor injection as Cedric most of the time).

---

<div class="post-metadata">

**Author:** ![m.h.shams](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/m.h.shams/32/1482_2.png) [@m.h.shams](https://discuss.kotlinlang.org/u/m.h.shams)\
**Post date:** [September 4, 2015, 7:06am UTC](https://discuss.kotlinlang.org/t/dependency-injection/138/4 "2015-09-04T07:06:23Z")

</div>

This is ok with some DI frameworks but not all. Some of them require to have the class with default constructor. they inject dependencis after instance creataion.

---

<div class="post-metadata">

**Author:** ![m.h.shams](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/m.h.shams/32/1482_2.png) [@m.h.shams](https://discuss.kotlinlang.org/u/m.h.shams)\
**Post date:** [September 4, 2015, 7:10am UTC](https://discuss.kotlinlang.org/t/dependency-injection/138/5 "2015-09-04T07:10:16Z")

</div>

This solution also does not work in some DI frameworks (for example: Tapestry IoC).

---

<div class="post-metadata">

**Author:** ![rodrigodev](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/rodrigodev/32/1682_2.png) [@rodrigodev](https://discuss.kotlinlang.org/u/rodrigodev)\
**Post date:** [September 4, 2015, 7:38am UTC](https://discuss.kotlinlang.org/t/dependency-injection/138/6 "2015-09-04T07:38:15Z")

</div>

Sure Mohammad, when an _x_ alternative is not a solution for a specific _y_ problem, then you shouldn't use it.

---

<div class="post-metadata">

**Author:** ![abreslav](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/abreslav/32/8279_2.png) [@abreslav](https://discuss.kotlinlang.org/u/abreslav)\
**Post date:** [September 4, 2015, 8:47am UTC](https://discuss.kotlinlang.org/t/dependency-injection/138/7 "2015-09-04T08:47:29Z")

</div>

We are going to roll out a new feature that fixes this issue very soon

---

<div class="post-metadata">

**Author:** ![m.h.shams](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/m.h.shams/32/1482_2.png) [@m.h.shams](https://discuss.kotlinlang.org/u/m.h.shams)\
**Post date:** [September 4, 2015, 9:58am UTC](https://discuss.kotlinlang.org/t/dependency-injection/138/8 "2015-09-04T09:58:06Z")

</div>

@Andrey,

May I have the issue number (if it is available in issue tracker)?

---

<div class="post-metadata">

**Author:** ![abreslav](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/abreslav/32/8279_2.png) [@abreslav](https://discuss.kotlinlang.org/u/abreslav)\
**Post date:** [September 4, 2015, 12:14pm UTC](https://discuss.kotlinlang.org/t/dependency-injection/138/9 "2015-09-04T12:14:52Z")

</div>

[https://youtrack.jetbrains.com/issue/KT-9067](https://youtrack.jetbrains.com/issue/KT-9067)

---

<div class="post-metadata">

**Author:** ![jayson.minard](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/jayson.minard/32/1434_2.png) [@jayson.minard](https://discuss.kotlinlang.org/u/jayson.minard)\
**Post date:** [September 4, 2015, 3:01pm UTC](https://discuss.kotlinlang.org/t/dependency-injection/138/10 "2015-09-04T15:01:04Z")

</div>

Or use delegates to inject from Injekt framework:

[https://github.com/kohesive/injekt](https://github.com/kohesive/injekt)

It can be used as default constructor expressions, as delegates, or as just a function to return a value from the object registry.

Injekt will soon materialize object graphs, and the “lateinit” will be useful then possibly.

Also it would be nice for the Jackson-Jr port to Kotlin.&nbsp;&nbsp;But mostly those are taking a bit different approach that might include “lateinit” but as a lower priority.&nbsp;&nbsp;With M13 they can base decisions on which things are nullable or not, final or can be set later, have default values or not (Java injection and data binding frameworks will not get this right, so they will never really fit well without using Kotlin reflection), and lastly could use “lateinit” when no other path makes sense such as a “val” property not in the constructor and should be injected without a delegate (but why not use a delegate unless your object wasn’t made by you that you are injecting into).&nbsp;&nbsp;So a combination of constructor parameters, setters, and “lateinit” properties.

But these are “going forward” frameworks that assume most your App code is Kotlin, and you “use” Java libraries but the Kotlin is in control, and where Injection and data binding happen.&nbsp;&nbsp;Although our data binder will handle generic Java cases as well.&nbsp;&nbsp;Just not as pretty.

---

<div class="post-metadata">

**Author:** ![vkopichenko](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/vkopichenko/32/1988_2.png) [@vkopichenko](https://discuss.kotlinlang.org/u/vkopichenko)\
**Post date:** [June 2, 2016, 7:51am UTC](https://discuss.kotlinlang.org/t/dependency-injection/138/11 "2016-06-02T07:51:41Z")

</div>

The issue has been fixed and here is the description: [https://kotlinlang.org/docs/reference/properties.html#late-initialized-properties](https://kotlinlang.org/docs/reference/properties.html#late-initialized-properties)

---

<div class="post-metadata">

**Author:** ![sebdeleuze](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/sebdeleuze/32/2196_2.png) [@sebdeleuze](https://discuss.kotlinlang.org/u/sebdeleuze)\
**Post date:** [June 4, 2016, 11:33pm UTC](https://discuss.kotlinlang.org/t/dependency-injection/138/12 "2016-06-04T23:33:31Z")

</div>

Constructor injection is also recommended with Spring, see this [spring-kotlin FAQ entry](https://github.com/sdeleuze/spring-kotlin#what-is-the-recommanded-way-to-inject-dependencies-in-a-spring-kotlin-application) for more details.

---

<div class="post-metadata">

**Author:** ![dalewking](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/dalewking/32/1496_2.png) [@dalewking](https://discuss.kotlinlang.org/u/dalewking)\
**Post date:** [June 8, 2016, 5:57pm UTC](https://discuss.kotlinlang.org/t/dependency-injection/138/13 "2016-06-08T17:57:25Z")

</div>

You might want to look at Kodein which is a nice DI alternative for Kotlin, that I quite like. It does not use annotation processing, it instead uses property delegation to retrieve dependencies:

> **[GitHub - SalomonBrys/Kodein](https://github.com/SalomonBrys/Kodein)**
>
> Contribute to SalomonBrys/Kodein development by creating an account on GitHub.
