Add ServletContextListener to embedded web app

* User can add @Bean of type EventListener (e.g.
ServletContextListener)

[Fixes #54112999] [bs-254]
This commit is contained in:
Dave Syer
2013-08-08 11:27:07 +01:00
parent 3b9df63f73
commit 42bb793155
4 changed files with 157 additions and 0 deletions

View File

@@ -16,8 +16,14 @@
package org.springframework.boot.sample.tomcat;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@@ -26,8 +32,25 @@ import org.springframework.context.annotation.Configuration;
@ComponentScan
public class SampleTomcatApplication {
private static Log logger = LogFactory.getLog(SampleTomcatApplication.class);
public static void main(String[] args) throws Exception {
SpringApplication.run(SampleTomcatApplication.class, args);
}
@Bean
protected ServletContextListener listener() {
return new ServletContextListener() {
@Override
public void contextInitialized(ServletContextEvent sce) {
logger.info("ServletContext initialized");
}
@Override
public void contextDestroyed(ServletContextEvent sce) {
logger.info("ServletContext destroyed");
}
};
}
}