Skip to main content

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@PreDestroy. That means we don't have to explicitly use Configurable Application Context# register Shutdown Hook()the registerShutdownHook() method of a ConfigurableApplicationContext in a boot application, like we have to do in spring core application.

@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"); } } } 

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.

@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"); } } } 

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 the registerShutdownHook() method of a ConfigurableApplicationContext in a boot application, like we have to do in spring core application.

@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"); } } } 
Source Link

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.

@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"); } } }