AndreaNobili
AndreaNobili

Reputation: 42957

Doubts about init and destroy bean method in Spring Bean Life Cycle

I am studying Spring Framework and now I am studying about the init and the destroy beans callbacl method.

I am following this tutorial: http://www.tutorialspoint.com/spring/spring_bean_life_cycle.htm

I have some doubts about this tutorial:

1) Why in the MainApp class he declare the context as AbstractApplicationContext but create it using the implementation ClassPathXmlApplicationContext ? Is it because I can override the init() and destroy() methods ? or for what else?

2) What exactly do the context.registerShutdownHook(); method?

Upvotes: 0

Views: 348

Answers (1)

axtavt
axtavt

Reputation: 242686

  • AbstractApplicationContext is where registerShutdownHook() is declared. So, you need to use this class (or any of its subclasses) in variable declaration in order to call registerShutdownHook() on your application context.

  • registerShutdownHook() registers a JVM shutdown hook that would close the application context (destroying all beans) upon JVM shutdown

Upvotes: 2

Related Questions