The @Import annotation

Spring's @Import annotation offers functions such as <import/> an element in Spring XML. By utilizing the @Import annotation, you can import at least one @Configuration class. It can also import classes that contain no less than one @Bean function.  

The content of Boo.kt is as follows:

class Foo{
init {
println("This is class Foo")
}
}
class Boo{
init {
println("This is class Boo")
}
}

The content of ConfigBoo.kt is as follows:

@Configuration
class ConfigFoo {
@Bean
fun foo(): Foo{
return Foo()
}
}

@Configuration
@Import(ConfigFoo::class)

class ConfigBoo {
@Bean
fun foo(): Boo {
return Boo()
}
}

You don't need to specify both ConfigFoo.class and ConfigBoo.class when instantiating the context, so the following code isn't required when you initialize AnnotationConfigApplicationContext:

val applicationContext = AnnotationConfigApplicationContext(ConfigBoo::class.java, ConfigFoo::class.java)

As bean definitions of ConfigFoo are already loaded by using the @Import annotation with the ConfigBoo bean, only ConfigBoo needs to be explicitly specified:

val applicationContext = AnnotationConfigApplicationContext(ConfigBoo::class.java)

Here's the modified complete code of the main function of  MainAppImport.kt:

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

//both beans Boo and Foo will be available...
val boo: Boo = applicationContext.getBean(Boo::class.java)
val foo: Foo = applicationContext.getBean(Foo::class.java)
}

The result will be as follows:

This is class Boo
This is class Foo