File tree Expand file tree Collapse file tree
spring-boot/src/main/java/com/baeldung/shutdownhooks Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ package com .baeldung .shutdownhooks ;
2+
3+ import org .springframework .boot .SpringApplication ;
4+ import org .springframework .boot .autoconfigure .EnableAutoConfiguration ;
5+ import org .springframework .boot .autoconfigure .SpringBootApplication ;
6+
7+ @ SpringBootApplication
8+ @ EnableAutoConfiguration
9+ public class ShutdownHookApplication {
10+
11+ public static void main (String args []) {
12+ SpringApplication .run (ShutdownHookApplication .class , args );
13+ }
14+ }
Original file line number Diff line number Diff line change 1+ package com .baeldung .shutdownhooks .beans ;
2+
3+ import javax .annotation .PreDestroy ;
4+
5+ import org .springframework .stereotype .Component ;
6+
7+ @ Component
8+ public class Bean1 {
9+
10+ @ PreDestroy
11+ public void destroy () {
12+ System .out .println ("Shutdown triggered using @PreDestroy." );
13+ }
14+ }
Original file line number Diff line number Diff line change 1+ package com .baeldung .shutdownhooks .beans ;
2+
3+ import org .springframework .beans .factory .DisposableBean ;
4+ import org .springframework .stereotype .Component ;
5+
6+ @ Component
7+ public class Bean2 implements DisposableBean {
8+
9+ @ Override
10+ public void destroy () throws Exception {
11+ System .out .println ("Shutdown triggered using DisposableBean." );
12+ }
13+
14+ }
Original file line number Diff line number Diff line change 1+ package com .baeldung .shutdownhooks .beans ;
2+
3+ public class Bean3 {
4+
5+ public void destroy () {
6+ System .out .println ("Shutdown triggered using bean destroy method." );
7+ }
8+ }
Original file line number Diff line number Diff line change 1+ package com .baeldung .shutdownhooks .config ;
2+
3+ import javax .servlet .ServletContextEvent ;
4+ import javax .servlet .ServletContextListener ;
5+
6+ public class ExampleServletContextListener implements ServletContextListener {
7+
8+ @ Override
9+ public void contextDestroyed (ServletContextEvent event ) {
10+ System .out .println ("Shutdown triggered using ServletContextListener." );
11+ }
12+
13+ @ Override
14+ public void contextInitialized (ServletContextEvent event ) {
15+ // Triggers when context initializes
16+ }
17+
18+ }
Original file line number Diff line number Diff line change 1+ package com .baeldung .shutdownhooks .config ;
2+
3+ import javax .servlet .ServletContextListener ;
4+
5+ import org .springframework .boot .web .servlet .ServletListenerRegistrationBean ;
6+ import org .springframework .context .annotation .Bean ;
7+ import org .springframework .context .annotation .Configuration ;
8+
9+ import com .baeldung .shutdownhooks .beans .Bean3 ;
10+
11+ @ Configuration
12+ public class ShutdownHookConfiguration {
13+
14+ @ Bean (destroyMethod = "destroy" )
15+ public Bean3 initializeBean3 () {
16+ return new Bean3 ();
17+ }
18+
19+ @ Bean
20+ ServletListenerRegistrationBean <ServletContextListener > servletListener () {
21+ ServletListenerRegistrationBean <ServletContextListener > srb = new ServletListenerRegistrationBean <>();
22+ srb .setListener (new ExampleServletContextListener ());
23+ return srb ;
24+ }
25+ }
You can’t perform that action at this time.
0 commit comments