A @Bean annotation supports determining discretionary introductions and obliteration callback functions. If you noticed beans.xml in the XMLBasedSpringConfiguration project, you can find the init-method and destroy-method attributes. Here's an example of how to initialize the init-method and destroy-method attributes:
<bean id="userGreeting" class="ktPackage.UserGreeting" init-method="afterPropertiesSet" destroy-method="destroy"/>
Here's the modified code of MainAppLifeCall.kt:
fun main(args: Array<String>) {
val applicationContext = AnnotationConfigApplicationContext(ConfigFoo::class.java)
val foo: Foo = applicationContext.getBean(Foo::class.java)
applicationContext.registerShutdownHook()
}
The modified code of Foo.kt is as follows:
class Foo{
fun init(){
println("Foo is initializing...")
}
fun destroy(){
println("Foo is destroying...")
}
}
Now create a configuration class for Foo. The modified code of ConfigFoo.kt is as follows:
@Configuration
open class ConfigFoo {
@Bean(initMethod = "init", destroyMethod = "destroy")
open fun foo(): Foo {
return Foo()
}
}
The output of this project will be as follows:
Foo is initializing...
Foo is destroying...