11
 HelloWorld obj = (HelloWorld) context.getBean("helloWorld");
  obj.getMessage();
  context.registerShutdownHook();  

below the output:

Feb 03, 2017 11:46:12 AM org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh  
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@799f7e29: startup date [Fri Feb 03 11:46:12 IST 2017]; root of context hierarchy
Feb 03, 2017 11:46:12 AM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions  
INFO: Loading XML bean definitions from class path resource [Beans.xml]
Bean is going through init.  
Your Message : Hello World!  
Bean will destroy now.  

Whereas using context.close() gives

Feb 03, 2017 11:53:57 AM org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh  
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@799f7e29: startup date [Fri Feb 03 11:53:57 IST 2017]; root of context hierarchy
Feb 03, 2017 11:53:57 AM   org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions  
INFO: Loading XML bean definitions from class path resource [Beans.xml]  
Bean is going through init.  
Your Message : Hello World!    
Feb 03, 2017 11:53:57 AM org.springframework.context.support.ClassPathXmlApplicationContext doClose  
INFO: Closing org.springframework.context.support.ClassPathXmlApplicationContext@799f7e29: startup date [Fri Feb 03 11:53:57 IST 2017]; root of context hierarchy  
Bean will destroy now.

Could someone explain the difference?

Manuel Jordan
  • 15,253
  • 21
  • 95
  • 158
user4790067
  • 451
  • 2
  • 6
  • 12

1 Answers1

28

The ApplicationContext class doesn't define either of these methods as a part of its interface, but the ConfigurableApplicationContext does define both of these.

From the JavaDoc:

  • close() -- Close this application context, destroying all beans in its bean factory.
  • registerShutdownHook() -- Register a shutdown hook with the JVM runtime, closing this context on JVM shutdown unless it has already been closed at that time.

Basically, AbstractApplicationContext#close() will close, or shutdown, the ApplicationContext at the time it is invoked, while AbstractApplicationContext#registerShutdownHook() will close, or shutdown, the ApplicationContext at a later time when the JVM is shutting down for whatever reason. This will be achieved by utilizing the JVM shutdown hook functionality.

In either case, the actual closing is done by the doClose() method.

If you are curious about why your outputs look so similar, it is because they are effectively doing the same thing, whether you call #close() or #registerShutdownHook() at line 3 of you example. #close will shutdown right away, and #registerShutdownHook will shutdown just before the JVM will exit, which is pretty much as soon as the method is done being invoked, because it is the last line of code!

nicholas.hauschild
  • 42,483
  • 9
  • 127
  • 120
  • 5
    Five cents on regarding to calling `registerShutdownHook()` from the official documentation - _Doing so ensures a graceful shutdown and calls the relevant destroy methods on your singleton beans so that all resources are released. Of course, you must still configure and implement these destroy callbacks correctly_ [Source](https://docs.spring.io/spring/docs/5.0.3.RELEASE/spring-framework-reference/core.html#beans-factory-shutdown) – skryvets Sep 09 '19 at 10:33