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
1 2 3 4 5 6 7 8 |
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
1 |
((AbstractApplicationContext) applicationContext).registerShutdownHook(); |
Calling close method
1 |
((AbstractApplicationContext) applicationContext).close(); |
Conclusion
We have seen how to efficiently access Spring ApplicationContext and various options of Shutdown Spring ApplicationContext in standalone application.