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?
The registerShutdownHook() is a method of Spring AbstractApplicationContext class. 2. The registerShutdownHook() method registers a shutdown hook named SpringContextShutdownHook with the JVM runtime. 3. On calling registerShutdownHook() , the Spring context is closed on JVM shutdown, if not already closed.
When the web container will want to stop the application, the ContextLoadListener will receive the event from the servlet. And then based on this event it will close the application context. After the undeploy of application, the @PreDestroy method will be called and the application context will be closed.
If you use Spring's IoC container in a non-web application environment (for example, in a rich client desktop environment), register a shutdown hook with the JVM. Doing so ensures a graceful shutdown and calls the relevant destroy methods on your singleton beans so that all resources are released.
registerShutdownHook() method in Spring In Java programming language you can create shutdown hooks, where you create a new thread and provide logic that is executed when the JVM is shutting down. Then you can register your thread class instance as a shutdown hook to the VM using Runtime.
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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With