Close

Spring Boot - Destruction callback

[Last Updated: Oct 29, 2025]

SpringApplication implicitly registers a shutdown hook with the JVM to ensure that ApplicationContext is closed gracefully on exit. That will also call all bean methods annotated with @PreDestroy. That means we don't have to explicitly use Configurable Application Context# register Shutdown Hook() in a boot application, like we have to do in spring core application.

Example

@SpringBootConfiguration
public class ExampleMain {
    @Bean
    MyBean myBean() {
        return new MyBean();
    }

    public static void main(String[] args) {
        ApplicationContext context = SpringApplication.run(ExampleMain.class, args);
        MyBean myBean = context.getBean(MyBean.class);
        myBean.doSomething();

        //no need to call context.registerShutdownHook();
    }

    private static class MyBean {

        @PostConstruct
        public void init() {
            System.out.println("init");
        }

        public void doSomething() {
            System.out.println("in doSomething()");
        }

        @PreDestroy
        public void destroy() {
            System.out.println("destroy");
        }
    }
}

Output


. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v1.5.3.RELEASE)

2025-10-29 13:55:00.986 INFO 20992 --- [mpleMain.main()] com.logicbig.example.ExampleMain : Starting ExampleMain on JoeMsi with PID 20992 (D:\LogicBig\example-projects\spring-boot\boot-destruction-callback\target\classes started by joe2 in D:\LogicBig\example-projects\spring-boot\boot-destruction-callback)
2025-10-29 13:55:00.995 INFO 20992 --- [mpleMain.main()] com.logicbig.example.ExampleMain : No active profile set, falling back to default profiles: default
init
2025-10-29 13:55:01.784 INFO 20992 --- [mpleMain.main()] com.logicbig.example.ExampleMain : Started ExampleMain in 1.288 seconds (JVM running for 10.243)
in doSomething()
destroy

Example Project

Dependencies and Technologies Used:

  • Spring Boot 1.5.3.RELEASE
    Corresponding Spring Version 4.3.8.RELEASE
  • spring-boot-starter : Core starter, including auto-configuration support, logging and YAML.
  • JDK 1.8
  • Maven 3.3.9

Destruction Callback Example Select All Download
  • boot-destruction-callback
    • src
      • main
        • java
          • com
            • logicbig
              • example
                • ExampleMain.java

    See Also