The hook that calls the handlers isn't registered until just before refresh. This means that a handler registered by components that are called before this (such as listeners to certain events or application context initializers) may not be called if the JVM exits before context refresh begins. We want to register the shutdown hook earlier, ideally before any calls are made to "plugins".

Comment From: wilkinsona

We need this for DockerComposeListener and DockerComposeLifecycleManager. Containers are started in response to an ApplicationPreparedEvent. This event is published at the end of SpringApplication.prepareContext but the hook that calls the registered handlers is not installed until refreshContext is called.