class BeanTest : **InitializingBean, DisposableBean** {
init {
println("call initBlock")
}
fun initTest() {
println("call initTest")
}
fun destroyTest() {
println("call destroyTest")
}
// 의존관계 주입이 끝나면 호출
**override fun afterPropertiesSet**() {
initTest()
}
// 스프링 컨테이너 종료시 호출
**override fun destroy**() {
destroyTest()
}
}@Configuration
class WebConfig {
**@Bean(initMethod = "initTest", destroyMethod = "destroyTest")**
fun getBeanTest(): BeanTest {
return BeanTest()
}
}
class BeanTest {
private val logger = LoggerFactory.getLogger("!!! bean check log !!!")
init {
logger.info("call initBlock")
}
fun initTest() {
logger.info("call initTest")
}
fun destroyTest() {
logger.info("call destroyTest")
}
}@Configuration
class WebConfig {
@Bean(initMethod = "initTest")
fun getBeanTest(): BeanTest {
return BeanTest()
}
}
class BeanTest {
private val logger = LoggerFactory.getLogger("!!! bean check log !!!")
init {
logger.info("call initBlock")
}
fun initTest() {
logger.info("call initTest")
}
**fun close() { // 스프링 컨테이너 종료시 호출됨
logger.info("call destroyTest")
}**
}@Configuration
class WebConfig {
@Bean()
fun getBeanTest(): BeanTest {
return BeanTest()
}
}
class BeanTest {
private val logger = LoggerFactory.getLogger("!!! bean check log !!!")
init {
logger.info("call initBlock")
}
**@PostConstruct**
fun initTest() {
logger.info("call initTest")
}
**@PreDestroy**
fun destroyTest() {
logger.info("call destroyTest")
}
}보통은 @PostConstruct, @PreDestroy 을 사용하는걸 권장하지만, 외부 라이브러리 초기화/ 소멸시 처리해야 할 과정이 있다면, @Bean 의 InitMethod, destroyMethod를 사용하면 된다.