Spring   发布时间:2022-04-09  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了spring – registerShutdownHook()和close()之间有什么区别大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

 HelloWorld obj = (HelloWorld) context.getBean("HelloWorld");
  obj.getmessage();
  context.registerShutdownHook();  

输出下方:

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.  

使用COntext.close()给出

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.

有人可以解释这个区别吗?

最佳答案
@H_944_24@ApplicationContext类没有将这些方法中的任何一个定义为其接口的一部分,但ConfigurableApplicationContext确实定义了这两个方法.

来自JavaDoc:

基本上,AbstractApplicationContext #close()将在调用关闭关闭ApplicationContext,而AbstractApplicationContext #registerShutdownHook()将在JVM因任何原因关闭的时候关闭关闭ApplicationContext.这将通过利用JVM关闭挂钩功能来实现.

在任何一种情况下,实际的关闭都是通过doClose()方法完成的.

如果您对输出看起来如此相似的原因感到好奇,那是因为它们实际上是在做同样的事情,无论您是在示例的第3行调用#close()还是#registerShutdownHook(). #close会立即关闭,而#registerShutdownHook将在JVM退出之前关闭,这几乎就是调用方法的时候,因为它是最后一行代码

大佬总结

以上是大佬教程为你收集整理的spring – registerShutdownHook()和close()之间有什么区别全部内容,希望文章能够帮你解决spring – registerShutdownHook()和close()之间有什么区别所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。