This post shows an example of how to use Spring Autowired to inject an service object into controller objects in Kotlin.
Requirements For Spring and Kotlin
These are Software applications used for this post.
IntelliJ
IDEA
Ultimate 2016.3- The
Community Edition
maybe enough, but we have not tried it.
- The
Kotlin
Version 1.1- Windows 10
- Spring or Spring Boot
First, we need a project that has both Kotlin and Spring working together. The fastest way to create such project is via the Spring Initialzr and explicitly choosing Kotlin as the language.
Service Codes For Kotlin in Spring
Next, we create a service within that Spring project we generated online for Kotlin. In our example, we are using two Spring beans (or components) – ClientRepo and ClientService. To demonstrate how Spring Autowired works with Kotlin, we autowire the ClientRepo in the ClientService.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | package com.turreta.service import com.turreta.domain.Client import com.turreta.repository.ClientRepo import org.springframework.beans.factory.annotation.Autowired import org.springframework.stereotype.Service import org.springframework.transaction.annotation.Propagation import org.springframework.transaction.annotation.Transactional /** * Created by Turreta.com on 18/6/2017. */ @Service class ClientService { // Our Spring Data repository @Autowired lateinit private var clientRepo: ClientRepo @Transactional(propagation = Propagation.REQUIRED) fun createClient(clientName: String) { var c = Client() c.clientName = clientName clientRepo.save(c) } } |
Although the ClientRepo is not available on this post, we could create it as follows.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | package com.turreta.repository import com.turreta.domain.Client import org.springframework.stereotype.Component /** * Created by Turreta.com on 17/10/2022. */ @Component class ClientRepo { fun save(client: Client) { // Save client to database } } |
Controller Codes
Then, we use the Spring Autowired annotation in Kotlin to inject the ClientService object in the IndexController.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | package com.turreta.controller import com.turreta.service.ClientService import org.springframework.beans.factory.annotation.Autowired import org.springframework.stereotype.Controller import org.springframework.web.bind.annotation.RequestMapping @Controller class IndexController { @Autowired lateinit var clientService: ClientService @RequestMapping("/") fun index(): String { clientService.createClient("Turreta.com") return "index" } } |
In summary, we created three objects and let Spring manage them – ClientRepository, ClientService, and IndexController. With the exception of the ClientRepository, the other two objects has dependency to at least one other object. Therefore, IndexController has a dependency to the ServiceClient which has a dependency on the ClientRepository. We used the Spring Autowired annotation to build that chain of dependencies in Kotlin.
Alternatively, we could do away with the Spring Autowired annotation in Kotlin and retrieve the dependencies from the Spring Context.