What is the difference between registerShutdownHook () and close ()

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

below 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.  

If use 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.

Can someone explain the difference?

+4
source share
1 answer

A class ApplicationContextdoes not define either of these methods as part of its interface, but ConfigurableApplicationContextdefines both of these parameters.

From JavaDoc:

  • close () - close this application context by destroying all beans in the bean factory.
  • registerShutdownHook () - register the shutdown binding with the JVM runtime, closing this context when the JVM terminated, if it was not already closed at that time.

, AbstractApplicationContext#close() ApplicationContext , AbstractApplicationContext#registerShutdownHook() ApplicationContext , JVM - . JQM .

doClose().

, , , , #close() #registerShutdownHook() 3 . #close , #registerShutdownHook , JVM , , , , !

+7

Source: https://habr.com/ru/post/1668702/


All Articles