Merge pull request #4480 from tinomthomas/master

Spring shutdown callback methodologies - BAEL 1831
This commit is contained in:
Tom Hombergs 2018-06-14 06:30:56 +02:00 committed by GitHub
commit ff9d4d48f3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 93 additions and 0 deletions

View File

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

View File

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

View File

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

View File

@ -0,0 +1,8 @@
package com.baeldung.shutdownhooks.beans;
public class Bean3 {
public void destroy() {
System.out.println("Shutdown triggered using bean destroy method.");
}
}

View File

@ -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
}
}

View File

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