Article describes the options of, how to Shutdown Spring ApplicationContext in standalone application.
Here goes an optimal approach to get access to spring ApplicationContext, by allowing spring to inject its context into our custom class via implementing ApplicationContextAware interface
public class MyApplicationContext implements ApplicationContextAware { private ApplicationContext applicationContext; public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { this.applicationContext = applicationContext; } }
That’s it, now we have access to spring application context is achieved.
Shutdown Spring ApplicationContext could be achieved using local variable applicationContext as given approaches. Reference of AbstractApplicationContext.
Registering Shutdown Hook
((AbstractApplicationContext) applicationContext).registerShutdownHook();
Calling close method
((AbstractApplicationContext) applicationContext).close();
Conclusion
We have seen how to efficiently access Spring ApplicationContext and various options of Shutdown Spring ApplicationContext in standalone application.