Spring shutdown callback methodologies - BAEL 1831
This commit is contained in:
parent
2fe0dee64e
commit
0da637d1f9
|
@ -0,0 +1,14 @@
|
||||||
|
package com.baeldung.shutdownhooks;
|
||||||
|
|
||||||
|
import org.springframework.boot.SpringApplication;
|
||||||
|
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||||
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
|
||||||
|
@SpringBootApplication
|
||||||
|
@EnableAutoConfiguration
|
||||||
|
public class ShutdownHookApplication {
|
||||||
|
|
||||||
|
public static void main(String args[]) {
|
||||||
|
SpringApplication.run(ShutdownHookApplication.class, args);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,14 @@
|
||||||
|
package com.baeldung.shutdownhooks.beans;
|
||||||
|
|
||||||
|
import javax.annotation.PreDestroy;
|
||||||
|
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
@Component
|
||||||
|
public class Bean1 {
|
||||||
|
|
||||||
|
@PreDestroy
|
||||||
|
public void destroy() {
|
||||||
|
System.out.println("Shutdown triggered using @PreDestroy.");
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,14 @@
|
||||||
|
package com.baeldung.shutdownhooks.beans;
|
||||||
|
|
||||||
|
import org.springframework.beans.factory.DisposableBean;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
@Component
|
||||||
|
public class Bean2 implements DisposableBean {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void destroy() throws Exception {
|
||||||
|
System.out.println("Shutdown triggered using DisposableBean.");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,8 @@
|
||||||
|
package com.baeldung.shutdownhooks.beans;
|
||||||
|
|
||||||
|
public class Bean3 {
|
||||||
|
|
||||||
|
public void destroy() {
|
||||||
|
System.out.println("Shutdown triggered using bean destroy method.");
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,18 @@
|
||||||
|
package com.baeldung.shutdownhooks.config;
|
||||||
|
|
||||||
|
import javax.servlet.ServletContextEvent;
|
||||||
|
import javax.servlet.ServletContextListener;
|
||||||
|
|
||||||
|
public class ExampleServletContextListener implements ServletContextListener {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void contextDestroyed(ServletContextEvent event) {
|
||||||
|
System.out.println("Shutdown triggered using ServletContextListener.");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void contextInitialized(ServletContextEvent event) {
|
||||||
|
// Triggers when context initializes
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,25 @@
|
||||||
|
package com.baeldung.shutdownhooks.config;
|
||||||
|
|
||||||
|
import javax.servlet.ServletContextListener;
|
||||||
|
|
||||||
|
import org.springframework.boot.web.servlet.ServletListenerRegistrationBean;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
|
||||||
|
import com.baeldung.shutdownhooks.beans.Bean3;
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
public class ShutdownHookConfiguration {
|
||||||
|
|
||||||
|
@Bean(destroyMethod = "destroy")
|
||||||
|
public Bean3 initializeBean3() {
|
||||||
|
return new Bean3();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
ServletListenerRegistrationBean<ServletContextListener> servletListener() {
|
||||||
|
ServletListenerRegistrationBean<ServletContextListener> srb = new ServletListenerRegistrationBean<>();
|
||||||
|
srb.setListener(new ExampleServletContextListener());
|
||||||
|
return srb;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue