Home » Use Spring @Autowired in Kotlin

Use Spring @Autowired in Kotlin

  • by
Using spring annotations like @Autowired in kotlin?

1. Overview

In this article, we will learn to use Spring @Autowired annotation in Kotlin. The Spring auto wiring enables you to inject the dependent beans or objects into the associated references of a class.

2. Key terminologies

Bean: An object that is instantiated, assembled, and managed by the Spring container. Otherwise, a bean is a class instance controlled by the Spring framework. See our bean instantiation article to understand more about bean instantiation.

Dependency: A class object requires objects of other classes to perform its responsibilities. We call them dependencies.

Dependency injection (DI): A process whereby the Spring container gives the bean its instance variables. Therefore, DI helps to achieve Inversion of Control (IOC). Here, The Spring container takes the responsibility of object creation and injecting its dependencies instead of the class creating the dependency objects by itself.

2.1. Use of @Autowired annotation in dependency injection

We can use @Autowired for dependency injection in one of the below ways:

  • Constructor based injection
  • Setter based injection (or)
  • Field based injection

See our article on Kotlin dependency injection to know better. Here, we will focus on @Autowired annotation usage in dependency injection.

3. Kotlin @Autowired constructor injection

We provide the dependencies required by the class as arguments to the constructor. So each argument represents a dependency.

@Component
class Car @Autowired constructor (private val owner: Owner) {
    fun getOwner() : Owner {
        return owner
    }
}
class Owner(private val name: String, private val address: String) {}

Note that we have annotated the constructor using @Autowired. This annotation instructs the Spring framework to inject the owner dependency into the Car bean. However, as of Spring 4.3, you no longer need to add @Autowired annotation to the class that has only one constructor.

Since our Car class has only one constructor, you can remove the @Autowired annotation:

@Component
class Car (private val owner: Owner) {
    fun getOwner() : Owner {
        return owner
    }
}
class Owner(private val name: String, private val address: String) {}

Suppose you have a class with more than one constructor, then you should explicitly specify the @Autowired annotation to one constructor that should be used by Spring to inject dependencies.

For example, the below Car class has two constructors. So you had to add @Autowired annotation to the constructor which you want the Spring container to use for injecting dependencies.

class Owner(val name: String, val address: String) {}
class Car @Autowired constructor(val owner: Owner) {
    private var carName : String? = null
    constructor(carName: String, owner: Owner): this(owner) {
        this.carName = carName
    }
}

4. Kotlin @Autowired setter based injection

You can provide the required dependencies as method arguments to your class rather than using the constructor or static factory method arguments.

Annotate your setter method with the @Autowired annotation.

Spring container will look at the @Autowired annotation and calls your setter method to inject the required dependencies.

First, let’s look at the below example. The updateBug setter method has the @Autowired annotation and the Spring container invokes this setter method and injects the desc and latestComment dependencies.

Note that the variables have the lateinit specifier to show that the Spring container will assign the variable at a later point in time.

class Bug {
   lateinit var desc: String;
   lateinit var latestComment: String;
    // a setter method so that the Spring container can inject desc and latestComment dependencies
   @Autowired
   fun updateBug(desc: String, latestComment: String) {
       this.desc = desc;
       this.latestComment = latestComment;
   }
}

5. Spring field based injection

You can assign the required dependencies directly to the fields by annotating them with @Autowired annotation.

Let’s take a similar example in Kotlin. We have @Autowired annotation to the field variables desc and latestComment. The Spring container looks for the @Autowired annotation in the field variables and injects the dependencies.

class Bug {
   @Autowired
   lateinit var desc: String
   @Autowired
   lateinit var latestComment: String
}

6. Conclusion

In this article, we have learned to use the @Autowired Spring annotation in Kotlin.

Leave a Reply

Your email address will not be published.