Skip to content

Commit ff9d4d4

Browse files
authored
Merge pull request eugenp#4480 from tinomthomas/master
Spring shutdown callback methodologies - BAEL 1831
2 parents fc14d42 + 0da637d commit ff9d4d4

6 files changed

Lines changed: 93 additions & 0 deletions

File tree

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
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+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
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+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
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+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
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+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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+
}

0 commit comments

Comments
 (0)