简体   繁体   中英

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?

  • 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM