Dependency injection bean

Annotate the @Bean annotation to inject dependencies. Here's the content of GreetingConfigurationDIBean.kt:

@Configuration
open class GreetingConfigurationDIBean{
@Bean
open fun greeting(): GreetingDIBean {
return GreetingDIBean(getUserDetails())
}

@Bean
open fun getUserDetails(): GreetingDetailsDIBean {
return GreetingDetailsDIBean()
}
}

When two @Beans are dependent on each other, the dependency is as simplistic as having one bean method call another.

The content of GreetingDIBean.kt is as follows:

class GreetingDIBean (private val userDetails: GreetingDetailsDIBean){
init {
println("Inside DependenciesInjectBean.GreetingDIBean constructor.")
}

fun getGreeting() {
userDetails.getGreetingDetails()
}
}

The content of GreetingDetailsDIBean.kt is as follows:

class GreetingDetailsDIBean{
init {
println("This class has all the details of the user")
}

fun getGreetingDetails(){
println("Welcome, Naruto Uzumaki!!")
}
}

The content of MainApp.kt is as follows:

fun main(args: Array<String>) {
val applicationContext = AnnotationConfigApplicationContext(GreetingConfigurationDIBean::class.java)

val greeting = applicationContext.getBean(GreetingDIBean::class.java)
greeting.getGreeting()
}

The result will be the following:

This class has all the details of the user
Inside Greeting constructor.
Welcome, Naruto Uzumaki!!