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");
}
}
}
2017-05-17 11:56:20.564 INFO 16524 --- [mpleMain.main()] com.logicbig.example.ExampleMain : Starting ExampleMain on JoeMsi with PID 16524 (D:\LogicBig\example-projects\spring-boot\boot-destruction-callback\target\classes started by Joe in D:\LogicBig\example-projects\spring-boot\boot-destruction-callback) 2017-05-17 11:56:20.565 INFO 16524 --- [mpleMain.main()] com.logicbig.example.ExampleMain : No active profile set, falling back to default profiles: default init 2017-05-17 11:56:20.723 INFO 16524 --- [mpleMain.main()] com.logicbig.example.ExampleMain : Started ExampleMain in 0.348 seconds (JVM running for 2.94) 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.