더북(TheBook)

예제 4-16 셧다운 후크 사용([[shutdown-hook]] DestructiveBeanWithHook.java)

...
public class DestructiveBeanWithHook {

    public static void main(String... args) throws Exception {
        GenericXmlApplicationContext ctx = new GenericXmlApplicationContext();
        ctx.load("classpath:spring/app-context-annotation.xml");
        ctx.refresh();

        ctx.getBean(DestructiveBeanWithHook.class);
        ctx.registerShutdownHook();
    }
}

 

이 코드를 실행하면 지금까지 보던 것과 동일한 결과가 출력됩니다.

빈을 초기화합니다.
파일 존재여부: true
빈을 소멸합니다.
파일 존재여부: false

 

보다시피 애플리케이션이 종료될 때 destroy() 메서드를 호출하도록 명시적으로 코드를 작성하지는 않았지만, destroy() 메서드가 호출되는 것을 알 수 있습니다.

신간 소식 구독하기
뉴스레터에 가입하시고 이메일로 신간 소식을 받아 보세요.